2
votes

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);
2
Got some code for us?B.K.
Well, I could show you some code, but it's wrong. I wasn't sure that was going to be helpful. I don't even think I'm close. I mean, I can't even figure out, from the online MSND documentation, which object/method/property I need to change. I've tried Point, derived from Series, derived from SeriesCollection. I've tried LegendKey from LegendEntry, derived from LegendEntries, derived from Legend. I've tried things I can't even remember now. Anway, for what it's worth, this is what my code currently looks like:Henry
Is it fair to assume that you're doing this through COM and that the PowerPoint file already exists?B.K.
Yes, that is a correct assumption.Henry

2 Answers

3
votes

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.

1
votes

Provided you are properly referencing Microsoft.Office.Interop.PowerPoint object library (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.aspx), changing the Pie Chart Points Color could be done as shown in the following C# code sample:

xlChart.Series[0].Points[0].Color = Color.Red;
xlChart.Series[0].Points[1].Color = Color.Blue;

Hope this will help.