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 through a step-by-step process.

 

The User Guide Documentation can help you acquire more knowledge about  WPF Chart and their features. You can also refer to the Feature Tour site to get an overview of all the features available in the chart. The assemblies required for creating the chart are detailed 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

 

Initialize data points for the series with a DataModel collection.

 

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

  

 Add a title to the chart using the Header property 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);

 

Below is the consolidated configuration of all the above code.

 

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


Output

Create pie chart in C# WPF

For a detailed view, explore the sample on GitHub.


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

 


Conclusion

I hope you enjoyed learning how to create pie chart example in C# WPF.

You can refer to our WPF Chart feature tour page know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart Examples to understand how to create and manipulate data.

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

If you have any queries or require clarifications, please let us know in the comments section below. 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