How to Display Unique Images for Data Label in Blazor Charts?
This article explains how to display different images for each data label in Blazor Charts.
Displaying unique images for datalabels
Blazor Charts provides an option to display custom content inside chart data labels using the Template feature of the ChartDataLabel tag.
This can be achieved by assigning a specific image URL for each data label based on the X-value of the chart series data point. Within the Template, you can then set the src attribute of the image tag by retrieving the corresponding URL using the X-value.
The below code example demonstrates how to display sport-specific images with the respective count of boys participating in a chart.
@using Syncfusion.Blazor.Charts
<SfChart Title="Athletes in Popular School">
<ChartSeriesCollection>
<ChartSeries Name="Boys" DataSource="PopulationDetails" XName="Sports" YName="Boys" ColumnWidth="0.75" ColumnSpacing="0.5" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
<ChartSeriesAnimation Enable="false"></ChartSeriesAnimation>
<ChartMarker Visible="false" Height="10" Width="10" Shape="ChartShape.Circle">
<ChartDataLabel Visible="true" Position="Syncfusion.Blazor.Charts.LabelPosition.Outer">
<Template>
@{
var data = context as ChartDataPointInfo;
var imageSrc = GetImageForSport(data.X.ToString());
}
<div id='templateWrap' style="display: flex; align-items: center; gap: 5px; border-radius: 3px;">
<img src="@imageSrc" width="25" height="25" alt="Sport Icon" />
<span style="color:black; font-style: normal; font-size:13px;">@data.Y</span>
</div>
</Template>
</ChartDataLabel>
</ChartMarker>
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code {
public string GetImageForSport(string sport)
{
return sport switch
{
"Tennis" => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRZss9E5QMOinKcG7WvX2cm_yH16wPUVqFivg&s",
"Badminton" => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkpCjFT6TEHGAUdZ9Mrg2-2KicW-BP3FZdSQ&s",
"Cycling" => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9vmk9-8tuXXa-Xrz_7fb1UP3fe5vP85yqvw&s",
"Football" => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcScUzNtl8-v4caUIUlFyjKd6os4XK9bflVv7Q&s",
"Hockey" => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ1vwdtfEG7F6v3I0l-Mh97-lSERy3KrR8rZQ&s",
_ => "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTK48NWCRgZa8dJoOsozHbwa4WtSWI4jBD4mw&s",
};
}
public List<ChartData> PopulationDetails { get; set; } = new List<ChartData>
{
new ChartData { Sports = "Tennis", Boys = 50 },
new ChartData { Sports = "Badminton", Boys = 30 },
new ChartData { Sports = "Cycling", Boys = 37 },
new ChartData { Sports = "Football", Boys = 60 },
new ChartData { Sports = "Hockey", Boys = 15 },
};
public class ChartData
{
public string Sports { get; set; }
public double Boys { get; set; }
}
}
Output
Live Sample for Unique Images for Datalabel
Conclusion
I hope you enjoyed learning how to display unique images for data label 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, Direct-Trac, or feedback portal. We are always happy to assist you!