Search code examples
c#.netpowerpointpie-chart

Setting color of pie chart slices and legend


Does anybody know how to set the color of the slice/legend of a PowerPoint 2010 Pie Chart in C#? I can't make anything I read from the MSDN site work. I can't work out the proper way to get the correct object and property.

Edit: Well, I thought about adding code, but what I have is not working, so I wasn't sure how helpful or informative it would be. I can't figure out which object/method/property to use to get access to the pie chart slice color. I've tried Point, LegendKey, LegendEntry, Legend, and associated methods/properties with each. I've tried many things that aren't even represented in my code anymore.

But, for what it's worth, this is what my code is now:

 PowerPoint.Series series = (PowerPoint.Series)xlChart.SeriesCollection(1);
 PowerPoint.Point point = (PowerPoint.Point)series.Points(xlCol);

 point.Interior.ColorIndex = Convert.ToInt32(dataArray[2, i]);

 PowerPoint.Legend legend = (PowerPoint.Legend)xlChart.Legend;
 PowerPoint.LegendEntry lentry = (PowerPoint.LegendEntry)legend.LegendEntries(1);

Solution

  • Interior.ColorIndex won't work, because there are only two values in the enumeration: xlColorIndexAutomatic and xlColorIndexNone.

    You were pretty close, however. What you want is Interior.Color. I use hex to set the color, but I'm sure there are other ways. The example below was based on assumptions that there is an existing PowerPoint file with a pie chart on first slide and nothing else. Obviously, you'd adjust it to your conditions.

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    
    namespace SampleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                var filePath = @"C:\users\userx\desktop\test.pptx";
                var app = new PowerPoint.Application();
                var presentation = app.Presentations.Open(filePath);
                var slide = presentation.Slides[1];
                var chart = slide.Shapes[1].Chart;
                var series = chart.SeriesCollection(1) as PowerPoint.Series;
                var point = series.Points(1) as PowerPoint.Point;
                point.Interior.Color = 0x41BA5D;
                point = series.Points(2) as PowerPoint.Point;
                point.Interior.Color = 0xA841BA;
                point = series.Points(3) as PowerPoint.Point;
                point.Interior.Color = 0xBA4141;
                point = series.Points(4) as PowerPoint.Point;
                point.Interior.Color = 0x7AB4FF;
            }
        }
    }
    

    The original pie chart looked like so:

    enter image description here

    While the new chart had this appearance:

    enter image description here

    As I've mentioned, there are many ways to set the colors and I showed you the hex way. If you reference System.Drawing assembly, then you'll have access to Color, which simplifies things a lot:

    var point = series.Points(1) as PowerPoint.Point;
    point.Interior.Color = Color.Red;
    point = series.Points(2) as PowerPoint.Point;
    point.Interior.Color = Color.Pink;
    point = series.Points(3) as PowerPoint.Point;
    point.Interior.Color = Color.Black;
    point = series.Points(4) as PowerPoint.Point;
    point.Interior.Color = Color.Green;
    

    enter image description here

    The legend entries will change their color accordingly, so if you're using this approach, you don't even have to worry about setting color there.

    As you can tell, Interop can be a pain. Hope this clears some things up for you.