Articles in this section
Category / Section

How to apply different color to the individual data points of a chart series in WinForms ?

1 min read

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 
//Set color for the serie of femle 
//Save the presentation into memory stream 
//Close the presentation intstance 
/// <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; 
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.


Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment