在图表上使用鼠标时如何在图表区域上获取自定义X值

时间:2018-12-28 17:33:48

标签: vb.net charts linechart

我正在根据报告制作图表,而我的问题是,当我在图表(注释)上移动鼠标时,如何获取图表的x值(图表区域的自定义值)?

在过去的2天中,我已经这样做了,但我仍然不知道如何获取图表的x值。我尝试使用图表上的相关代码,但仍然没有得到想要的结果

Private Sub cmdGenChart_Click(sender As Object, e As EventArgs) Handles cmdGenChart.Click
    Access.AddParam("@dt1", DateTimePicker1.Value.AddDays(-1).ToString)
    Access.AddParam("@dt2", DateTimePicker2.Value.ToString)

    Access.ExecQuery("SELECT [Flood_ID], [Flood_Level], [Flood_Time], [Flood_Date] " & _
                     "FROM WaterLevel " & _
                     "WHERE [Flood_Date] BETWEEN @dt1 AND @dt2 " & _
                     "ORDER BY [Flood_ID] ASC")

    Dim checkDate As DateTime = DateTimePicker1.Value

    For Each r As DataRow In Access.dt.Rows

        Chart1.ChartAreas(0).AxisX.CustomLabels.Add(Counter - 1, Counter + 1, r("Flood_Time"))

        Chart1.Series("Water Level").Points.AddXY(Counter, r("Flood_Level"))

        Counter += 1
    Next

    Dim PC As New CalloutAnnotation

    With PC
        Chart1.Annotations.Add(PC)
    End With
End Sub

Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove

    Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y)

    If result.ChartElementType = ChartElementType.DataPoint Then
        Chart1.Series(0).Points(result.PointIndex).XValue.ToString()

        Dim thisPt As New PointF(CSng(Chart1.Series(0).Points(result.PointIndex).XValue),
                               CSng(Chart1.Series(0).Points(result.PointIndex).YValues(0)))

        Dim ta As New CalloutAnnotation

        With ta
            .AnchorDataPoint = Chart1.Series(0).Points(result.PointIndex)
            .Text = "Water Level: " & thisPt.Y.ToString & vbCrLf & "Time: " & DateTime.FromOADate(thisPt.X).ToShortTimeString

            If thisPt.Y.ToString <= 20 And thisPt.Y Then
                .ForeColor = Color.Green
                .Font = New Font("Tahoma", 10, FontStyle.Bold)
            ElseIf thisPt.Y.ToString <= 60 Then
                .ForeColor = Color.Orange
                .Font = New Font("Tahoma", 10, FontStyle.Bold)
            ElseIf thisPt.Y.ToString > 60 Then
                .ForeColor = Color.Red
                .Font = New Font("Tahoma", 10, FontStyle.Bold)
            End If

        End With

        Chart1.Annotations(0) = ta
        Chart1.Invalidate()
    End If
End Sub

我想要的是Time的值等于它下面相应x值的值。

在图像上:
水位:15
时间:12:00 AM

所需结果:
水位:15
时间:上午6:21

http://prntscr.com/m0p4xb

1 个答案:

答案 0 :(得分:0)

经过如此多的思考,我找到的解决方案是将计数器设置为等于数据库中主键的值,并将其设置为将其绑定在图表上,然后仅将其加载到SQL中命令,就成功了。

全局变量

    Dim counter as Double = 1

“生成图表”按钮

Private Sub cmdGenChart_Click(sender As Object, e As EventArgs) Handles cmdGenChart.Click
    Access.AddParam("@dt1", DateTimePicker1.Value.AddDays(-1).ToString)
    Access.AddParam("@dt2", DateTimePicker2.Value.ToString)


    Access.ExecQuery("SELECT [Flood_ID], [Flood_Level], [Flood_Time], [Flood_Date] " & _
                     "FROM WaterLevel " & _
                     "WHERE [Flood_Date] BETWEEN @dt1 AND @dt2 " & _
                     "ORDER BY [Flood_ID] ASC")

    For Each r As DataRow In Access.dt.Rows

        Counter = r("Flood_ID")

        Chart1.ChartAreas(0).AxisX.CustomLabels.Add(Counter - 1, Counter + 1, r("Flood_Time"))

        Chart1.Series("Water Level").Points.AddXY(Counter, r("Flood_Level"))

        Counter += 1
    Next

    Dim PC As New CalloutAnnotation

    With PC
        Chart1.Annotations.Add(PC)
    End With

End Sub

图表鼠标移动

Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove

    Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y)

    If result.ChartElementType = ChartElementType.DataPoint Then
        Chart1.Series(0).Points(result.PointIndex).XValue.ToString()

        Access.AddParam("@pos", Chart1.Series(0).Points(result.PointIndex).XValue)
        Access.AddParam("@level", Chart1.Series(0).Points(result.PointIndex).YValues(0))

        Access.ExecQuery("SELECT [Flood_ID], [Flood_Level], [Flood_Time] " & _
                         "FROM WaterLevel " & _
                         "WHERE [Flood_ID] = @pos AND [Flood_Level] = @level ")

        Dim r As DataRow = Access.dt.Rows(0)

        Dim ta As New CalloutAnnotation

        With ta
            .AnchorDataPoint = Chart1.Series(0).Points(result.PointIndex)
            .Text = "Water Level: " & r("Flood_Level") & vbCrLf & "Time: " & r("Flood_Time")

        End With

        Chart1.Annotations(0) = ta
        Chart1.Invalidate()
    End If
End Sub

这是结果: http://prntscr.com/m2xpnf