Articles in this section
Category / Section

How to create pie chart example in C# WPF

2 mins read

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied