Skip to content

Commit 7cf63ce

Browse files
Merge branch 'development' into 998039-stock-legend
2 parents 99aa591 + 5f0683d commit 7cf63ce

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
43.1 KB
Loading

blazor/chart/legend.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,101 @@ The series [Name](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts
678678

679679
![Hiding Legend Item in Blazor Column Chart](images/legend/blazor-column-chart-hide-legend-item.png)
680680

681+
## Legend Template
682+
683+
Legend templates allow you to replace default legend icons and text with custom HTML or Blazor markup for each series. This enables branded styles, richer content (icons, multi-line text, badges), improved readability, and localization.
684+
685+
To use, add a `LegendItemTemplate` inside any [ChartSeries](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts.ChartSeries.html) you want to customize. The rendered content becomes the legend item and can be styled with CSS. Legend interactions (click to toggle series) remain unless [ToggleVisibility](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts.ChartLegendSettings.html#Syncfusion_Blazor_Charts_ChartLegendSettings_ToggleVisibility) is set to false. Templates work with all legend positions, alignments, and paging.
686+
687+
```
688+
@using Syncfusion.Blazor.Charts
689+
690+
@* Initialize the chart and configure essential features *@
691+
<SfChart Title="Olympic Medals">
692+
@* Set category axis for country names *@
693+
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category" />
694+
695+
@* Show the legend and enable series toggle *@
696+
<ChartLegendSettings Visible="true" />
697+
698+
<ChartSeriesCollection>
699+
@* Gold column series *@
700+
<ChartSeries DataSource="@MedalDetails" Name="Gold" XName="Country" Width="2" Opacity="1" YName="Gold" Fill="#FFD700" Type="ChartSeriesType.Column">
701+
@* Custom legend with icon, label, and total count *@
702+
<LegendItemTemplate>
703+
<div style="display:flex; align-items:center; gap:10px; padding:4px 0;">
704+
<span style="font-size:18px;">🥇</span>
705+
<div style="display:flex; flex-direction:column; line-height:1.15;">
706+
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:#FFD700;">Gold</span>
707+
<span style="font-size:12px; opacity:0.85;">Awarded for first place finishes</span>
708+
<span style="font-size:12px; opacity:0.75;">Total: @GoldTotal</span>
709+
</div>
710+
</div>
711+
</LegendItemTemplate>
712+
</ChartSeries>
713+
714+
@* Silver column series *@
715+
<ChartSeries DataSource="@MedalDetails" Name="Silver" XName="Country" Width="2" Opacity="1" YName="Silver" Fill="#898989" Type="ChartSeriesType.Column">
716+
@* Custom legend with icon, label, and total count *@
717+
<LegendItemTemplate>
718+
<div style="display:flex; align-items:center; gap:10px; padding:4px 0;">
719+
<span style="font-size:18px;">🥈</span>
720+
<div style="display:flex; flex-direction:column; line-height:1.15;">
721+
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:#898989;">Silver</span>
722+
<span style="font-size:12px; opacity:0.85;">Awarded for second place finishes</span>
723+
<span style="font-size:12px; opacity:0.75;">Total: @SilverTotal</span>
724+
</div>
725+
</div>
726+
</LegendItemTemplate>
727+
</ChartSeries>
728+
729+
@* Bronze column series *@
730+
<ChartSeries DataSource="@MedalDetails" Name="Bronze" XName="Country" Width="2" Opacity="1" YName="Bronze" Fill="#CD7F32" Type="ChartSeriesType.Column">
731+
@* Custom legend with icon, label, and total count *@
732+
<LegendItemTemplate>
733+
<div style="display:flex; align-items:center; gap:10px; padding:4px 0;">
734+
<span style="font-size:18px;">🥉</span>
735+
<div style="display:flex; flex-direction:column; line-height:1.15;">
736+
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:#CD7F32;">Bronze</span>
737+
<span style="font-size:12px; opacity:0.85;">Awarded for third place finishes</span>
738+
<span style="font-size:12px; opacity:0.75;">Total: @BronzeTotal</span>
739+
</div>
740+
</div>
741+
</LegendItemTemplate>
742+
</ChartSeries>
743+
</ChartSeriesCollection>
744+
745+
</SfChart>
746+
747+
@code {
748+
749+
public class ChartData
750+
{
751+
public string Country { get; set; }
752+
public double Gold { get; set; }
753+
public double Silver { get; set; }
754+
public double Bronze { get; set; }
755+
}
756+
757+
public List<ChartData> MedalDetails = new()
758+
{
759+
new ChartData{ Country= "USA", Gold=50, Silver=70, Bronze=45 },
760+
new ChartData{ Country= "China", Gold=40, Silver=60, Bronze=55 },
761+
new ChartData{ Country= "Japan", Gold=70, Silver=60, Bronze=50 },
762+
new ChartData{ Country= "Australia",Gold=60, Silver=56, Bronze=40 },
763+
new ChartData{ Country= "France", Gold=50, Silver=45, Bronze=35 },
764+
new ChartData{ Country= "Germany", Gold=40, Silver=30, Bronze=22 },
765+
new ChartData{ Country= "Italy", Gold=40, Silver=35, Bronze=37 },
766+
new ChartData{ Country= "Sweden", Gold=30, Silver=25, Bronze=27 }
767+
};
768+
769+
public int GoldTotal => (int)MedalDetails.Sum(m => m.Gold);
770+
public int SilverTotal => (int)MedalDetails.Sum(m => m.Silver);
771+
public int BronzeTotal => (int)MedalDetails.Sum(m => m.Bronze);
772+
}
773+
```
774+
![Legend Template in Blazor Column Chart](images/legend/blazor-column-chart-legend-template.png)
775+
681776
N> Refer to our [Blazor Charts](https://www.syncfusion.com/blazor-components/blazor-charts) feature tour page for its groundbreaking feature representations and also explore our [Blazor Chart Example](https://blazor.syncfusion.com/demos/chart/line?theme=bootstrap5) to know various chart types and how to represent time-dependent data, showing trends at equal intervals.
682777

683778
## See also

0 commit comments

Comments
 (0)