How to render a custom legend using annotations in Blazor Charts?
This article explains how to render a custom legend using annotations in Blazor Charts.
Render a Custom Legend Using Annotations
Blazor Charts provides an option to render a custom legend using annotations. This can be achieved by utilizing the ChartAnnotation to render the custom legend at the desired pixel location, using the chart’s Loaded and OnAxisActualRangeCalculated events.
In this code snippet, a custom legend is added to a scatter chart using ChartAnnotation component. The ChartAnnotation is rendered with svg elements to display a custom legend, which is positioned dynamically based on the chart’s dimensions. This positioning is managed using the Loaded and OnAxisActualRangeCalculated events. The RangeCalculated method captures the chart’s width, while the ChartLoaded method sets the annotation’s position to the top-right corner of the chart.
Index.razor
@using Syncfusion.Blazor.Charts
<SfChart DataSource="@MedalDetails">
<ChartEvents Loaded="ChartLoaded" OnAxisActualRangeCalculated="RangeCalculated"></ChartEvents>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
<ChartAnnotations>
<ChartAnnotation X="@AnnotationX" Y="@AnnotationY" Region="Regions.Series" CoordinateUnits="Units.Pixel">
<ContentTemplate>
<div>
<svg height="20" width="100" xmlns="http://www.w3.org/2000/svg">
<circle r="5" cx="5" cy="5" fill="red" />
<text x="15" y="10" fill="black">Series 1</text>
</svg>
<svg height="20" width="100" xmlns="http://www.w3.org/2000/svg">
<circle r="5" cx="5" cy="5" fill="blue" />
<text x="15" y="10" fill="black">Series 2</text>
</svg>
</div>
</ContentTemplate>
</ChartAnnotation>
</ChartAnnotations>
<ChartSeriesCollection>
<ChartSeries XName="X" Fill="red" YName="YValue" Type="ChartSeriesType.Scatter">
<ChartMarker Visible=true Height="10" Width="10"></ChartMarker>
</ChartSeries>
<ChartSeries XName="X" Fill="blue" YName="YValue1" Type="ChartSeriesType.Scatter">
<ChartMarker Shape="ChartShape.Circle" Visible=true Height="10" Width="10"></ChartMarker>
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code {
public double AnnotationX { get; set; }
public string AnnotationY { get; set; }
public double Width { get; set; }
public void RangeCalculated(AxisRangeCalculatedEventArgs args)
{
Width = args.Bounds.Width;
}
public void ChartLoaded(LoadedEventArgs args)
{
// To place annotation in top right corner
AnnotationX = Width - 10;
AnnotationY = 30.ToString();
StateHasChanged();
}
public class ChartData
{
public string X { get; set; }
public double YValue { get; set; }
public double YValue1 { get; set; }
}
public List<ChartData> MedalDetails = new List<ChartData>
{
new ChartData { X= "USA", YValue= 46, YValue1=56 },
new ChartData { X= "GBR", YValue= 27, YValue1=17 },
new ChartData { X= "CHN", YValue= 26, YValue1=36 },
new ChartData { X= "UK", YValue= 56, YValue1=16 },
new ChartData { X= "AUS", YValue= 12, YValue1=46 },
new ChartData { X= "IND", YValue= 26, YValue1=16 },
new ChartData { X= "DEN", YValue= 26, YValue1=12 },
new ChartData { X= "MEX", YValue= 34, YValue1=32},
};
}
The following screenshot illustrates the output of the code snippet.
Live Sample for Rendering a Custom Legend Using Annotations
Conclusion
I hope you enjoyed learning how to render a custom legend using annotations in Blazor Charts.
You can refer to our Blazor Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!