Articles in this section

How to bind JSON data to UWP Charts?

This article provides a step-by-step guide on how to effectively bind and display data from a JSON file within an UWP Chart. By following these instructions, you will learn to configure your UWP application to parse JSON data and present it visually using the Chart control.

 

Step 1: Define the Model class to represent the structure of your JSON data. This model will facilitate the deserialization of the JSON into a strongly typed object.

public class Model
{
    public DateTime Date { get; set; }
    public decimal CapacityUsed { get; set; }
    public decimal CapacityAvailable { get; set; }
    public decimal OverCapacity { get; set; }
    public decimal TotalHours { get; set; }
}

JSON Data (Capacities.json)

[
    {
        "Date": "2017-01-01T00:00:00",
        "CapacityUsed": 33.000,
        "CapacityAvailable": 60.000,
        "OverCapacity": 0.0,
        "TotalHours": 60.000
    },
    {
        "Date": "2017-02-05T00:00:00",
        "CapacityUsed": 53.000,
        "CapacityAvailable": 60.000,
        "OverCapacity": 0.0,
        "TotalHours": 60.000
    },
    {
        "Date": "2017-03-05T00:00:00",
        "CapacityUsed": 43.000,
        "CapacityAvailable": 60.000,
        "OverCapacity": 0.0,
        "TotalHours": 60.000
    },

Step 2: Create a ViewModel to handle the deserialization of the JSON data from an embedded resource into a list of Model objects. Ensure the JSON file's build action is set to `Embedded Resource`.

public class ViewModel 
{
    public List<Model> Items { get; set; }
 
    public ViewModel()
    {
          using (var stream = GetType().GetTypeInfo().Assembly.GetManifestResourceStream("JsonDataBinding.Capacities.json"))
          using (TextReader textStream = new StreamReader(stream))
          {
               var text = textStream.ReadToEnd();
               Items = JsonConvert.DeserializeObject<List<Model>>(text);
          }
    }
}

Step 3: Bind the List<Model> from your ViewModel to the SfChart control in your XAML. Specify the XBindingPath and YBindingPath for your series to correctly visualize the data.

<chart:SfChart>
    . . .
    <chart:ColumnSeries XBindingPath="Date"
                        YBindingPath="CapacityUsed"
                        ItemsSource="{Binding Items}" />
</chart:SfChart>

Output

Output image of the UWP chart using JSON data as the data source.


Conclusion

I hope you enjoyed learning about how to work with JSON data in UWP Charts.

You can refer to our UWP Chart feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our UWP Chart example to understand how to present and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied