πŸ“Š IrbyImaging/Models/DevicesChart.cs Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class Diagram (UML)
  5. Data Model Details
  6. Serialization & JSON
  7. Sample Code Blocks & Usage
  8. Integration Examples
  9. 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 DevicesChart objects, 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

  1. Data Representation

    • Holds a string Label and a nullable double Y.
  2. Serialization Support

    • Custom JSON field names via [DataMember(Name = "...")].
  3. Ease of Instantiation

    • Public constructor takes label and y value for quick object creation.

πŸ“ˆ 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) or JsonSerializer.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. πŸš€