Articles in this section

How to add an annotation at the mouse-clicked location in Blazor Charts?

This article explains how to add an annotation at the mouse-clicked location in Blazor Charts.

Adding an Annotation to the Clicked Location:

Blazor Charts provides an option to add an annotation to the clicked location on a chart using the ChartMouseClick event.

To add an annotation, you need to set CoordinateUnits to Pixel for the ChartAnnotation property and update the X and Y values using the MouseX and MouseY values from the ChartMouseClick event arguments.

The code example below demonstrates how to add annotations to the clicked position.

Index.cshtml:

@using Syncfusion.Blazor.Charts
<SfChart Title="Sales Analysis">
    <ChartEvents ChartMouseClick="OnMouseEvent"></ChartEvents>
    <ChartAnnotations>
        <ChartAnnotation X="@xCor" Y="@yCor" CoordinateUnits="Units.Pixel">
            <ContentTemplate>
                <div style="color: firebrick; font-size: medium; font-style: italic">@message</div>
            </ContentTemplate>
        </ChartAnnotation>
    </ChartAnnotations>
    <ChartPrimaryXAxis Title="Month" ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
    <ChartPrimaryYAxis Title="Sales Count" LabelFormat="{value}">
    </ChartPrimaryYAxis>
    <ChartTooltipSettings Enable="true"></ChartTooltipSettings>
    <ChartSeriesCollection>
        <ChartSeries Width="2" DataSource="@Sales" XName="Month" YName="SalesValue" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Line">
            <ChartMarker Visible="true">
                <ChartDataLabel Visible="true" Name="Text" />
            </ChartMarker>
        </ChartSeries>
    </ChartSeriesCollection>
</SfChart>
@code {
    public string xCor { get; set; } = "";
    public string yCor { get; set; } = "";
    public string message { get; set; } = "";
    public class SalesInfo
    {
        public string Month { get; set; }
        public double SalesValue { get; set; }
    }
    public List<SalesInfo> Sales = new List<SalesInfo>
    {
        new SalesInfo { Month = "Jan", SalesValue = 15 },
        new SalesInfo { Month = "Feb", SalesValue = 18 },
        new SalesInfo { Month = "Mar", SalesValue = 14 },
        new SalesInfo { Month = "Apr", SalesValue = 12 },
        new SalesInfo { Month = "May", SalesValue = 13 },
        new SalesInfo { Month = "Jun", SalesValue = 19 },
        new SalesInfo { Month = "Jul", SalesValue = 17 }
    };
    public void OnMouseEvent(Syncfusion.Blazor.Charts.ChartMouseEventArgs args)
    {
        var xValue = args.MouseX;
        var yValue = args.MouseY;
        AddAnnotation(xValue.ToString(), yValue.ToString(), "Your Annotation Text");
    }
    private void AddAnnotation(string xValue, string yValue, string annotationText)
    {
        xCor = xValue;
        yCor = yValue;
        message = annotationText;
        StateHasChanged();
    }
}

The following screenshot illustrates the output of the code snippet.

Output:

image.png

Live Sample for Adding Annotation

Conclusion:

We hope you enjoyed learning how to add an annotation at the mouse-clicked location in the Blazor Chart component.

You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.

If you have any questions 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied