How to apply custom colors for pie chart in WinForms XIsIO?
. NET Excel (XlsIO) library is used to create, read, and edit Excel documents. Using this library, you can apply custom colors or change colors in a pie chart using the Chart feature in C# and VB.NET.
When a pie chart is created in Excel, it will have default colors for chart data points. These data points’ colors can be changed by accessing them through their chart series. Refer to the following steps to use custom colors in a pie chart.
Steps to apply custom colors in pie chart in Excel programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.Org.
Install NuGet package
Step 3: Include the following namespaces in the program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Use the following code snippet to apply custom colors in pie chart in Excel file.
C#
// Instantiate the spreadsheet creation engine using (ExcelEngine excelEngine = new ExcelEngine()) { // Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("CustomColorPieChart.Input.xlsx"); IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(fileStream); IWorksheet worksheet = workbook.Worksheets[0]; // Applying custom color IChartSerie serie = worksheet.Charts[0].Series[0]; serie.DataPoints[0].DataFormat.Fill.FillType = ExcelFillType.SolidColor; serie.DataPoints[0].DataFormat.Fill.ForeColor = Color.FromArgb(0, 255, 0); serie.DataPoints[1].DataFormat.Fill.FillType = ExcelFillType.SolidColor; serie.DataPoints[1].DataFormat.Fill.ForeColor = Color.FromArgb(146, 208, 80); serie.DataPoints[2].DataFormat.Fill.FillType = ExcelFillType.SolidColor; serie.DataPoints[2].DataFormat.Fill.ForeColor = Color.FromArgb(255, 0, 0); // Saving and Closing the workbook workbook.SaveAs("Output.xlsx"); }
VB.NET
'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine() 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Module1).GetTypeInfo().Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("CustomColorPieChart.Input.xlsx") Dim application As IApplication = excelEngine.Excel Dim workbook As IWorkbook = application.Workbooks.Open(fileStream) Dim worksheet As IWorksheet = workbook.Worksheets(0) Dim serie As IChartSerie = worksheet.Charts(0).Series(0) 'Applying custom color serie.DataPoints(0).DataFormat.Fill.FillType = ExcelFillType.SolidColor serie.DataPoints(0).DataFormat.Fill.ForeColor = Color.FromArgb(0, 255, 0) serie.DataPoints(1).DataFormat.Fill.FillType = ExcelFillType.SolidColor serie.DataPoints(1).DataFormat.Fill.ForeColor = Color.FromArgb(146, 208, 80) serie.DataPoints(2).DataFormat.Fill.FillType = ExcelFillType.SolidColor serie.DataPoints(2).DataFormat.Fill.ForeColor = Color.FromArgb(255, 0, 0) 'Saving and Closing the workbook workbook.SaveAs("Output.xlsx") End Using
A complete working example to apply custom colors in pie chart in Excel can be downloaded from Apply custom color for pie chart.zip.
By executing the program, you will get the output Excel file as shown below.
Output Excel document
Learn more about the Syncfusion® Excel (XlsIO) library through the documentation, where you will find supported features like charts and drawing objects with respective code examples.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
An online sample link for charts.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, PDF, CSV and Image conversions with code examples.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to apply custom colors for pie chart in WinForms XIsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!