In this topic, we will explain easy-to-use steps on how to create Excel Pie Chart in C# .NET application. The steps below exhibit how to create dynamic Pie chart in C# windows application programmatically.
Commonly, a Pie chart denotes categorical data whereas each pie slice can show specific category. In MS Excel, you may utilize rich set of chart tools. So, you can make Excel Pie chart in C# project on the fly. Next, you may save to Excel XLSX format. You can simply open the output XLSX file into some Excel viewer to display the graph.
Steps to Create Excel Pie Chart in C#
- Download Aspose.Cells for .NET package from NuGet Gallery
- Import relevant Aspose.Cells and Aspose.Cells.Charts namespaces with using directive
- Instantiate License object and set the license file using SetLicense method
- Create a Workbook object to create a new Excel workbook
- Get the first worksheet in the workbook and add data to the worksheet
- Add new Pie chart using ChartType.Pie enum member and set its data series with other attributes
- Save the output Excel file that contains the Pie chart in it
In below example, we will be creating a Pie chart dynamically based on quarterly sales data. The source data for the chart will also be pasted via code. Moreover, we set chart’s title and specify data labels properties to show values and percentages on the pie slices. We can use ChartType enumeration to select or set other similar types to draw charts.
Sample code to Create Excel Pie Chart in C#
The above code snippet inserts some data into the first worksheet cells and then creates an Excel Pie graph in C# based on that data source. Finally, it saves an Excel XLSX file which will contain the Pie chart. Moreover, you can convert Excel chart to JPG in C# without Interop as well.