2017-02-20 203 views
-1

如何在运行时在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

Google vb.net图形方法。并且不要从图表中获取数据。修改鼠标的数据并绘制数据。 –

+0

什么前端技术? WinForms,WPF,ASP.NET或其他? – djangojazz

+0

WinForms。 @Trevor,出于好奇,你如何建议使用vb.net图形与图表对象并试图从中拉取/修改数据? – dtbingle

回答

0

经过一番摆弄周围,我想出了,似乎工作得很好。

在您的应用程序中,添加图表对象并将图表类型设置为样条曲线(属性 - >系列 - > ChartType =样条曲线)。我手动设定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