2020年4月3日金曜日

Oxyplot Mouse Events (03) MouseDown

MouseDown

Imports Oxyplot.Axes
Imports Oxyplot.Series
Public Class Form
    Private WithEvents Model As PlotModel
    Private WithEvents Line As LineSeries
    Private indexOfPointToMove As Integer
    Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
        Model = New PlotModel With {
            .Subtitle = "Left click to edit or add points.",
            .Title = "MouseDown",
            .LegendSymbolLength = 40,
            .Background = OxyColors.White
        }
        Dim linearAxisX = New LinearAxis With {
            .Position = AxisPosition.Bottom
        }
        Model.Axes.Add(linearAxisX)
        Dim linearAxisY = New LinearAxis()
        Model.Axes.Add(linearAxisY)
        Line = New LineSeries With {
            .Color = OxyColors.SkyBlue,
            .MarkerFill = OxyColors.SkyBlue,
            .MarkerSize = 6,
            .MarkerStroke = OxyColors.White,
            .MarkerStrokeThickness = 1.5,
            .MarkerType = MarkerType.Circle,
            .Title = "LineSeries1"
        }
        Line.Points.Add(New DataPoint(0, 10))
        Line.Points.Add(New DataPoint(10, 40))
        Line.Points.Add(New DataPoint(40, 20))
        Line.Points.Add(New DataPoint(60, 30))
        Model.Series.Add(Line)
        PlotView.Model = Model
        indexOfPointToMove = -1
    End Sub
    Private Sub Line_MouseDown(sender As Object, e As OxyMouseDownEventArgs) Handles Line.MouseDown
        If e.ChangedButton = OxyMouseButton.Left Then
            Dim indexOfNearestPoint As Integer = CInt(Math.Round(e.HitTestResult.Index))
            Dim nearestPoint As ScreenPoint = Line.Transform(Line.Points(indexOfNearestPoint))
            If (nearestPoint - e.Position).Length < 10 Then
                indexOfPointToMove = indexOfNearestPoint
            Else
                Dim i As Integer = CInt(e.HitTestResult.Index + 1)
                Line.Points.Insert(i, Line.InverseTransform(e.Position))
                indexOfPointToMove = i
            End If
            Line.LineStyle = LineStyle.DashDot
            Model.InvalidatePlot(False)
            e.Handled = True
        End If
    End Sub
    Private Sub Line_MouseMove(sender As Object, e As OxyMouseEventArgs) Handles Line.MouseMove
        If indexOfPointToMove >= 0 Then
            Line.Points(indexOfPointToMove) = Line.InverseTransform(e.Position)
            Model.InvalidatePlot(False)
            e.Handled = True
        End If
    End Sub
    Private Sub Line_MouseUp(sender As Object, e As OxyMouseEventArgs) Handles Line.MouseUp
        indexOfPointToMove = -1
        Line.LineStyle = LineStyle.Solid
        Model.InvalidatePlot(False)
        e.Handled = True
    End Sub
    Private Sub Model_MouseDown(sender As Object, e As OxyMouseDownEventArgs) Handles Model.MouseDown
        If e.ChangedButton = OxyMouseButton.Left Then
            Line.Points.Add(Line.InverseTransform(e.Position))
            indexOfPointToMove = Line.Points.Count - 1
            Model.InvalidatePlot(False)
            e.Handled = True
        End If
    End Sub
End Class

0 件のコメント:

コメントを投稿