Skip to content

Commit 5f6d974

Browse files
Merge branch 'development' into 998039-acc-legend
2 parents 58916ff + fe5111b commit 5f6d974

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-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
56.6 KB
Loading

blazor/stock-chart/legend.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,94 @@ The series [Name](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts
561561
```
562562

563563
![Blazor Stock Chart hiding legend item](images/blazor-stock-chart-hidding-legend.png)
564+
565+
## Legend Template
566+
567+
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.
568+
569+
To use, add a `LegendItemTemplate` inside any [StockChartSeries](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts.StockChartSeries.html#Syncfusion_Blazor_Charts_StockChartSeries) 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.StockChartLegendSettings.html#Syncfusion_Blazor_Charts_StockChartLegendSettings_ToggleVisibility) is set to false. Templates work with all legend positions, alignments, and paging.
570+
571+
```
572+
@using Syncfusion.Blazor.Charts
573+
574+
@* Initialize the stock chart component and configure its essential features *@
575+
<SfStockChart Title="AAPL vs GOOGL Stock Price"
576+
SelectionMode="SelectionMode.Series">
577+
578+
@* Display the legend and allow toggling series visibility on interaction *@
579+
<StockChartLegendSettings Visible="true" ToggleVisibility="true" />
580+
581+
<StockChartSeriesCollection>
582+
583+
@* Define the first series using OHLC fields and render it as a line to show trend *@
584+
<StockChartSeries DataSource="@StockDetails" Type="ChartSeriesType.Line" XName="Date" High="High" Low="Low" Open="Open" Close="Close" Name="Apple Stock Price" Fill="@AppleColor">
585+
@* This legend item contains an emoji indicator, a color swatch, a descriptive label, and a dynamic point count *@
586+
<LegendItemTemplate>
587+
<div style="display:flex; align-items:center; gap:10px; padding:6px 0;">
588+
<span style="font-size:18px;">🍎</span>
589+
<div style="display:flex; flex-direction:column; line-height:1.2;">
590+
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:@AppleColor;">
591+
Apple Stock Price
592+
</span>
593+
<span style="font-size:12px; opacity:0.85;">
594+
AAPL OHLC rendered as Line
595+
</span>
596+
<span style="font-size:12px; opacity:0.75;">
597+
Points: @StockDetails.Count
598+
</span>
599+
</div>
600+
</div>
601+
</LegendItemTemplate>
602+
</StockChartSeries>
603+
604+
@* Define the second series using the same OHLC fields and render it as a spline for a smoothed trend *@
605+
<StockChartSeries DataSource="@StockDetails" Type="ChartSeriesType.Spline" XName="Date" High="High" Low="Low" Open="Open" Close="Close" Name="Google Stock Price" Fill="@GoogleColor">
606+
@* This legend item presents an emoji indicator, a color swatch, a descriptive label, and a dynamic point count *@
607+
<LegendItemTemplate>
608+
<div style="display:flex; align-items:center; gap:10px; padding:6px 0;">
609+
<span style="font-size:18px;">🔍</span>
610+
<div style="display:flex; flex-direction:column; line-height:1.2;">
611+
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:@GoogleColor;">
612+
Google Stock Price
613+
</span>
614+
<span style="font-size:12px; opacity:0.85;">
615+
GOOGL OHLC rendered as Spline
616+
</span>
617+
<span style="font-size:12px; opacity:0.75;">
618+
Points: @StockDetails.Count
619+
</span>
620+
</div>
621+
</div>
622+
</LegendItemTemplate>
623+
</StockChartSeries>
624+
625+
</StockChartSeriesCollection>
626+
</SfStockChart>
627+
628+
@code {
629+
private string AppleColor => "#16a34a";
630+
private string GoogleColor => "#2563eb";
631+
632+
public class ChartData
633+
{
634+
public DateTime Date { get; set; }
635+
public double Open { get; set; }
636+
public double Low { get; set; }
637+
public double Close { get; set; }
638+
public double High { get; set; }
639+
public double Volume { get; set; }
640+
}
641+
642+
public List<ChartData> StockDetails = new List<ChartData>
643+
{
644+
new ChartData { Date = new DateTime(2012, 04, 02), Open = 85.9757, High = 90.6657, Low = 85.7685, Close = 90.5257, Volume = 660187068 },
645+
new ChartData { Date = new DateTime(2012, 04, 09), Open = 89.4471, High = 92, Low = 86.2157, Close = 86.4614, Volume = 912634864 },
646+
new ChartData { Date = new DateTime(2012, 04, 16), Open = 87.1514, High = 88.6071, Low = 81.4885, Close = 81.8543, Volume = 1221746066 },
647+
new ChartData { Date = new DateTime(2012, 04, 23), Open = 81.5157, High = 88.2857, Low = 79.2857, Close = 86.1428, Volume = 965935749 },
648+
new ChartData { Date = new DateTime(2012, 04, 30), Open = 85.4, High = 85.4857, Low = 80.7385, Close = 80.75, Volume = 615249365 }
649+
};
650+
}
651+
```
652+
![Blazor Stock Chart legend template](images/blazor-stock-chart-legend-template.png)
653+
654+

0 commit comments

Comments
 (0)