Articles in this section
Category / Section

How to apply different colors for each data point in Blazor Charts?

2 mins read

This article explains how to apply different colors to each data point in Blazor Charts using an event.

Customizing the color of data points using an event

Blazor Charts provide an option to apply colors for each point in a chart series using the OnPointRender event.

This can be achieved by defining the required colors as an array of strings and assigning each color to the Fill argument by incrementing the count in the OnPointRender event for each point.

The code example below demonstrates how to apply different colors for each data point.

Index.razor

@using Syncfusion.Blazor.Charts
<SfChart Title="Olympic Medals">
   <ChartEvents OnPointRender="PointRenderEvent"></ChartEvents>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
   <ChartSeriesCollection>
       <ChartSeries DataSource="@MedalDetails" XName="X" YName="Y"  Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

@code {
   public string[] colors = new string[] {"red", "green", "#ff0097","crimson", "blue", "darkorange" };
   int i = 0;
   public class ChartData
   {
       public string X { get; set; }
       public double Y { get; set; }
   }
   public List<ChartData> MedalDetails = new List<ChartData>
   {
       new ChartData { X= "South Korea", Y= 39.4 },
       new ChartData { X= "India", Y= 61.3 },
       new ChartData { X= "Pakistan", Y= 20.4 },
       new ChartData { X= "Germany", Y= 65.1 },
       new ChartData { X= "Australia", Y= 15.8 },
       new ChartData { X= "Italy", Y= 29.2 },
   };
   public void PointRenderEvent(PointRenderEventArgs args)
   {
       if (i >= MedalDetails.Count)
           i = 0;
       args.Fill = colors[i];
       i++;
   }
}

The following screenshot illustrates the output of the code snippet.

Output

image.png

Live Sample for Customizing Point Color

Conclusion

I hope you enjoyed learning how to apply different colors for each data point using event in Blazor Chart Component.

You can refer to our Blazor Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Chart example 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, support portal, or feedback portal. We are always happy to assist you!

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