Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions blazor/stock-chart/legend.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,80 @@ The series [Name](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Charts
```

![Blazor Stock Chart hiding legend item](images/blazor-stock-chart-hidding-legend.png)

## Legend Template

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.

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.

```
@using Syncfusion.Blazor.Charts

<SfStockChart Title="AAPL vs GOOGL Stock Price" SelectionMode="SelectionMode.Series">
<StockChartLegendSettings Visible="true" ToggleVisibility="true" />
<StockChartSeriesCollection>
<StockChartSeries DataSource="@StockDetails" Type="ChartSeriesType.Line" XName="Date" High="High" Low="Low" Open="Open" Close="Close" Name="Apple Stock Price" Fill="red">
<LegendItemTemplate>
<div style="display:flex; align-items:center; gap:10px; padding:4px 0;">
<span style="font-size:18px;">🍎</span>
<div style="display:flex; flex-direction:column; line-height:1.15;">
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:red;">
Apple Stock Price
</span>
<span style="font-size:12px; opacity:0.85;">
AAPL OHLC rendered as Line
</span>
<span style="font-size:12px; opacity:0.75;">
Points: @StockDetails.Count
</span>
</div>
</div>
</LegendItemTemplate>
</StockChartSeries>

<StockChartSeries DataSource="@StockDetails" Type="ChartSeriesType.Spline" XName="Date" High="High" Low="Low" Open="Open" Close="Close" Name="Google Stock Price" Fill="cornflowerblue">
<LegendItemTemplate>
<div style="display:flex; align-items:center; gap:10px; padding:4px 0;">
<span style="font-size:18px;">🔍</span>
<div style="display:flex; flex-direction:column; line-height:1.15;">
<span style="font-family:'Segoe UI'; font-size:14px; font-weight:700; color:cornflowerblue;">
Google Stock Price
</span>
<span style="font-size:12px; opacity:0.85;">
GOOGL OHLC rendered as Spline
</span>
<span style="font-size:12px; opacity:0.75;">
Points: @StockDetails.Count
</span>
</div>
</div>
</LegendItemTemplate>
</StockChartSeries>
</StockChartSeriesCollection>
</SfStockChart>

@code {
public class ChartData
{
public DateTime Date { get; set; }
public double Open { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Volume { get; set; }
}

public List<ChartData> StockDetails = new List<ChartData>
{
new ChartData { Date = new DateTime(2012, 04, 02), Open = 85.9757, High = 90.6657, Low = 85.7685, Close = 90.5257, Volume = 660187068 },
new ChartData { Date = new DateTime(2012, 04, 09), Open = 89.4471, High = 92, Low = 86.2157, Close = 86.4614, Volume = 912634864 },
new ChartData { Date = new DateTime(2012, 04, 16), Open = 87.1514, High = 88.6071, Low = 81.4885, Close = 81.8543, Volume = 1221746066 },
new ChartData { Date = new DateTime(2012, 04, 23), Open = 81.5157, High = 88.2857, Low = 79.2857, Close = 86.1428, Volume = 965935749 },
new ChartData { Date = new DateTime(2012, 04, 30), Open = 85.4, High = 85.4857, Low = 80.7385, Close = 80.75, Volume = 615249365 }
};
}
```
![Blazor Stock Chart legend template](images/blazor-stock-chart-legend-template.png)