We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind the JSON data in WPF Chart

Platform: WPF |
Control: SfChart |
Published Date: January 24, 2020 |
Last Revised Date: June 18, 2020

This article explains how to populate the JSON data to WPF SfChart with the following steps

 

Step 1: Create the required model in accordance with the JSON data.

 

[JSON Data]

[
    {
        "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
    },
…

 

[C#] Model

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; }
}

 

Step 2: Convert JSON data to List<Model>.

 

[C#] ViewModel

public class ViewModel 
{
    public List<Model> Items { get; set; }
 
    public ViewModel()
    {
using (var stream = GetType().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 converted List<Model> to SfChart.

 

[XAML]

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

Output

How to bind JSON data to the WPF chart

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile