How to add an editable textbox at the mouse-clicked position in Blazor Charts?
This article explains how to add an editable textbox at the mouse-clicked position in Blazor Charts.
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 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 the Chart and TextBox components 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:
Live Sample for Adding Editable Textbox
Conclusion:
We hope you enjoyed learning how to add an editable textbox at the mouse-clicked position 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!