Articles in this section

How to format category axis labels dynamically in Blazor Charts?

This article explains how to format category axis labels dynamically in Blazor Charts.

Format the Category Axis Using an Event

Blazor Charts provides an option to format category axis labels dynamically. This can be achieved by utilizing the OnAxisLabelRender event to format the axis label using the Text argument.

In this code snippet, the OnAxisLabelRender event is utilized to format the axis labels of the chart. The AxisLabelEvent method modifies the Text of the axis labels by appending “2024” to each label on the PrimaryXAxis. This customization is achieved through the AxisLabelRenderEventArgs argument, which provides access to the axis label Text.

Index.razor:

@using Syncfusion.Blazor.Charts

<SfChart>
   <ChartEvents OnAxisLabelRender="AxisLabelEvent"></ChartEvents>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
   <ChartSeriesCollection>
       <ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="ChartSeriesType.Column">
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

@code {
   public class SalesInfo
   {
       public string Month { get; set; }
       public double SalesValue { get; set; }
   }

   public List<SalesInfo> Sales = new List<SalesInfo>
   {
       new SalesInfo { Month = "Jan", SalesValue = 35 },
       new SalesInfo { Month = "Feb", SalesValue = 28 },
       new SalesInfo { Month = "Mar", SalesValue = 34 },
       new SalesInfo { Month = "Apr", SalesValue = 32 },
       new SalesInfo { Month = "May", SalesValue = 40 },
       new SalesInfo { Month = "Jun", SalesValue = 32 },
       new SalesInfo { Month = "Jul", SalesValue = 35 }
   };

   public void AxisLabelEvent(AxisLabelRenderEventArgs args)
   {
       if (args.Axis.Name == "PrimaryXAxis")
       {
           args.Text = args.Text + " 2024";
       }
   }
} 

The following screenshot illustrates the output of the code snippet:

chrome_IAx0XkDTwD.png

Live Sample for Formatting the Category Axis Using the OnAxisLabelRender Event

Conclusion:

We hope you enjoyed learning how to format category axis labels dynamically in Blazor Charts.

You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.

If you have any questions 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)
Access denied
Access denied