We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to apply different color to a chart series?

Platform: Xamarin.Android |
Control: Presentation

We can apply different color to the individual chart series in a chart. For example, we can apply different color for each bar in the bar chart or for each slice in a Pie chart.

The following code example illustrates applying different colors to the chart series.

private void CreatePresentation() 
{ 
//Create a PowerPoint presentation instance 
IPresentation presentation = Presentation.Create();  
//Create a blank slide 
ISlide slide = presentation.Slides.Add(SlideLayoutType.Blank);  
//Add a chart in specific location 
IPresentationChart chart = slide.Charts.AddChart(270, 70, 500, 400); 
//Set the chart data 
chart.ChartData.SetValue(1, 1, "Gender"); 
chart.ChartData.SetValue(1, 2, "Male"); 
chart.ChartData.SetValue(1, 3, "Female"); 
chart.ChartData.SetValue(2, 1, "Accommodation"); 
chart.ChartData.SetValue(3, 1, "Food"); 
chart.ChartData.SetValue(4, 1, "Cosmetics"); 
chart.ChartData.SetValue(5, 1, "Cloths"); 
chart.ChartData.SetValue(6, 1, "Medicines"); 
chart.ChartData.SetValue(2, 2, 30); 
chart.ChartData.SetValue(3, 2, 60); 
chart.ChartData.SetValue(4, 2, 45); 
chart.ChartData.SetValue(5, 2, 55); 
chart.ChartData.SetValue(6, 2, 15); 
chart.ChartData.SetValue(2, 3, 60); 
chart.ChartData.SetValue(3, 3, 30); 
chart.ChartData.SetValue(4, 3, 75); 
chart.ChartData.SetValue(5, 3, 15); 
chart.ChartData.SetValue(6, 3, 35); 
 
//Specifies the chart title 
chart.ChartTitle = "Expenses";  
//Create a new chart-series with the name 
IOfficeChartSerie serieForMale = chart.Series.Add("Male"); 
//Set the data range for chart 
serieForMale.Values = chart.ChartData[2, 2, 6, 2]; 
 
//Create a new chart-series with the name 
IOfficeChartSerie serieForFemale = chart.Series.Add("Female"); 
//Set the data range for chart 
serieForFemale.Values = chart.ChartData[2, 3, 6, 3]; 
 
//Set the data range for category axis 
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 6, 1];  
//Set the chart type as bar chart 
chart.ChartType = OfficeChartType.Bar_Clustered; 
 
//Set color for the serie of male 
SetColorForSerie(serieForMale);  
//Set color for the serie of femle 
SetColorForSerie(serieForFemale); 
 
//Save the presentation into memory stream 
presentation.Save("sample.pptx"); 
//Close the presentation intstance 
presentation.Close(); 
} 
 
/// <summary> 
/// Set the color to serie datapoints 
/// </summary> 
/// <param name="serie">Represent the serie of the chart</param> 
private void SetColorForSerie(IOfficeChartSerie serie) 
{ 
for (int i = serie.Values.FirstRow; i <= serie.Values.LastRow; i++) 
{ 
int percentage = Convert.ToInt32(serie.Values.GetValue(i, serie.Values.FirstColumn)); 
if (percentage > 50) 
serie.DataPoints[i - serie.Values.FirstRow].DataFormat.Fill.ForeColorIndex = OfficeKnownColors.Orange; 
else 
serie.DataPoints[i - serie.Values.FirstRow].DataFormat.Fill.ForeColorIndex = OfficeKnownColors.Violet; 
} 
} 
 

 

The following screen shot displays the pie chart and bar with different color.

 

Pie chart Bar chart

 

Please find the sample from this link.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile