Articles in this section
Category / Section

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 to each data label based on the X-value of the chart series data point. Within the Template, you can set the src attribute of the image tag by retrieving the corresponding URL using the X-value.

The following code example demonstrates how to display sport-specific images along with the respective number of boys participating, within 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

image.png

Live Sample for unique images for data labels

Conclusion:
We 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 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, Direct-Trac, 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