c# Excel Pie Chart Custom Color

Home / Uncategorized / c# Excel Pie Chart Custom Color

Question:
How to create custom color on my excel pie chart? My pie chart has 5 slice. Below are my source code.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 500, 350);
Excel.Chart chartPage = myChart.Chart;

chartRange = xlWorkSheet.get_Range("A3", "B7");
chartPage.ChartStyle = 209;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "HeaderText Title";
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded;
chartPage.Elevation = 35;
chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowLabelAndPercent ,false, true, true, false, true, false, true, true, Separator:System.Environment.NewLine);

xlWorkBook.SaveAs(saveAsLocation);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();


Answer:
You can change the ColorIndex of each point of your pie chart like this:var series = (Excel.SeriesCollection)chartPage.SeriesCollection();
series.Item(1).Points(1).Interior.ColorIndex = 3; //Red
series.Item(1).Points(2).Interior.ColorIndex = 4; //Green
series.Item(1).Points(3).Interior.ColorIndex = 5; //Blue
series.Item(1).Points(4).Interior.ColorIndex = 6; //Yellow
series.Item(1).Points(5).Interior.ColorIndex = 7; //Magenta

Here is a full list of available colors msdn

The ColorIndex property can have valid integer arguments between 0 and 56 that generate color.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *