Articles in this section
Category / Section

How to add an editable textbox at the mouse-clicked position in Blazor Charts?

3 mins read

This article explains how to add an editable textbox at the mouse-clicked position.

Adding an Editable Textbox on Clicked Position

Blazor Charts offers a convenient option to include an editable textbox at a specific position within a chart area using the ChartMouseClick event.

To achieve this, you will need to utilize the TextBox component within ChartAnnotations, which will be displayed when clicked. This can be accomplished by setting CoordinateUnits to “Pixel” and updating the X and Y values of the annotation using the ChartMouseClick event’s MouseX and MouseY values.

The following code example demonstrates how to add an editable textbox at the clicked position.

Index.razor

@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Inputs
<SfChart Title="Sales Analysis">
   <ChartEvents ChartMouseClick="OnMouseEvent"></ChartEvents>
   <ChartAnnotations>
       <ChartAnnotation X="@xCor" Y="@yCor" CoordinateUnits="Units.Pixel">
           <ContentTemplate>
               <SfTextBox Placeholder='Enter Text'></SfTextBox>
           </ContentTemplate>
       </ChartAnnotation>
   </ChartAnnotations>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
   <ChartPrimaryYAxis 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 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(ChartMouseEventArgs args)
   {
       xCor = args.MouseX.ToString();
       yCor = Math.Round(args.MouseY).ToString();
       StateHasChanged();
   }   
}

In this code snippet, we utilize Chart and TextBox component from Syncfusion Blazor Charts and Inputs libraries, respectively. The ChartMouseClick event updates the X and Y values based on the mouse click position, which in turn positions the editable textbox accordingly in the chart area.
The following screenshot illustrates the output of the code snippet.

Output

image.png

Live Sample for Adding Editable Textbox

Conclusion

I hope you enjoyed learning how to add an editable textbox at the mouse-clicked position in Blazor Chart Component.

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!

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