We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to apply or change colors in Excel pie chart?

Platform: WinForms |
Control: XlsIO

Syncfusion Excel (XlsIO) library is a .NET Excel library 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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile