Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.
Wed Mar 28, 2018 5:28 pm
There is only 1 series in my chart. However, i went each bar to be a different color. How can i do this?
Login to view the files attached to this post.
-
ghoff12
-
- Posts: 11
- Joined: Wed Dec 20, 2017 7:12 pm
Thu Mar 29, 2018 2:40 am
Hello,
Thanks for your inquiry. Please refer to the below code snippet to change the color of each Bar.
- Code: Select all
Presentation ppt = new Presentation();
ppt.LoadFromFile(@"sample.pptx");
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
//set 1st bar as red
ChartDataPoint cdp0 = new ChartDataPoint(chart.Series[0]);
cdp0.Index = 0;
cdp0.Fill.FillType = FillFormatType.Solid;
cdp0.Fill.SolidColor.KnownColor = KnownColors.Red;
chart.Series[0].DataPoints.Add(cdp0);
//set 2nd bar as yellow
ChartDataPoint cdp1 = new ChartDataPoint(chart.Series[0]);
cdp1.Index = 1;
cdp1.Fill.FillType = FillFormatType.Solid;
cdp1.Fill.SolidColor.KnownColor = KnownColors.Yellow;
chart.Series[0].DataPoints.Add(cdp1);
//set 3rd bar as blue
ChartDataPoint cdp2 = new ChartDataPoint(chart.Series[0]);
cdp2.Index = 2;
cdp2.Fill.FillType = FillFormatType.Solid;
cdp2.Fill.SolidColor.KnownColor = KnownColors.Blue;
chart.Series[0].DataPoints.Add(cdp2);
ppt.SaveToFile("Accessdatapoint.pptx", FileFormat.Pptx2010);
Best regards,
Simon
E-iceblue support team
-
Simon.yang
-
- Posts: 620
- Joined: Wed Jan 11, 2017 2:03 am
Thu Mar 29, 2018 4:12 pm
That worked, and i did something similar for a chart that has multiple categories and series. However, the legend doesn't reflect the colors. How can i change the colors in the legend.
this is how i modified the colors for each bar
- Code: Select all
foreach (var cat in chart.Categories)
{
int seriesCnt = 0;
foreach (var ser in chart.Series)
{
ChartDataPoint cdp = new ChartDataPoint(chart.Series[seriesCnt]);
cdp.Index = catCnt;
cdp.Fill.FillType = FillFormatType.Solid;
cdp.Fill.SolidColor.Color = System.Drawing.ColorTranslator.FromHtml("#" + StdColors[seriesCnt].ToString());
chart.Series[seriesCnt].DataPoints.Add(cdp);
seriesCnt++;
}
catCnt++;
}
Login to view the files attached to this post.
-
ghoff12
-
- Posts: 11
- Joined: Wed Dec 20, 2017 7:12 pm
Fri Mar 30, 2018 3:02 am
Hello,
Thanks for your feedback. When there is a chart that has multiple categories and series, you just need to set colors to the series like below and the legends will automatically reflect the colors.
- Code: Select all
Presentation ppt = new Presentation();
ppt.LoadFromFile(@"chart.pptx");
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
chart.Series[0].Fill.SolidColor.Color = System.Drawing.Color.Red;
Best regards,
Simon
E-iceblue support team
-
Simon.yang
-
- Posts: 620
- Joined: Wed Jan 11, 2017 2:03 am
Fri Mar 30, 2018 1:59 pm
Thank you! That worked.
-
ghoff12
-
- Posts: 11
- Joined: Wed Dec 20, 2017 7:12 pm
Mon Apr 02, 2018 1:24 am
Hello,
Thanks for your feedback. Welcome to write to us if there is any other query.
Best regards,
Simon
E-iceblue support team
-
Simon.yang
-
- Posts: 620
- Joined: Wed Jan 11, 2017 2:03 am