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.
2 series, 4 categories.Monthly revenueTop labels with background and border020406080100120140JanFebMarApr50k72k106k129k84k79k99k93kTokyoNew York

Interact with a chart point or data label.

<SvgColumnChart TItem="MonthlySales"
                Items="@sales"
                Options="@options"
                Clicked="@OnPointClicked">
    <SvgChartTitle Title='@("Monthly revenue")' Subtitle='@("Top labels with background and border")' />
    <SvgChartLegend Position="SvgChartLegendPosition.Bottom" />
    <SvgChartTooltip Enabled />
    <SvgChartDataLabels Visible
                        Position="SvgChartDataLabelPosition.Top"
                        Formatter="@FormatRevenueLabel"
                        BackgroundColor='@((Color)"#ffffff")'
                        Border="@( new SvgChartBorderOptions { Color = Color.Primary, Width = 1, Radius = 4 } )"
                        Clicked="@OnDataLabelClicked"
                        Hovered="@OnDataLabelHovered" />
    <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 configuring SvgChartOptions.DataLabels.
1 series, 4 categories.Traffic sourcesCentered labels on radial slices42%27%18%13%SearchDirectSocialReferral

Click a slice or label.

<SvgDoughnutChart TItem="object"
                  Data="@data"
                  Options="@options"
                  Clicked="@OnPointClicked">
    <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 TypeDefault
BackgroundColor

Defines the label background color.

Colornull
Border

Defines the label border options.

SvgChartBorderOptionsnull
Font

Defines data label font options.

SvgChartFontOptionsnull
Interactive

Defines whether data labels react to pointer and keyboard interactions.

booltrue
Offset

Defines the label offset from the point anchor.

double6
Opacity

Defines the label opacity.

double1
Padding

Defines the label padding.

SvgChartSpacingnull
Position

Defines the data label position.

Possible values:Auto, Center, Top, End, Bottom, Start

SvgChartDataLabelPositionSvgChartDataLabelPosition.Auto
Visible

Defines whether data labels are visible.

booltrue

SvgChartDataLabelsOptions

Parameter Description TypeDefault
BackgroundColor

Defines the label background color.

Colornull
Border

Defines the label border options.

SvgChartBorderOptionsnew() { Radius = 3, }
Font

Defines data label font options.

SvgChartFontOptionsnew() { Size = 11, Weight = "600", }
Interactive

Defines whether data labels react to pointer and keyboard interactions.

booltrue
Offset

Defines the label offset from the point anchor.

double6
Opacity

Defines the label opacity.

double1
Padding

Defines the label padding.

SvgChartSpacingnew() { Top = 3, End = 5, Bottom = 3, Start = 5, }
Position

Defines the data label position.

Possible values:Auto, Center, Top, End, Bottom, Start

SvgChartDataLabelPositionSvgChartDataLabelPosition.Auto
Visible

Defines whether data labels are visible.

boolfalse

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 DescriptionReturnParameters
Render Renders the plugin SVG content into the chart. voidSvgChartPluginRenderContext context, RenderTreeBuilder builder, int sequence
On this page