I want to put one vertical line to chart, when mouse left button click inside the chart.
And, I want to put vertical line where nearest plotted X value to mouse cursor location in the chart.
No interpolation.
Also, I’m using RubberBandXyZoomModifier ExecuteOn=”MouseLeftButton”
andZoomPanModifier ExecuteOn=”MouseRigthButton”
What is the best Modifier to use?
And how can it set like that?
- Hiroyuki Seki asked 5 years ago
- last edited 5 years ago
- You must login to post comments
To place a vertical line at the mouse point, you need to use a few APIs in SciChart together.
For example:
- The Hit-Test API
- The VerticalLineAnnotation / Annotations API
- Some caveats such as Transforming Coordinates in the viewport
Putting it all together I’ve created an example at our Github repository.
<s:SciChartSurface x:Name="scs" PreviewMouseDown="Scs_OnMouseDown">
<s:SciChartSurface.XAxis>
<s:NumericAxis/>
</s:SciChartSurface.XAxis>
<s:SciChartSurface.YAxis>
<s:NumericAxis AxisAlignment="Left"/>
</s:SciChartSurface.YAxis>
</s:SciChartSurface>
public partial class HitTestToAddAnnotations : Window
{
public HitTestToAddAnnotations()
{
InitializeComponent();
}
private void Scs_OnMouseDown(object sender, MouseButtonEventArgs e)
{
// Get mouse point
var mousePoint = e.GetPosition(scs);
// Transform point to inner viewport
// https://www.scichart.com/questions/question/how-can-i-convert-xyaxis-value-to-chart-surface-coodinate
// https://www.scichart.com/documentation/v5.x/Axis%20APIs%20-%20Convert%20Pixel%20to%20Data%20Coordinates.html
mousePoint = scs.RootGrid.TranslatePoint(mousePoint, scs.ModifierSurface);
// Convert the mousePoint.X to x DataValue using axis
var xDataValue = scs.XAxis.GetDataValue(mousePoint.X);
// Create a vertical line annotation at the mouse point
scs.Annotations.Add(new VerticalLineAnnotation() { X1 = xDataValue});
}
}
Best regards,
Andrew
- Andrew Burnett-Thompson answered 5 years ago
- Thank you for the sample code! I’ll try to modify this and use it.
- Great! Another way you could achieve the same thing is to override the VerticalSliceModifier.OnModifierMouseDown because in there you have access to charts and can do the above code.
- it seems GetDataValue is interpolating the data instead of sticking to the closest data point available. Is there anything better?
- I would suggest looking at the Hit-Test API. This can now give you the closest datapoint OR the exact data-value under the mouse. Link to documentation here: https://www.scichart.com/documentation/win/current/webframe.html#RenderableSeries%20APIs%20-%20Hit%20Testing.html
- You must login to post comments
Please login first to submit.