Articles in this section
Category / Section

How to create a .NET MAUI SfCartesianChart using the JSON data?

5 mins read

This article explains how to bind the JSON data to the .NET MAUI Charts.

 

Since JSON data cannot be directly bound to the SfCartesianChart, 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.

using Newtonsoft.Json;
…{
     …
     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.BindingContext = jsonDataCollection;
}

Step 5: Bind the deserialized JSON data to the ChartSeries ItemsSource property. Set the XBindingPath and YBindingPath properties so that the chart fetches 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

.NET MAUI column chart with JSON data

 

Download the complete sample on GitHub.

 

Conclusion

I hope you enjoyed learning how to create a .NET MAUI Chart using JSON Data (SfCartesianChart).

You can refer to our .NET MAUI Chart feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Chart documentation to understand how to present and manipulate data. 

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

Please let us know in the comments section if you have any queries or require clarification. 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