Skip to content

Commit fe5111b

Browse files
Merge pull request #7253 from syncfusion-content/998039-stock-legend
998039: Added UG Documentation for Stock chart legend template.
2 parents 5f0683d + 7cf63ce commit fe5111b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
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)