2017-02-20 193 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