Previous topicNext topic
Help > 开发指南 > 窗体开发 > 控件参考 > 控件参考 > 图表 > SmFlexChart > 方法 >
HitTest

HitTest

图表中的命中测试是指在鼠标下获取有关图表对象的信息的能力,并允许您创建交互式应用程序。例如,此功能可用于在鼠标悬停在特定数据点上时显示特殊的自定义工具提示。同样,您甚至可以使用命中测试信息向下钻取图表数据,以设置警报并启用其他用户交互功能。


FlexChart 通过 FlexChart 类提供 HitTest 方法,以获取有关基础图表对象的信息。此方法具有以下两个重载,并返回 HitTestInfo 类的一个对象,该对象提供诸如指针下的图表元素、与最近数据点的距离、最近数据点的索引等信息。

HitTest (Point)
HitTest(Point,MeasureOption,Int32)
若要使用此方法提取信息,需要订阅要在其上提取信息的鼠标事件,然后在其事件处理程序中调用 HitTest 方法。然后,您可以按所需的方式使用此信息。在本主题所示的示例中,我们获取了鼠标移动时图表对象的信息,并将其显示在图表下方的信息面板中。

Vb.Net
 
Private Sub FlexChart1_MouseMove(sender As Object, e As MouseEventArgs)
    ' Show information about chart element under mouse cursor
    Dim hitInfo As HitTestInfo = flexChart1.HitTest(e.Location)
    Dim result1 As StringBuilder = New StringBuilder()
    If hitInfo IsNot Nothing Then
        result1.AppendLine(String.Format("Chart element: {0}", hitInfo.ChartElement))
        If hitInfo.Series IsNot Nothing Then
            result1.AppendLine(String.Format("Series name: {0}", hitInfo.Series.Name))
        End If
        If hitInfo.PointIndex >= 0 Then
            result1.AppendLine(String.Format("Point index= {0:0}", hitInfo.PointIndex))
        End If
        _lInfo1.Text = result1.ToString()

        Dim result2 As StringBuilder = New StringBuilder()
        If hitInfo.Distance > 0 Then
            result2.AppendLine(String.Format("Distance= {0:0}", hitInfo.Distance))
        End If
        If hitInfo.X IsNot Nothing Then
            result2.AppendLine(String.Format("X= {0:0}", hitInfo.X))
        End If
        If hitInfo.Y IsNot Nothing Then
            result2.AppendLine(String.Format("Y= {0:p}", hitInfo.Y))
        End If
        _lInfo2.Text = result2.ToString()
    End If
End Sub

C#
 
private void FlexChart1_MouseMove(object sender, MouseEventArgs e)
{
    // Show information about chart element under mouse cursor
    var hitInfo = flexChart1.HitTest(e.Location);
    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));
        _lInfo1.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:0}", hitInfo.X));
        if (hitInfo.Y != null)
            result2.AppendLine(string.Format("Y= {0:p}", hitInfo.Y));
        _lInfo2.Text = result2.ToString();
    }
}