Articles in this section
Category / Section

How to find nearest data point in series using touch point in UWP Chart?

This article explains how to find the nearest data point in a UWP Chart from any touch point within the chart area. This can be accomplished by using the PointerMoved event of the chart and the FindNearestChartPoint method available in chart series. 

Step 1: Attach the PointerMoved event to the SfChart control to capture mouse or touch movements within the chart area.

<chart:SfChart PointerMoved="Chart_PointerMoved">
. . .
</chart:SfChart>

Step 2: Implement the PointerMoved event handler to retrieve the touch position relative to the chart's adorning canvas, then use the FindNearestChartPoint method of the chart series to determine the closest data point to that position.

private void Chart_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    FieldInfo field = typeof(ChartBase).GetField("AdorningCanvas", BindingFlags.NonPublic | BindingFlags.Instance);
    UIElement canvas = field.GetValue(Chart) as UIElement;
    var pointer = e.GetCurrentPoint(canvas);
    double xvalue = pointer.Position.X;
    double yvalue = pointer.Position.Y;
    double stackvalue = double.NaN;
    Point point = new Point(xvalue, yvalue);

    Chart.Series[0].FindNearestChartPoint(point, out xvalue, out yvalue, out stackvalue);

    textblock.Text = "Nearest X Point is " + xvalue.ToString();
    textblock1.Text = "Nearest Y Point is " + yvalue.ToString();
}

Output

Nearest points displayed based on the pointer movement.

Conclusion

I hope you enjoyed learning about how to find nearest data point in series using touch point from chart area in UWP Chart.

You can refer to our UWP Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our UWP Chart documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 UWP Charts and other UWP components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, 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)
Access denied
Access denied