如何绘制仅包含一行的折线图(并获取值)

时间:2017-02-20 20:59:08

标签: vb.net

如何在VB.NET中运行时绘制折线图,​​只允许一行?一行我的意思是每个X值只能有一个Y值。最重要的是,我希望能够在完成时拉出该行的相应X和Y值。例如:

从一组在X和Y方向上从0到100运行的空白轴开始。单击并按住鼠标并绘制所需的曲线(图中左侧)。现在您想要修改30 - 50 X值范围。单击x = 30并绘制一个刚刚超过x = 50的山谷(图中的右图)。在第二次单击进行修改的位置时,请注意未绘制第二条线,但是会覆盖第一次单击时的Y值并将其修补到原始线中。

最后,我需要能够在绘制线后从表中提取数据。所以我会留下一个定义该行的X,Y坐标列表。

enter image description here

1 个答案:

答案 0 :(得分:0)

经过一番摆弄后,我想出了一些似乎运作良好的东西。

在您的应用程序中,添加图表对象并将图表类型设置为样条线(Properties - > Series - > ChartType = Spline)。我手动设置我的x和y轴范围从0到100。

全球大战

x和y数组必须具有相同的长度,并存储将在图表中绘制的x / y点。

Dim bIsDragging = False

Dim xTest() As Integer = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100}
Dim yTest() As Integer = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}

鼠标事件

这里的想法是,在鼠标按下事件之后,它被认为是"拖动"动作直到鼠标按下事件。 bIsDragging标志跟踪此状态。还要注意"拖动"如果鼠标离开图表窗口,则会停止事件。

 Private Sub Chart1_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart1.MouseDown
    bIsDragging = True
End Sub

Private Sub Chart1_MouseUp(sender As Object, e As MouseEventArgs) Handles Chart1.MouseUp
    bIsDragging = False
End Sub

Private Sub Chart1_MouseLeave(sender As Object, e As EventArgs) Handles Chart1.MouseLeave
    bIsDragging = False
End Sub

鼠标移动事件

这是大部分行动发生的地方。有关详细信息,请参阅注释。

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

    If bIsDragging Then

        ' The PixelPositionToValue() command expects a value within the chart area size.  When your
        ' mouse leaves the top edge for example, there is a brief second where the PixelPositionToValue()
        ' has a value outside of the chart area window and throws and exception.  This prevents that exception.
        If e.X < 5 Or e.X > Chart1.Size.Width - 5 Or e.Y < 5 Or e.Y > Chart1.Size.Height - 5 Then
            Return
        End If

        ' Gets chart value based on mouse position
        Dim xChartVal As Integer = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.X)
        Dim yChartVal As Integer = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Y)

        ' Allows some drawing control outside of the hard axes lines.  For example,
        ' if you are trying to draw a line across the y=0 line, it would be hard to
        ' hold the mouse exactly at y=0 and is much eaasier to go in a region below
        ' the x-axis to ensure it says at y=0.  Note that the 0 and 100 values below are hard
        ' coded based on my manual chart axes regions of 0 to 100.
        If xChartVal < 0 Then
            xChartVal = 0
        End If
        If xChartVal > 100 Then
            xChartVal = 100
        End If
        If yChartVal < 0 Then
            yChartVal = 0
        End If
        If yChartVal > 100 Then
            yChartVal = 100
        End If

        ' This relates your current mouse position to the nearest '5' x value in the
        ' global array and then modifies the y-index that matches this x-value.
        ' There's probably a better way to record these positions, but my application is
        ' a table output to an arduino where a massive table to go through would impact performance.
        ' Therefore, my x-axis resolution 5 for 0 to 100 to keep the number of points rather small.
        xChartVal = Math.Round(xChartVal / 5) * 5
        yTest(Array.IndexOf(xTest, xChartVal)) = yChartVal

        ' Updates chart
        Chart1.Series("Series1").Points.DataBindXY(xTest, yTest)

    End If

End Sub