π IrbyImaging/Models/DevicesChart.cs Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Class Diagram (UML)
- Data Model Details
- Serialization & JSON
- Sample Code Blocks & Usage
- Integration Examples
- Conclusion
π Overview & Purpose
The DevicesChart class is a simple data model (also known as a Domain/View Model) used in the IrbyImaging.UI assembly to represent chartβfriendly data points.
- It holds a label (e.g., device name or category) and a numeric value (e.g., count or percentage).
- Annotated with .NET
[DataContract]/[DataMember]attributes for JSON serialization. - Designed to be consumed by frontβend charting libraries (Highcharts, Chart.js, etc.) over a REST API.
π‘ Use Case Example: You fetch device usage statistics on the server, transform them into a list of
DevicesChartobjects, serialize to JSON, and feed into a pie/bar chart on your web UI.
π§ Dependencies & Technologies
| Technology/Namespace | Purpose |
|---|---|
System.Runtime.Serialization |
[DataContract], [DataMember] for JSON-ready models |
.NET Standard / .NET Core |
Base framework |
| JSON (Newtonsoft or System.Text) | Serialization & deserialization |
No external NuGet packages are strictly required, though you typically pair with Newtonsoft.Json or System.Text.Json.
π Key Functional Areas
-
Data Representation
- Holds a string Label and a nullable double Y.
-
Serialization Support
- Custom JSON field names via
[DataMember(Name = "...")].
- Custom JSON field names via
-
Ease of Instantiation
- Public constructor takes
labelandyvalue for quick object creation.
- Public constructor takes
π Class Diagram (UML)
classDiagram
class DevicesChart {
<<DataContract>>
+string Label
+Nullable<double> Y
+DevicesChart(string label, double y)
}
π Data Model Details
| Member | Type | JSON Field | Description |
|---|---|---|---|
| Label | string |
label |
The category or device name. |
| Y | double? (Nullable) |
y |
Numeric value (count, percentage). |
Constructor
/// <summary>
/// Create a new chart data point.
/// </summary>
/// <param name="label">The descriptive label.</param>
/// <param name="y">The numeric value (double).</param>
public DevicesChart(string label, double y)
{
this.Label = label;
this.Y = y;
}
π Serialization & JSON
- Applies
[DataContract]on the class. - Applies
[DataMember(Name = "...")]on fields to control JSON property names.
Example JSON output:
[
{ "label": "Tablet", "y": 120.0 },
{ "label": "Smartphone", "y": 300.0 },
{ "label": "Desktop", "y": 80.0 }
]
π‘ Use
JsonConvert.SerializeObject(devicesChartsList)(Newtonsoft) orJsonSerializer.Serialize(...)(System.Text.Json).
π¨ Sample Code Blocks & Usage
1. Preparing Data in C# (Server Side)
using IrbyImaging.DomainClasses;
using System.Collections.Generic;
public List<DevicesChart> GetDeviceUsageChartData()
{
// Example raw data source
var usage = new Dictionary<string, int>
{
["Desktop"] = 80,
["Smartphone"] = 300,
["Tablet"] = 120
};
var chartData = new List<DevicesChart>();
foreach (var kvp in usage)
{
chartData.Add(new DevicesChart(kvp.Key, kvp.Value));
}
return chartData;
}
2. Serializing to JSON
var devicesList = GetDeviceUsageChartData();
string json = Newtonsoft.Json.JsonConvert.SerializeObject(devicesList);
// => '[{"label":"Desktop","y":80.0}, ...]'
βοΈ Integration Examples
Highcharts (JavaScript)
<script src="https://code.highcharts.com/highcharts.js"></script>
<script>
fetch('/api/devices/usage')
.then(res => res.json())
.then(data => {
Highcharts.chart('container', {
chart: { type: 'pie' },
title: { text: 'Device Usage Breakdown' },
series: [{
name: 'Devices',
colorByPoint: true,
data: data
}]
});
});
</script>
<div id="container"></div>
Chart.js (JavaScript)
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
fetch('/api/devices/usage')
.then(res => res.json())
.then(data => {
const labels = data.map(d => d.label);
const values = data.map(d => d.y);
new Chart(document.getElementById('myChart'), {
type: 'bar',
data: {
labels: labels,
datasets: [{ data: values, backgroundColor: ['#4e73df','#1cc88a','#36b9cc'] }]
}
});
});
</script>
π Conclusion
The DevicesChart class is a lightweight, JSONβfriendly data holder tailored for feeding charting libraries in the front end. By leveraging .NETβs DataContract serialization attributes, it ensures clear, concise JSON payloads. Integrate it into your API controllers or service layer to quickly transform analytics data into graphical dashboards with minimal overhead.
Β© 2022 IrbyImaging.UI β All Rights Reserved. π