2020年4月6日月曜日

Oxyplot Mouse Events (06) ArrowAnnotation

ArrowAnnotation

Imports Oxyplot.Axes
Imports Oxyplot.Annotations
Public Class Form
    Private WithEvents ArrowAnt As ArrowAnnotation
    Private plotModel As PlotModel
    Private lastPoint As DataPoint
    Private moveStartPoint As Boolean
    Private moveEndPoint As Boolean
    Private originalColor As OxyColor
    Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
        plotModel = New PlotModel With {
            .Subtitle = "Click and drag the arrow.",
            .Title = "ArrowAnnotation",
            .Background = OxyColors.White
        }
        Dim linearAxisX = New LinearAxis With {
            .Maximum = 60,
            .Minimum = -40,
            .Position = AxisPosition.Bottom
        }
        plotModel.Axes.Add(linearAxisX)
        Dim linearAxisY = New LinearAxis With {
            .Maximum = 10,
            .Minimum = -10
        }
        plotModel.Axes.Add(linearAxisY)
        ArrowAnt = New ArrowAnnotation With {
            .StartPoint = New DataPoint(8, 4),
            .Text = "Move me!"
        }
        plotModel.Annotations.Add(ArrowAnt)
        PlotView.Model = plotModel

        lastPoint = DataPoint.Undefined
        moveStartPoint = False
        moveEndPoint = False
        originalColor = ArrowAnt.Color
    End Sub
    Private Sub ArrowAnt_MouseDown(sender As Object, e As OxyMouseDownEventArgs) Handles ArrowAnt.MouseDown
        If Not e.ChangedButton = OxyMouseButton.Left Then
            Return
        End If
        lastPoint = ArrowAnt.InverseTransform(e.Position)
        If Not e.HitTestResult.Index = 2 Then
            moveStartPoint = True
        Else
            moveStartPoint = False
        End If
        If Not e.HitTestResult.Index = 1 Then
            moveEndPoint = True
        Else
            moveEndPoint = False
        End If
        originalColor = ArrowAnt.Color
        ArrowAnt.Color = OxyColors.Red
        plotModel.InvalidatePlot(False)
        e.Handled = True
    End Sub
    Private Sub ArrowAnt_MouseMove(sender As Object, e As OxyMouseEventArgs) Handles ArrowAnt.MouseMove
        Dim thisPoint = ArrowAnt.InverseTransform(e.Position)
        Dim dx = thisPoint.X - lastPoint.X
        Dim dy = thisPoint.Y - lastPoint.Y
        If moveStartPoint = True Then
            ArrowAnt.StartPoint = New DataPoint(ArrowAnt.StartPoint.X + dx, ArrowAnt.StartPoint.Y + dy)
        End If
        If moveStartPoint = True Then
            ArrowAnt.EndPoint = New DataPoint(ArrowAnt.EndPoint.X + dx, ArrowAnt.EndPoint.Y + dy)
        End If
        lastPoint = thisPoint
        plotModel.InvalidatePlot(False)
        e.Handled = True
    End Sub
    Private Sub ArrowAnt_MouseUp(sender As Object, e As OxyMouseEventArgs) Handles ArrowAnt.MouseUp
        ArrowAnt.Color = originalColor
        plotModel.InvalidatePlot(False)
    End Sub
End Class

0 件のコメント:

コメントを投稿