Articles in this section
Category / Section

How to Create a WinUI Chart Using the JSON Data (SfCartesianChart)?

2 mins read

This article explains how to bind the JSON data to the WinUI charts.

JSON data cannot be bound directly to the SfCartesianChart, so you should deserialize the JSON data to a bindable format. Use the open-source NuGet Newtonsoft.Json to serialize and deserialize the JSON objects.

The following steps explain how to pass JSON data to the SfCartesianChart.

JSON data

{
 'Country': 'USA',
 'Count': 46
}, {
 'Country': 'China',
 'Count': 36
}, {
 'Country': 'Japan',
 'Count': 63
}, {
 'Country': 'Australia',
 'Count': 54
}, {
 'Country': 'France',
 'Count': 50
}
 

 

Step 1: Add the Newtonsoft.Json reference to your project.

Step 2: Create a data model to store JSON data.

public class Medal
{
    public string Country { get; set; }
    public int Count { get; set; }
}
 

 

Step 3: Create a ViewModel to store a collection of model objects.

public class ViewModel
{
    public ObservableCollection<Medal> Medal { get; set; }
}
 

 

Step 4: Deserialize the JSON data as a collection of data models.

{
     …
     string jsonData = @"{'Medal':[{'Country':'USA','Count':46},{'Country':'China','Count':36},{'Country':'Japan','Count':63},{'Country':'Australia','Count':54},{'Country':'France','Count':50}]}";
 
     var jsonDataCollection = JsonConvert.DeserializeObject<ViewModel>(jsonData);
     chart.DataContext = jsonDataCollection;
}
 

 

Step 5: Bind the deserialized JSON data to the ChartSeries ItemsSource property and set the XBindingPath and YBindingPath properties, so that the chart would fetch values from the respective properties in the data model to plot the series.

<chart:SfCartesianChart x:Name="chart">
    …
    <chart:ColumnSeries ItemsSource="{Binding Medal}"
                        XBindingPath="Country" 
                        YBindingPath="Count">
    </chart:ColumnSeries>
</chart:SfCartesianChart>
 

 

Output

WinUI column chart with JSON data

View sample in GitHub

 

 

 

See also

 How to create a Column Chart in WinUI (SfCartesianChart)?

How to bind the SQLite Database to the WinUI Chart (SfCartesianChart)?

Conclusion

I hope you enjoyed learning about how to Create a WinUI Chart Using the JSON Data (SfCartesianChart).

You can refer to our WinUI Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinUI Chart documentation to understand how to present and manipulate data. 

For current customers, you can check out our WinUI 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 WinUI Chart and other WinUI components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-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)
Please  to leave a comment
Access denied
Access denied