0

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();
Tuan Zaidi
  • 343
  • 1
  • 14
  • The BEST advice someone can give you, isn't necessarily a fish, but how to convert Recorded Macro's to C# to fish for life: https://blogs.msdn.microsoft.com/csharpfaq/2010/09/27/converting-a-vba-macro-to-c-4-0/ – Jeremy Thompson Aug 15 '17 at 05:42

2 Answers2

1

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.

Ben
  • 763
  • 1
  • 5
  • 14
0

I believe you can alter the ForeColor property on the series points format. Something like:

var series = (Excel.SeriesCollection)chartPage.SeriesCollection();
var points = (Excel.Points)series.Item(1).Points();
points.Item(1).Format.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbRed;
points.Item(2).Format.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbBlue;

...and so on.

Daniel Forslund
  • 241
  • 1
  • 8
  • Just be careful of advertising double dots in Interop. Here is a [couple](https://stackoverflow.com/questions/13069153) of [posts](https://stackoverflow.com/questions/29067714) on it. – Jeremy Thompson Aug 15 '17 at 05:43