SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and iOS Chart & Android Chart Components

Answered
0
0

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”
    and

  • ZoomPanModifier ExecuteOn=”MouseRigthButton”

What is the best Modifier to use?
And how can it set like that?

Version
v5.1.1.11473
  • You must to post comments
Best Answer
1
1

To place a vertical line at the mouse point, you need to use a few APIs in SciChart together.

For example:

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

  • Hiroyuki Seki
    Thank you for the sample code! I’ll try to modify this and use it.
  • Andrew
    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.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.