How to apply different color to a chart series in Xamarin.Forms application?
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.
Please find the sample from this link.
Conclusion
I hope you enjoyed learning about how to apply different color to a Xamarin.Forms Chart series.
You can refer to our Xamarin.Forms Chart feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms Chart documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!