После некоторых пустячный вокруг, я придумал что-то, что, кажется, работает очень хорошо.
В приложении добавьте объект диаграммы и задайте тип графика для сплайна (Свойства -> Серия -> ChartType = Spline). Я вручную установить мои х и у оси находятся в диапазоне от 0 до 100.
Global варов
х и у массивов должны быть одинаковой длиной и будут хранить 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
Mouse Move Event
Это где большая часть действия происходит. См. Комментарии для деталей.
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
Google vb.net графические методы. И НЕ получайте данные с графика. Измените данные с мыши и нарисуйте данные. –
Что такое технология переднего конца? WinForms, WPF, ASP.NET или другие? – djangojazz
WinForms. @Trevor, из любопытства, почему вы предлагаете использовать графику vb.net против объекта диаграммы и пытаться вытащить/изменить данные из этого? – dtbingle