Free Support Forum - aspose.com

Updating charts colour

I was wondering how i would be able to change the colour of the series on the chart though c# code

i hvae a chart ex and am updating the chart and that when the colour is not being displayes

Hi Jim,


Thanks for inquiring Aspose.Slides.

I have observed the requirements shared by you. Please visit this thread link for your kind reference. I have shared a comprehensive example for setting different chart entities and using them. Please share, if I may help you further in this regard.

Many Thanks,


chart = (ChartEx)shape;
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
        </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">ChartSeriesEx</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> series </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> chart</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">ChartData</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">Series</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">[</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">];</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">

   </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139); background-position: initial initial; background-repeat: initial initial;">for</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">(</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">int</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> i</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">=</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">i</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"><</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">9</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">i</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">++)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">
   </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">

        fact</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">GetCell</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">"Index"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">
        series</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">Values</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">check</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">].</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175); background-position: initial initial; background-repeat: initial initial;">Value</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial;">10</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">
   </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent;">}</span></code></pre><pre class="lang-cs prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; line-height: 18px;">this is the code that i am usingf would like to know how to use it in this</pre><pre class="lang-cs prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; background-color: rgb(238, 238, 238); overflow: auto; width: auto; max-height: 600px;"><font face="Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif"><span style="font-size: 14px; line-height: 18px;">the code the link does not work as and says Color.Red is not in the </span><span style="font-size: 14.44444465637207px; line-height: 17.981481552124023px;">context</span></font></pre>

Hi Jim,


I have observed the requirements of modifying the chart series value in for loop and have generated the following code for you. I also like to add that the sample code shared by you is not right and also the Color.Red issue is not highlighted from your code. Please share the complete sample application if there is still issue on your end.

public static void ModifyChart()
{
//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx(“D:\Aspose Data\AsposeChart.pptx”);

//Access first slide
SlideEx sld = pres.Slides[0];

// Add chart with default data
ChartEx chart = (ChartEx)sld.Shapes[0];

//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

//Getting the chart data worksheet
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;

//Take first chart series
ChartSeriesEx series = chart.ChartData.Series[0];

//Now updating series data
fact.GetCell(defaultWorksheetIndex, 0, 1, “New_Series1”);//modifying series name
for (int i = 0; i < series.Values.Count; i++)
{
if(i==0)
series.Values[i].Value = 90;
else if(i==1)
series.Values[i].Value = 123;
else if(i==2)
series.Values[i].Value = 44;
}
//Take Second chart series
series = chart.ChartData.Series[1];


//Now updating series data
fact.GetCell(defaultWorksheetIndex, 0, 2, “New_Series2”);//modifying series name
for (int i = 0; i < series.Values.Count; i++)
{
if (i == 0)
series.Values[i].Value = 23;
else if (i == 1)
series.Values[i].Value = 67;
else if (i == 2)
series.Values[i].Value = 99;
}

//Now, Adding a new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, “Series 3”), chart.Type);

//Take 3rd chart series
series = chart.ChartData.Series[2];

//Now populating series data
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, 20));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 2, 3, 50));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 3, 3, 30));
chart.Type = ChartTypeEx.ClusteredCylinder;

// Save presentation with chart
pres.Write(@“D:\Aspose Data\AsposeChartMoodified.pptx”);


}

Many Thanks,

The code that u have given does not change the color of the chart but create the chart i know how to do that and would like to know how to set a color of a certain series based on the code that has been given…


Hi Jim,


Please use the sample code shared over this thread link. The thread link contains a working sample code that will help you in understanding the concept of working with different chart entities. Please look for the following code in the shared code as it is responsible for setting the particular series fill color. Please share, if I may help you further in this regard.

ChartSeriesEx series = chart.ChartData.Series[0];
series.Format.Fill.FillType = FillTypeEx.Solid;
series.Format.Fill.SolidFillColor.Color = Color.FromArgb(32, 202, 250);
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, 40));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 2, 1, 64));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 3, 1, 50));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 4, 1, 1));

Many Thanks,