Blazorise SVG Chart Data Labels
Render labels around chart values and handle label interactions.
SVG chart data labels are configured declaratively with SvgChartDataLabels or through SvgChartOptions.DataLabels. Use the formatter callback to customize label text per point.
Examples
Value labels
Render formatted labels above column values and react to label events.Interact with a chart point or data label.
<SvgColumnChart TItem="MonthlySales" Items="" Options="" Clicked=""> <SvgChartTitle Title='@("Monthly revenue")' Subtitle='@("Top labels with background and border")' /> <SvgChartLegend Position="SvgChartLegendPosition.Bottom" /> <SvgChartTooltip Enabled /> <SvgChartDataLabels Visible Position="SvgChartDataLabelPosition.Top" Formatter="" BackgroundColor='@((Color)"#ffffff")' Border="@( new SvgChartBorderOptions { Color = Color.Primary, Width = 1, Radius = 4 } )" Clicked="" Hovered="" /> <SvgChartCategoryAxis Value="@( item => item.Month )" /> <SvgChartValueAxis BeginAtZero TickCount="6" /> <SvgColumnSeries Name="Tokyo" Value="@( item => item.Tokyo )" Color="Color.Primary" BorderRadius="4" /> <SvgColumnSeries Name="New York" Value="@( item => item.NewYork )" Color="Color.Info" BorderRadius="4" /> </SvgColumnChart> <Paragraph Margin="Margin.Is2.FromTop.Is0.FromBottom">@lastEvent</Paragraph>
@code { private string lastEvent = "Interact with a chart point or data label."; private readonly SvgChartOptions options = new() { Height = 360, Legend = new() { Position = SvgChartLegendPosition.Bottom }, }; private readonly List<MonthlySales> sales = [ new() { Month = "Jan", Tokyo = 49.9, NewYork = 83.6 }, new() { Month = "Feb", Tokyo = 71.5, NewYork = 78.8 }, new() { Month = "Mar", Tokyo = 106.4, NewYork = 98.5 }, new() { Month = "Apr", Tokyo = 129.2, NewYork = 93.4 }, ]; private Task OnPointClicked( SvgChartPointEventArgs eventArgs ) { lastEvent = $"Clicked point {eventArgs.SeriesName} / {eventArgs.Category}: {eventArgs.Value}"; return Task.CompletedTask; } private Task OnDataLabelClicked( SvgChartPointEventArgs eventArgs ) { lastEvent = $"Clicked label {eventArgs.SeriesName} / {eventArgs.Category}: {eventArgs.Value}"; return Task.CompletedTask; } private Task OnDataLabelHovered( SvgChartPointEventArgs eventArgs ) { lastEvent = $"Hovered label {eventArgs.SeriesName} / {eventArgs.Category}: {eventArgs.Value}"; return Task.CompletedTask; } private string FormatRevenueLabel( SvgChartDataLabelContext context ) { return $"{Convert.ToDouble( context.Value, System.Globalization.CultureInfo.InvariantCulture ).ToString( "0", System.Globalization.CultureInfo.InvariantCulture )}k"; } private sealed class MonthlySales { public string Month { get; set; } public double Tokyo { get; set; } public double NewYork { get; set; } } }
Radial labels
Render labels inside doughnut slices by configuringSvgChartOptions.DataLabels.
Click a slice or label.
<SvgDoughnutChart TItem="object" Data="" Options="" Clicked=""> <SvgChartTitle Title='@("Traffic sources")' Subtitle='@("Centered labels on radial slices")' /> <SvgChartTooltip Enabled /> </SvgDoughnutChart> <Paragraph Margin="Margin.Is2.FromTop.Is0.FromBottom">@lastEvent</Paragraph>
@code { private string lastEvent = "Click a slice or label."; private readonly SvgChartOptions options = new() { Height = 340, Legend = new() { Position = SvgChartLegendPosition.Bottom }, DataLabels = new() { Visible = true, Position = SvgChartDataLabelPosition.Center, Formatter = FormatShareLabel, Font = new() { Color = Color.Light, Weight = "700", }, }, }; private readonly SvgChartData<double?> data = new() { Labels = ["Search", "Direct", "Social", "Referral"], Series = [ new() { Name = "Share", Values = [42, 27, 18, 13], }, ], }; private Task OnPointClicked( SvgChartPointEventArgs eventArgs ) { lastEvent = $"Clicked {eventArgs.Category}: {eventArgs.Value}%"; return Task.CompletedTask; } private static string FormatShareLabel( SvgChartDataLabelContext context ) { return $"{Convert.ToDouble( context.Value, System.Globalization.CultureInfo.InvariantCulture ):0}%"; } }
API
Parameters
SvgChartDataLabels
| Parameter | Description | Type | Default |
|---|---|---|---|
BackgroundColor |
Defines the label background color. |
Color | null |
Border |
Defines the label border options. |
SvgChartBorderOptions | null |
Font |
Defines data label font options. |
SvgChartFontOptions | null |
Interactive |
Defines whether data labels react to pointer and keyboard interactions. |
bool | true |
Offset |
Defines the label offset from the point anchor. |
double | 6 |
Opacity |
Defines the label opacity. |
double | 1 |
Padding |
Defines the label padding. |
SvgChartSpacing | null |
Position |
Defines the data label position. Possible values: |
SvgChartDataLabelPosition | SvgChartDataLabelPosition.Auto |
Visible |
Defines whether data labels are visible. |
bool | true |
SvgChartDataLabelsOptions
| Parameter | Description | Type | Default |
|---|---|---|---|
BackgroundColor |
Defines the label background color. |
Color | null |
Border |
Defines the label border options. |
SvgChartBorderOptions | new()
{
Radius = 3,
} |
Font |
Defines data label font options. |
SvgChartFontOptions | new()
{
Size = 11,
Weight = "600",
} |
Interactive |
Defines whether data labels react to pointer and keyboard interactions. |
bool | true |
Offset |
Defines the label offset from the point anchor. |
double | 6 |
Opacity |
Defines the label opacity. |
double | 1 |
Padding |
Defines the label padding. |
SvgChartSpacing | new()
{
Top = 3,
End = 5,
Bottom = 3,
Start = 5,
} |
Position |
Defines the data label position. Possible values: |
SvgChartDataLabelPosition | SvgChartDataLabelPosition.Auto |
Visible |
Defines whether data labels are visible. |
bool | false |
Events
SvgChartDataLabels
| Event | Description | Type |
|---|---|---|
Clicked |
Occurs when a data label is clicked. |
EventCallback<SvgChartPointEventArgs> |
Formatter |
Defines a callback used to format label text. |
Func<SvgChartDataLabelContext, string> |
Hovered |
Occurs when a data label is hovered. |
EventCallback<SvgChartPointEventArgs> |
SvgChartDataLabelsOptions
| Event | Description | Type |
|---|---|---|
Formatter |
Defines a callback used to format label text. |
Func<SvgChartDataLabelContext, string> |
Methods
SvgChartDataLabels
| Method | Description | Return | Parameters |
|---|---|---|---|
Render |
Renders the plugin SVG content into the chart. | void | SvgChartPluginRenderContext context, RenderTreeBuilder builder, int sequence |