How to add custom axis labels in a Blazor Chart?
This article explains how to add custom axis label in Blazor Chart.
Customizing Axis Labels Using the OnAxisLabelRender Event in a Blazor Chart
Axis label in Blazor Chart has been generated based on the provided data points. Blazor Chart supports you to change the entire text with your desired text.
We can add the custom label to the axis label in Blazor Chart using OnAxisLabelRender event. This event triggers before each axis label are rendered.
This event contains the following arguments.
• LabelStyle – Specifies the font information of the axis label.
• Text – Specifies the text to be displayed in the axis label.
• Value – Specifies the value of the axis label.
We can add custom label to the axis label in Blazor Chart by using the Text property in the OnAxisLabelRender event.
The following code example illustrates this.
Index.razor
@using Syncfusion.Blazor.Charts
<SfChart Width="600">
<ChartEvents OnAxisLabelRender="AxisLabelEvent"></ChartEvents>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="ChartSeriesType.Line">
</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 = 10 },
new SalesInfo { Month = "Feb", SalesValue = 12 },
new SalesInfo { Month = "Mar", SalesValue = 20 },
new SalesInfo { Month = "Apr", SalesValue = 28 },
new SalesInfo { Month = "May", SalesValue = 35 },
new SalesInfo { Month = "Jun", SalesValue = 40 },
new SalesInfo { Month = "Jul", SalesValue = 50 }
};
public void AxisLabelEvent(AxisLabelRenderEventArgs args)
{
if(args.Axis.Name == "PrimaryYAxis")
{
if(args.Value < 20)
{
args.Text = "low";
}
if (args.Value >= 20 && args.Value <= 30)
{
args.Text = "average";
}
if(args.Value > 30)
{
args.Text = "high";
}
}
}
}
The following screenshot illustrates the output of the above code snippet.
Output:
Conclusion
I hope you enjoyed learning how to add custom axis label 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!