Articles in this section
Category / Section

How to apply or change colors in Excel pie chart?

4 mins read

. 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 pie chart using 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’ color can be changed by accessing it through its chart series. Refer to the following steps to use custom colors in pie chart.

Steps to apply custom colors in pie chart in Excel programmatically:

Step 1: Create a new C# console application project.

Create an empty console application in visual studio

Create a new C# console application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.Org.

Add XlsIO reference to the project

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.

Custom colors applied Pie-Chart

Output Excel document

Learn more about 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.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to apply or change colors in Excel pie chart.

You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example 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 forumsDirect-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 sign in to leave a comment
Access denied
Access denied