简体   繁体   English

设置饼图切片和图例的颜色

[英]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#? 有人知道如何在C#中设置PowerPoint 2010饼图的切片/图例的颜色吗? I can't make anything I read from the MSDN site work. 我无法从MSDN站点上读取任何内容。 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. 我已经尝试了Point,LegendKey,LegendEntry,Legend以及与之相关的方法/属性。 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);

Interior.ColorIndex won't work, because there are only two values in the enumeration: xlColorIndexAutomatic and xlColorIndexNone . Interior.ColorIndex将不起作用,因为枚举中只有两个值: xlColorIndexAutomaticxlColorIndexNone

You were pretty close, however. 但是,您距离很近。 What you want is Interior.Color . 您想要的是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. 下面的示例基于以下假设:第一张幻灯片上已有饼图的PowerPoint文件,仅此而已。 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: 原始饼图如下所示:

在此处输入图片说明

While the new chart had this appearance: 尽管新图表具有以下外观:

在此处输入图片说明

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: 如果引用System.Drawing程序集,则可以访问Color ,这大大简化了事情:

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;

在此处输入图片说明

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. 如您所知,Interop可能很痛苦。 Hope this clears some things up for you. 希望这可以为您清除一些麻烦。

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: 如果您正确地引用了Microsoft.Office.Interop.PowerPoint对象库( https://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.aspx ),则可以更改饼图点颜色如以下C#代码示例所示完成:

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

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM