WinUI | ComponentOne
Controls / FlexChart / End-user Interaction / Hit Test
In This Topic
    Hit Test
    In This Topic

    Hit test in charts refers to the ability to fetch information about the chart object under the mouse and lets you create interactive applications.

    For instance, this feature can be used for displaying a special custom tooltip when mouse hovers over a particular data point. Similarly, you can even use the hit test information for drilling down the chart data, to set alerts and enable other user interaction functionalities.

    FlexChart provides HitTest method in the FlexChart class to fetch information about the underlying chart objects. This method has following two overloads and returns an object of the HitTestInfo class which provides information such as chart element under pointer, distance from the closest data point, index of the nearest data point etc.

    To use this method to fetch information, you need to subscribe to a mouse event on which you want to fetch the information and then invoke the HitTest method in its event handler. You can then use this information in the required manner. In the example shown in this topic, we have fetched the information of chart objects on mouse movement and have displayed it in the information panel below the chart. For displaying fetched data in the information panel, we have added two TextBlock controls in the XAML code named 'info1' and 'info2'. This sample uses the same datasource as used in Quick Start.

    CS
    Copy Code
    public HitTest()
    {
        this.InitializeComponent();
        flexChart.ItemsSource = DataService.GetProductRevenue();
        this.PointerMoved += HitTest_PointerMoved;
    }
    
    private void HitTest_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        var hitInfo = flexChart.HitTest(e.GetCurrentPoint(flexChart).Position);
        var result1 = new StringBuilder();
        if (hitInfo != null)
        {
            result1.AppendLine(string.Format("Chart element: {0}", hitInfo.ChartElement));
            if (hitInfo.Series != null)
                result1.AppendLine(string.Format("Series name: {0}", hitInfo.Series.Name));
            if (hitInfo.PointIndex >= 0)
                result1.AppendLine(string.Format("Point index= {0:0}", hitInfo.PointIndex));
            info1.Text = result1.ToString();
    
            var result2 = new StringBuilder();
            if (hitInfo.Distance >= 0)
                result2.AppendLine(string.Format("Distance= {0:0}", hitInfo.Distance));
            if (hitInfo.X != null)
                result2.AppendLine(string.Format("X= {0}", hitInfo.X.ToString()));
            if (hitInfo.Y != null)
                result2.AppendLine(string.Format("Y= {0}", hitInfo.Y.ToString()));
            info2.Text = result2.ToString();
        }
    }