How to render a stripline at the mouse-clicked location in Blazor Charts?
This article explains how to render a stripline at the mouse-clicked location in Blazor Charts.
Render Striplines on Chart Click
Blazor Charts provides an option to render an axis stripline when clicking inside the chart area using the ChartMouseClick event.
This can be achieved by using the OnCrosshairMove event to obtain the Y value of the current mouse cursor location and then assigning this Y value to the stripline start value in the ChartMouseClick event.
The code example below demonstrates how to render a stripline at the mouse-clicked location in Blazor Charts.
Index.razor:
@using Syncfusion.Blazor.Charts
<SfChart>
<ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime">
<ChartAxisCrosshairTooltip Enable="true"></ChartAxisCrosshairTooltip>
</ChartPrimaryXAxis>
<ChartCrosshairSettings Enable="true"></ChartCrosshairSettings>
<ChartEvents ChartMouseClick="OnMouseEvent" OnCrosshairMove="CrosshairMove"></ChartEvents>
<ChartPrimaryYAxis>
@if (Start != 0)
{
<ChartStriplines>
<ChartStripline Start="@Start" Size="1" SizeType="SizeType.Pixel" Color="red" />
</ChartStriplines>
}
<ChartAxisCrosshairTooltip Enable="true"></ChartAxisCrosshairTooltip>
</ChartPrimaryYAxis>
<ChartSeriesCollection>
<ChartSeries DataSource="@SalesDetails" XName="X" YName="Y" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Line">
</ChartSeries>
</ChartSeriesCollection>
</SfChart>
@code {
public class ChartData
{
public DateTime X { get; set; }
public double Y { get; set; }
}
public double Start { get; set; } = 0;
public double CurrentStartValue { get; set; } = 0;
public List<ChartData> SalesDetails = new List<ChartData>
{
new ChartData { X = new DateTime(2000, 01, 01), Y = 4 },
new ChartData { X = new DateTime(2001, 01, 01), Y = 3.0 },
new ChartData { X = new DateTime(2002, 01, 01), Y = 3.8 },
new ChartData { X = new DateTime(2003, 01, 01), Y = 3.4 },
new ChartData { X = new DateTime(2004, 01, 01), Y = 3.2 },
new ChartData { X = new DateTime(2005, 01, 01), Y = 3.9 },
};
public void CrosshairMove(CrosshairMoveEventArgs args)
{
CurrentStartValue = (double)args.AxisInfo[0].Value;
}
public void OnMouseEvent(ChartMouseEventArgs args)
{
Start = CurrentStartValue;
StateHasChanged();
}
}
The following screenshot illustrates the output of the code snippet.
Output:
Live Sample for Stripline on Chart Click
Conclusion:
We hope you enjoyed learning how to render a stripline on clicking 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!