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 pie chart example in C# WPF

Platform: WPF |
Control: SfChart |
Published Date: October 17, 2019 |
Last Revised Date: January 20, 2021

This article explains how to create a pie chart example in C# WPF in a step-by-step process.

 

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.

 

Step 1:

 

Create a DataModel for the PieSeries.

 

public class Person
{
    public string Name { get; set; }
    public double Weight { get; set; }
}

 

Step 2:

 

Create a ViewModel class with the DataModel collection to initialize datapoints for series.

 

public class ViewModel
{
    public List<Person> Data { get; set; }
 
    public ViewModel()
    {
        Data = new List<Person>()
        {
            new Person { Name = "David", Weight = 45 },
            new Person { Name = "Michael", Weight = 50 },
            new Person { Name = "Steve", Weight = 79 },
            new Person { Name = "Joel", Weight = 65 },
            new Person { Name = "Sam", Weight = 54 },
            new Person { Name = "Smith", Weight = 60 }
        };
    }
}

 

Step 3:

 

Initialize the chart with title by using the Header property of the chart and set the DataContext for the chart as ViewModel.

 

SfChart chart = new SfChart();
chart.DataContext = new ViewModel();
chart.Header = "Weight Report";
chart.Legend = new ChartLegend();

 

Step 4:

Define the PieSeries and add it to the chart.

 

PieSeries series = new PieSeries();
series.XBindingPath = "Name";
series.YBindingPath = "Weight";
series.ItemsSource = viewModel.Data;
chart.Series.Add(series);

 

The following code snippet gives you the consolidated configuration of all of the above codes.

 

SfChart chart = new SfChart();            
ViewModel viewModel = new ViewModel();
chart.DataContext = viewModel;
chart.Header = "Weight Report";
chart.Legend = new ChartLegend();
 
// Defining the PieSeries with adornments.
PieSeries series = new PieSeries();
series.XBindingPath = "Name";
series.YBindingPath = "Weight";
series.ItemsSource = viewModel.Data;
series.AdornmentsInfo = new ChartAdornmentInfo() 
{
    ShowLabel = true 
};
chart.Series.Add(series);

 

View the sample in GitHub.

Output

Create pie chart in C# WPF

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
Live Chat Icon