How to add custom text to the legend in Blazor Charts?
This article explains how to add custom text to the legend in Blazor Charts.
Add Custom Text to the Legend Using an OnLegendItemRender Event
Blazor Charts offers an option to add custom text to the legend using the OnLegendItemRender event. To include custom text in the legend, you need to retrieve the Text argument from the OnLegendItemRender event and append the custom text to this argument. In this case, we used currency-formatted text.
The following code example demonstrates how to add custom text using the OnLegendItemRender event.
Index.razor
@using Syncfusion.Blazor
@using System.Globalization
@using Syncfusion.Blazor.Charts
<SfAccumulationChart>
<AccumulationChartEvents OnLegendItemRender="LegendItemRender"></AccumulationChartEvents>
<AccumulationChartSeriesCollection>
<AccumulationChartSeries DataSource="@PieChartPoints" XName="Company" YName="Value" Radius="50%">
</AccumulationChartSeries>
</AccumulationChartSeriesCollection>
</SfAccumulationChart>
@code {
public List<PieData> PieChartPoints = new List<PieData>
{
new PieData { Company = "Microsoft", Value = 7510753, Radius = "100"},
new PieData { Company = "Wipro", Value = 530953 , Radius = "124.6"},
new PieData { Company = "Adobe", Value = 247853, Radius = "118.7"},
new PieData { Company = "Auto Desk", Value = 900742 , Radius = "137.5"}
};
public class PieData
{
public string Company { get; set; }
public double Value { get; set; }
public string Radius { get; set; }
}
void LegendItemRender(AccumulationLegendRenderEventArgs args)
{
args.Text += " " + CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol + PieChartPoints.Find(x => x.Company == args.Text).Value.ToString("N0", CultureInfo.CurrentCulture);
}
}
The following screenshot illustrates the output of the code snippet.
Live Sample for Adding Custom Text to the Legend
Conclusion
I hope you enjoyed learning how to add custom text to the legend using an event in Blazor Charts.
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!