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 create Chart control example in C# WPF


This KB article explains how to create a simple chart with header, legend, axis, and series in WPF.

 

The User Guide Documentation helps you to acquire more knowledge on chart and its features. You can also refer to the Feature Tour site to get an overview on all the features in chart. The assembly required for creating the chart is manifested in this documentation.

 

Creating chart title

 

The title can be set by using the Header property of the chart.

 

[C#]

SfChart chart = new SfChart();
chart.Header = "Chart";

 

Creating axis for chart

 

The required types of axes can be set by using PrimaryAxis and SecondaryAxis properties as demonstrated in the following code snippet.

 

[C#]

CategoryAxis primaryAxis = new CategoryAxis();
primaryAxis.Header = "Name";
primaryAxis.FontSize = 14;
chart.PrimaryAxis = primaryAxis;
 
NumericalAxis secondaryAxis = new NumericalAxis();
secondaryAxis.Header = "Height(in cm)";
secondaryAxis.FontSize = 14;
chart.SecondaryAxis = secondaryAxis;

 

To know more about various types of axis supported by chart, refer to this chart axis documentation.

Adding series

Add the required type of series to the chart and set the populated data collection to the series using the ItemsSource property of the chart series. Then, map the XBindingPath and YBindingPath properties of chart series to the required properties in the model to retrieve the data from the model and render the chart.

 

[C#]

ColumnSeries columnSeries = new ColumnSeries();
columnSeries.XBindingPath = "Name";
columnSeries.YBindingPath = "Height";
columnSeries.ItemsSource = (chart.DataContext as ViewModel).Data;
columnSeries.Label = "Height";
chart.Series.Add(columnSeries);

 

The various types of series supported by the chart are Cartesian, accumulation, financial, stacking, range, and circular and its sub types are discussed in the following series documentation.

 

Populating data for chart series

Generate the required Model and ViewModel to bind the data point for the chart. The following code snippet demonstrates a simple data model that represents a data point and create the view model for data.

 

[C#]

public class Person
{
    public string Name { get; set; }
 
    public double Height { get; set; }
}
 
public class ViewModel
{
    public List<Person> Data { get; set; }
 
    public ViewModel()
    {
        Data = new List<Person>()
        {
            new Person { Name = "David", Height = 180 },
            new Person { Name = "Michael", Height = 170 },
            new Person { Name = "Steve", Height = 160 },
            new Person { Name = "Joel", Height = 182 },
        };
    }
}

 

Set the ViewModel instance as the DataContext for the chart or your window ; this is done to bind properties of ViewModel to chart.

 

[C#]

chart.DataContext = new ViewModel();

 

The various types of data binding supported by chart can be found in this documentation.

Adding legend

The legend can be added by using the Legend property of the chart. The following code snippet shows how to add legend for the chart.

 

[C#]

chart.Legend = new ChartLegend();

 

In addition, you need to set label for each series using Label property of ChartSeries, which will be displayed in corresponding legend item.

 

[C#]

columnSeries.Label = "Height";

 

To know more about legend and its customizations, refer to the legend documentation.

The following code snippet gives you the consolidated configuration of all the above codes in creating a simple chart.

 

Code snippet [C#]

SfChart chart = new SfChart();
 
// Setting header for the SfChart.
chart.Header = "Chart";
 
// Setting DataContext for SfChart.
chart.DataContext = new ViewModel();
 
// Adding Legend to the SfChart.
chart.Legend = new ChartLegend();
 
// Initialize the horizontal axis for SfChart.
CategoryAxis primaryAxis = new CategoryAxis();
primaryAxis.Header = "Name";
primaryAxis.FontSize = 14;
chart.PrimaryAxis = primaryAxis;
 
// Initialize the vertical axis for SfChart.
NumericalAxis secondaryAxis = new NumericalAxis();
secondaryAxis.Header = "Height(in cm)";
secondaryAxis.FontSize = 14;
chart.SecondaryAxis = secondaryAxis;
 
// Initialize the series for SfChart.
ColumnSeries columnSeries = new ColumnSeries();
columnSeries.XBindingPath = "Name";
columnSeries.YBindingPath = "Height";
columnSeries.ItemsSource = (chart.DataContext as ViewModel).Data;
columnSeries.Label = "Height";
chart.Series.Add(columnSeries);

View the sample in GitHub.


Output

Create Simple Chart in WPF

 

Take a moment to peruse the documentation, where you can find the brief description about creation of chart. Refer here to explore interactive features available in chart.

 

See also

How to create chart in WPF application using XAML

How to create pie chart in C# WPF

How to create chart in VB .NET WPF

How to create chart in VB .NET Windows Forms

How to create chart in Xamarin.Forms

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