none
Get both ends of a line annotation's x/y coordinates

    Question

  • Hi all,

    I've been searching for this answer left and right and I can't seem to find it. I have a basic chart on my form. And I have a line annotation on the chart which allows moving, allows selecting, allows resizing, etc for the user. This annotation is not anchored to any points.

    My question is, when in run time, and the user moves and resizes the line annotation, how can I get the coordinates of both ends of the line? I know that you can set/get the X/Y position, but this only sets the left end of the line annotation. If a user moves it, or resizes it, I need to know what the x/y position is of the right end of the line as well. Is this possible at all?

    Thanks, Shawn

    Monday, July 06, 2015 8:40 PM

Answers

  • @ Monkeyboy and STShawnJK,

     Yes, that helps.  With a mix of that and what Tom showed i threw this together which will work.  I suppose since i am using the Sender object to get the LineAnnotation and using that to get the X, Y, Width, and Height to calculate the X2 and Y2 Values, it could be used in the AnnotationPositionChanged or AnnotationPositionChanging events.  You just need to call the event sub that you choose to use.   8)

     You will need to call the event sub you choose to use by supplying the LineAnnotation as the Sender object (the 1st parameter) such as shown in the Button1 Click event sub in the code below.  I used Tom`s example code for creating the chart for this example (hope Tom does not mind).  Every time you move the Annotation`s with code you will need to call the sub.

     I named my Annotation which i figured might make it easier to call the sub and supply the correct Annotation for the Sender parameter if you have more than one on the chart.  However, you can use the Index instead if you want.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Private WithEvents Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart With {.Width = 500, .Height = 400}
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Me.Width = 700
            Me.Controls.Add(Chart1)
    
            Chart1.ChartAreas.Add(New ChartArea)
            Chart1.Legends.Add(New Legend)
            Chart1.Legends(0).Enabled = False
            Chart1.AntiAliasing = AntiAliasingStyles.All
            Chart1.TextAntiAliasingQuality = TextAntiAliasingQuality.High
    
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Distance"
                .AxisX.MajorGrid.LineColor = Color.LightBlue
                .AxisX.Minimum = 0
                .AxisY.Title = "Elevation"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.Minimum = -1
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("")
            With Chart1.Series(Chart1.Series.Count - 1)
                .ChartType = DataVisualization.Charting.SeriesChartType.Line
                .BorderWidth = 2
    
                For x As Integer = 1 To 9
                    .Points.AddXY(x, Math.Sin(x))
                Next
            End With
    
            Dim annotation As New LineAnnotation()
            annotation.Name = "Annotation1"
            annotation.AnchorDataPoint = Chart1.Series(0).Points(3)
            annotation.Height = -10
            annotation.Width = 25
            annotation.LineWidth = 2
            annotation.StartCap = LineAnchorCapStyle.Arrow
            annotation.EndCap = LineAnchorCapStyle.Arrow
            annotation.AllowAnchorMoving = True
            annotation.AllowMoving = True
            annotation.AllowSelecting = True
            annotation.AllowResizing = True
            Chart1.Annotations.Add(annotation)
        End Sub
    
        Private Sub Chart1_AnnotationPositionChanging(ByVal sender As Object, ByVal e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging
            Dim la As LineAnnotation = DirectCast(sender, LineAnnotation) 'get the LineAnnotation from the Sender parameter Object
    
            Dim x2p As Double = Chart1.ChartAreas(0).AxisX.GetPosition(la.X) + la.Width
            Dim x2 As Double = Chart1.ChartAreas(0).AxisX.PositionToValue(x2p)
    
            Dim y2p As Double = Chart1.ChartAreas(0).AxisY.GetPosition(la.Y) + la.Height
            Dim y2 As Double = Chart1.ChartAreas(0).AxisY.PositionToValue(y2p)
    
            Me.Text = la.Name & "  X2 = " & x2.ToString & "  Y2 = " & y2.ToString
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Set the new position of the LineAnnotation
            Chart1.Annotations("Annotation1").X = 2
            Chart1.Annotations("Annotation1").Y = 0.5
    
            'Call the AnnotationPositionChanging by supplying the LineAnnotation you just moved as the Sender Object
            Chart1_AnnotationPositionChanging(Chart1.Annotations("Annotation1"), New AnnotationPositionChangingEventArgs)
        End Sub
    
    End Class

     

     PS - They are shutting my electric of from 9AM until 2PM so if you have questions i will not be available until after 2 sometime but, Tom or Monkeyboy can probably help if you need it.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, July 08, 2015 11:40 AM
    • Marked as answer by STShawnJK Thursday, July 09, 2015 12:38 AM
    Wednesday, July 08, 2015 11:24 AM

All replies

  • I've been searching for this answer left and right and I can't seem to find it. I have a basic chart on my form. And I have a line annotation on the chart which allows moving, allows selecting, allows resizing, etc for the user. This annotation is not anchored to any points.

    If you are using the Microsoft Chart Control for .Net then you should ask your question in that forum.
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=MSWinWebChart

    If you are doing your chart in some other way then you should describe what that is.

    Monday, July 06, 2015 10:07 PM
  • According to MSDN's Annotations documentation you can use the below and supposedly get that info at runtime. Wouldn't left and top and right and bottom provide that?

    Sizing Annotations

    Size an annotation in the following ways:

    • Use Annotation.Width and Annotation.Height at design time.
    • Use Annotation.Right and Annotation.Bottom at run time.


    La vida loca


    Monday, July 06, 2015 11:16 PM
  • Yes, if you read the MS documents on the LineAnnotation Class you will see there is a few properties that might work for you.  There is the Right, Left, and Bottom properties as Mr. Monkeyboy has mentioned already but, i didn`t see a Top property (seems strange).  There is also a property called LineWidth that seems like if you know the X value you could just add the LineWidth to that and get the value of the other end.  There may be more i missed so, i would read through the link.

    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, July 07, 2015 1:57 AM
    Tuesday, July 07, 2015 1:56 AM
  • Monkey is right. But how to get at that point is a bit tricky!

    Note the coordinates from the

         Chart1_AnnotationPositionChanging

    put into the form text seem to be what you want?

    Option Strict On
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form4
        Private WithEvents Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart With {.Dock = DockStyle.Fill}
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.Width = 500
            Me.Controls.Add(Chart1)
    
            'setup the chart
            Chart1.ChartAreas.Add(New ChartArea())
            Chart1.Legends.Add(New Legend())
            Chart1.Legends(0).Enabled = False
            Chart1.AntiAliasing = AntiAliasingStyles.All
            Chart1.TextAntiAliasingQuality = TextAntiAliasingQuality.High
    
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Distance"
                .AxisX.MajorGrid.LineColor = Color.LightBlue
                .AxisX.Minimum = 0
                .AxisY.Title = "Elevation"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.Minimum = -1
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
    
            'draw the chart
            'create series 
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            'add data to chart series
            With Chart1.Series(Chart1.Series.Count - 1)
                .ChartType = DataVisualization.Charting.SeriesChartType.Line
                .BorderWidth = 2
    
                For x = 1 To 9
                    .Points.AddXY(x, Math.Sin(x))
                Next
            End With
    
            Dim annotation As New LineAnnotation()
            annotation.AnchorDataPoint = Chart1.Series(0).Points(3)
            annotation.Height = -10
            annotation.Width = 25
            annotation.LineWidth = 2
            annotation.StartCap = LineAnchorCapStyle.Arrow
            annotation.EndCap = LineAnchorCapStyle.Arrow
            annotation.AllowAnchorMoving = True
    
            annotation.AllowMoving = True
            annotation.AllowSelecting = True
            annotation.AllowResizing = True
    
            Chart1.Annotations.Add(annotation)
    
        End Sub
    
        Private Sub Chart1_AnnotationPositionChanging(sender As Object, e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging
    
            Dim x As Single = CSng(Chart1.Annotations(0).AnchorDataPoint.XValue)
            Dim y As Single = CSng(Chart1.Annotations(0).AnchorDataPoint.YValues(0))
            Dim w As Single = CSng(Chart1.Annotations(0).Width)
            Dim h As Single = CSng(Chart1.Annotations(0).Height)
    
            Me.Text = (x + w).ToString & ", " & (y + h).ToString
        End Sub
    
    End Class

    Tuesday, July 07, 2015 3:37 AM
  •  Thanks all for your replies, I really appreciate it. Tommy, that seems to be getting me in the right direction, thanks. What I need is the chart coordinates of the end point. So, in your image, where the annotation is moved, it stops at around 4.9 and 0.3. Those are the coordinates I would need to retrieve....

    Tuesday, July 07, 2015 12:29 PM
  •  Thanks all for your replies, I really appreciate it. Tommy, that seems to be getting me in the right direction, thanks. What I need is the chart coordinates of the end point. So, in your image, where the annotation is moved, it stops at around 4.9 and 0.3. Those are the coordinates I would need to retrieve....


        Private Sub Chart1_AnnotationPositionChanged(sender As Object, e As EventArgs) Handles Chart1.AnnotationPositionChanged
            Dim x As Single = CSng(Chart1.Annotations(0).AnchorDataPoint.XValue)
            Dim y As Single = CSng(Chart1.Annotations(0).AnchorDataPoint.YValues(0))
            Dim w As Single = CSng(Chart1.Annotations(0).Width)
            Dim h As Single = CSng(Chart1.Annotations(0).Height)
    
            Me.Text = (x + w).ToString & ", " & (y + h).ToString
        End Sub
    
    

    • Proposed as answer by Mr. Monkeyboy Tuesday, July 07, 2015 1:48 PM
    Tuesday, July 07, 2015 1:29 PM
  • Hey Tom,

     I don`t believe that is what he wants.  He wants the numbers like if you move the right side of the LineAnnotation to (.5, 8) it returns the numbers .5 and 8.  I have been pulling my hair out for hours trying to calculate things to get these numbers but, still no luck.  Your example returns numbers like (35.72098 and -50.3558) when the right side is moved to (.5, 8).

     It seems to have some screwy way of scaling these numbers that i can`t seem to figure out.  I thought maybe dividing the clientsize width by 10 for an X scale and calculating the X position with that would work but, the more i try the more i fail to understand how they are scaling these.  Of coarse, i am not as familiar with charts as you and Monkeyboy so, that probably has something to do with it.   8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, July 07, 2015 2:56 PM
    Tuesday, July 07, 2015 2:54 PM
  • Hey Tom,

     I don`t believe that is what he wants.  He wants the numbers like if you move the right side of the LineAnnotation to (.5, 8) it returns the numbers .5 and 8.  I have been pulling my hair out for hours trying to calculate things to get these numbers but, still no luck.  Your example returns numbers like (35.72098 and -50.3558) when the right side is moved to (.5, 8).

     It seems to have some screwy way of scaling these numbers that i can`t seem to figure out.  I thought maybe dividing the clientsize width by 10 for an X scale and calculating the X position with that would work but, the more i try the more i fail to understand how they are scaling these.  Of coarse, i am not as familiar with charts as you and Monkeyboy so, that probably has something to do with it.   8)


    If you say it can`t be done then i`ll try it

    Oh, yeah, duh. The chart has its own ways. Best not to try and understand it I find. Just copy the examples. You do have the examples right? Sometimes you have to look at the c# as vb is sometimes missing in the examples.

    Looks like it has to go this way then (which might be easier than the other), but there are details to work out:

        Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                ' Calculate new Y value from current cursor position
                Dim x As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.X)
                Dim y As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Y)
    
                Me.Text = x.ToString & ", " & y.ToString
            End If
    
    
        End Sub
    

    • Proposed as answer by IronRazerz Tuesday, July 07, 2015 6:09 PM
    Tuesday, July 07, 2015 4:54 PM
  • PS Here are the examples I mean.

    Get the full version. You just run it in VS as a project. It has pics and code. Its fun.

    Tuesday, July 07, 2015 5:12 PM
  • @ Tom,

     That was the magic trick.  I would only add one thing.  In the If Then statement i would check if the Annotation is selected too.  That way it does not change the values if you left click and drag the mouse on the chart when you are not dragging the Annotation.  Other than that it works great.   8)

        Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Chart1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left And Chart1.Annotations(0).IsSelected Then
                ' Calculate new Y value from current cursor position
                Dim x As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.X)
                Dim y As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Y)
    
                Me.Text = x.ToString & ", " & y.ToString
            End If
        End Sub

     I also noticed that they say in the documents that the AxisX.PixelPositionToValue method only works in a Paint event.  I saw that earlier so, i did not try it because i was not using a Paint event.  I guess i should have tried it anyways because, it appears that it does work outside of a Paint event.  I wonder why they say that?

     Well, i got my scaling problem figured out.  I had to subtract the pixel positions of the AxisX.GetPosition(0) value and the ClientSize.Width - AxisX.GetPosition(10) value from the ClientSize width before dividing it by 10 to calculate the scaling ratio.   So anyways, it figures you would show the answer now.   8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, July 07, 2015 6:15 PM
    Tuesday, July 07, 2015 6:09 PM
  • Razerz,

    Ok that's good. Good to see another Chart person BTW.

    Yes I don't know exactly what STS wants or needs so that's what I mean by details.

    You can define chartareas and can have titles and legends and junk I don't show so they may affect what you are calculating too. I have never needed to actually do what you are doing. There always seems to be something built in. Its just really hard to find it there are so many things.

    Here is more details from the examples. I am running the examples in VS 2010. Not sure if they work for you?

    This one allows moving up and down only but also has select and other events shown (partial bits of code only not complete project).

    Imports System.Windows.Forms.DataVisualization.Charting
    ...
    
    '/ <summary>
    '/ Mouse Down Event
    '/ </summary>
    Private Sub Chart1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)  Handles Chart1.MouseDown
       ' Call Hit Test Method
       Dim hitResult As HitTestResult = Chart1.HitTest(e.X, e.Y)
       
       ' Initialize currently selected data point
       selectedDataPoint = Nothing
       If hitResult.ChartElementType = ChartElementType.DataPoint Then
          selectedDataPoint = CType(hitResult.Object, DataPoint)
          
          ' Show point value as label
          selectedDataPoint.IsValueShownAsLabel = True
          
          ' Set cursor shape
          Chart1.Cursor = Cursors.SizeNS
       End If
    End Sub 'Chart1_MouseDown
    
    
    '/ <summary>
    '/ Mouse Move Event
    '/ </summary>
    Private Sub Chart1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)  Handles Chart1.MouseMove
       ' Check if data point selected
       If Not (selectedDataPoint Is Nothing) Then
          ' Mouse coordinates should not be outside of the chart 
          Dim coordinate As Integer = e.Y
          If coordinate < 0 Then
             coordinate = 0
          End If
          If coordinate > Chart1.Size.Height - 1 Then
             coordinate = Chart1.Size.Height - 1
          End If 
          ' Calculate new Y value from current cursor position
          Dim yValue As Double = Chart1.ChartAreas("Default").AxisY.PixelPositionToValue(coordinate)
          yValue = Math.Min(yValue, Chart1.ChartAreas("Default").AxisY.Maximum)
          yValue = Math.Max(yValue, Chart1.ChartAreas("Default").AxisY.Minimum)
          
          ' Update selected point Y value
          selectedDataPoint.YValues(0) = yValue
          
          ' Invalidate chart
          Chart1.Invalidate()
       Else
          ' Set different shape of cursor over the data points
          Dim hitResult As HitTestResult = Chart1.HitTest(e.X, e.Y)
          If hitResult.ChartElementType = ChartElementType.DataPoint Then
             Chart1.Cursor = Cursors.Hand
          Else
             Chart1.Cursor = Cursors.Default
          End If
       End If
    End Sub 'Chart1_MouseMove
    
    '/ <summary>
    '/ Mouse Up Event
    '/ </summary>
    Private Sub Chart1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs)  Handles Chart1.MouseUp
       ' Initialize currently selected data point
       If Not (selectedDataPoint Is Nothing) Then
          ' Hide point label
          selectedDataPoint.IsValueShownAsLabel = False
          
          ' reset selected object
          selectedDataPoint = Nothing
          
          ' Invalidate chart
          Chart1.Invalidate()
          
          ' Reset cursor style
          Chart1.Cursor = Cursors.Default
       End If
    End Sub 'Chart1_MouseUp
    
    ... 


    Tuesday, July 07, 2015 6:39 PM
  • You guys are awesome, thanks a lot! I've been searching and searching and couldn't find this...that's why I hate to saythis, but it's about half of what I need. The code you gave indeed works perfect if you are resizing the annotation. And I need that for sure. So that's half the problem, the other half is, let's just say instead of resizing the annotation I simply move it somewhere else on the chart without resizing it. Would there still be a way to get that x,y coordinate at the end of the line (the same coordinates that you got when you provided the code above on a resize)?

    Thanks guys, Shawn

    Tuesday, July 07, 2015 8:28 PM
  • You can use the same code with a small change from within the Chart.AnnotationPositionChanging event which is raised if you move the lineannotation in any way.  Try this.   8)

        Private Sub Chart1_AnnotationPositionChanging(ByVal sender As Object, ByVal e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging
            Dim pnt As Point = Chart1.PointToClient(MousePosition)
    
            Dim x As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(pnt.X)
            Dim y As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(pnt.Y)
    
            Me.Text = x.ToString & ", " & y.ToString
        End Sub
     

     EDIT -  I see this is not being raised if i change the X and Y position in code so, i will see if i can find something else.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, July 07, 2015 10:07 PM
    Tuesday, July 07, 2015 9:22 PM
  • Thanks, any help is much appreciated :-)
    Wednesday, July 08, 2015 2:45 AM
  • @IronRazerz

    Maybe the code in the Button2 click event can help? Bottom of the code window.

    Option Strict On
    
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
    
        Dim Generate As New Random
        Dim IncreaseWidth As Integer = 0
        Dim Count As Integer = 0
    
        Dim annotation As New LineAnnotation()
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            Chart1.AntiAliasing = AntiAliasingStyles.All
            Chart1.TextAntiAliasingQuality = TextAntiAliasingQuality.High
    
            Chart1.ChartAreas("ChartArea1").AxisX.ScaleView.Size = 10
            Chart1.ChartAreas("ChartArea1").AxisX.ScrollBar.Size = 20
    
            Chart1.ChartAreas(0).AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll
            Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = True
            Chart1.ChartAreas(0).AxisX.ScrollBar.BackColor = Color.LightGray
            Chart1.ChartAreas(0).AxisX.ScrollBar.ButtonColor = Color.Gray
            Chart1.Series("Example1").Color = Color.Green
            Chart1.Series("Example1").BorderWidth = 3 ' Set the lines width
            Chart1.Series("Example2").Color = Color.Red
            Chart1.Series("Example2").BorderWidth = 3
            Chart1.Series("Example3").Color = Color.Turquoise
            Chart1.Series("Example3").BorderWidth = 3
            Chart1.Series("Example4").Points.AddXY(4, 0)
            Chart1.Series("Example4").Points.AddXY(7, 2)
            annotation.AnchorDataPoint = Chart1.Series(3).Points(0)
            annotation.BackColor = Color.Black
            annotation.ForeColor = Color.Black
            annotation.Height = 0
            annotation.Width = 7
            annotation.LineWidth = 4
            annotation.StartCap = LineAnchorCapStyle.Arrow
            annotation.EndCap = LineAnchorCapStyle.Arrow
            annotation.AllowAnchorMoving = True
    
            annotation.AllowMoving = True
            annotation.AllowSelecting = True
            annotation.AllowResizing = True
    
            Chart1.Annotations.Add(annotation)
            Timer1.Interval = 500
            Timer2.Interval = 10
            Button1.Text = "Start"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Timer1.Enabled = False Then
                Button1.Text = "Stop"
                Timer1.Enabled = True
                Timer2.Enabled = True
            Else
                Button1.Text = "Start"
                Timer1.Enabled = False
                Timer2.Enabled = False
            End If
        End Sub
    
        Private Sub Chart1_DoubleClick(sender As Object, e As EventArgs) Handles Chart1.DoubleClick
            If Count = 0 Then Timer2.Stop()
            If Count = 1 Then Timer2.Start()
            Count = Count + 1
            If Count = 2 Then Count = 0
        End Sub
    
        Private Sub Chart1_MouseHover(sender As Object, e As EventArgs) Handles Chart1.MouseHover
            Try
                Chart1.Series("Example1").Points(3).ToolTip = "(Hello world)"
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim A As Integer = (Generate.Next(51, 101))
            If Chart1.Series("Example1").Points.Count = 200 Then
                Chart1.Series("Example1").Points.RemoveAt(0)
            End If
            Chart1.Series("Example1").Points.AddY(A)
            Dim B As Integer = (Generate.Next(-50, 51))
            Chart1.Series("Example2").Points.AddY(B)
            Dim C As Integer = (Generate.Next(-100, -51))
            Chart1.Series("Example3").Points.AddY(C)
        End Sub
    
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            If Chart1.ChartAreas(0).AxisX.Maximum > Chart1.ChartAreas(0).AxisX.ScaleView.Size Then
                Chart1.ChartAreas(0).AxisX.ScaleView.Scroll(Chart1.ChartAreas(0).AxisX.Maximum + 100)
            End If
        End Sub
    
        Private Sub Chart1_AnnotationPositionChanging(ByVal sender As Object, ByVal e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging
            Label1.Text = "X = " & CType(sender, LineAnnotation).X.ToString & " " & "Y = " & CType(sender, LineAnnotation).Y.ToString & vbCrLf & _
            "AnchorOffsetX = " & CType(sender, LineAnnotation).AnchorOffsetX.ToString & " " & "AnchorOffsetY = " & CType(sender, LineAnnotation).AnchorOffsetY.ToString & vbCrLf & _
            "AnchorX = " & CType(sender, LineAnnotation).AnchorX.ToString & " " & "AnchorY = " & CType(sender, LineAnnotation).AnchorY.ToString & vbCrLf & _
            "Right = " & CType(sender, LineAnnotation).Right.ToString & " " & "Bottom = " & CType(sender, LineAnnotation).Bottom.ToString & vbCrLf & _
            "Width = " & CType(sender, LineAnnotation).Width.ToString & " " & "Height = " & CType(sender, LineAnnotation).Height.ToString & vbCrLf & _
            "AnchorDataPoint.XValue = " & CType(sender, LineAnnotation).AnchorDataPoint.XValue.ToString & " " & "AnchorDataPoint.YValues(0) = " & CType(sender, LineAnnotation).AnchorDataPoint.YValues(0).ToString & vbCrLf & _
            "e.NewAnchorLocationX = " & e.NewAnchorLocationX.ToString & " " & "e.NewAnchorLocationY = " & e.NewAnchorLocationY.ToString & vbCrLf & _
            "e.NewLocationX = " & e.NewLocationX.ToString & " " & "e.NewLocationY = " & e.NewLocationY.ToString & vbCrLf & _
            "e.NewSizeWidth = " & e.NewSizeWidth.ToString & " " & "e.NewSizeHeight = " & e.NewSizeHeight.ToString & vbCrLf & _
            CType(sender, LineAnnotation).LineWidth.ToString
        End Sub
    
        Private Sub Chart1_AnnotationPositionChanged(sender As Object, e As EventArgs) Handles Chart1.AnnotationPositionChanged
            Label2.Text = "X = " & CType(sender, LineAnnotation).X.ToString & " " & "Y = " & CType(sender, LineAnnotation).Y.ToString & vbCrLf & _
             "AnchorOffsetX = " & CType(sender, LineAnnotation).AnchorOffsetX.ToString & " " & "AnchorOffsetY = " & CType(sender, LineAnnotation).AnchorOffsetY.ToString & vbCrLf & _
             "AnchorX = " & CType(sender, LineAnnotation).AnchorX.ToString & " " & "AnchorY = " & CType(sender, LineAnnotation).AnchorY.ToString & vbCrLf & _
             "Right = " & CType(sender, LineAnnotation).Right.ToString & " " & "Bottom = " & CType(sender, LineAnnotation).Bottom.ToString & vbCrLf & _
             "Width = " & CType(sender, LineAnnotation).Width.ToString & " " & "Height = " & CType(sender, LineAnnotation).Height.ToString & vbCrLf & _
             "AnchorDataPoint.XValue = " & CType(sender, LineAnnotation).AnchorDataPoint.XValue.ToString & " " & "AnchorDataPoint.YValues(0) = " & CType(sender, LineAnnotation).AnchorDataPoint.YValues(0).ToString
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If Button2.Text = "1st" Then
                Button2.Text = "2nd"
                annotation.X = 7
                annotation.Y = 2
                Chart1_AnnotationPositionChanging(annotation, New AnnotationPositionChangingEventArgs)
                Chart1_AnnotationPositionChanged(annotation, New EventArgs)
            Else
                Button2.Text = "1st"
                annotation.X = 5
                annotation.Y = 0
                Chart1_AnnotationPositionChanging(annotation, New AnnotationPositionChangingEventArgs)
                Chart1_AnnotationPositionChanged(annotation, New EventArgs)
            End If
        End Sub
    
    End Class

    Image, top pic app run, 2nd pic Button2 selected, 3rd pic Button2 selected again, 4th pic manually moved right end of annotation with mouse.


    La vida loca



    Wednesday, July 08, 2015 5:34 AM
  • @ Monkeyboy and STShawnJK,

     Yes, that helps.  With a mix of that and what Tom showed i threw this together which will work.  I suppose since i am using the Sender object to get the LineAnnotation and using that to get the X, Y, Width, and Height to calculate the X2 and Y2 Values, it could be used in the AnnotationPositionChanged or AnnotationPositionChanging events.  You just need to call the event sub that you choose to use.   8)

     You will need to call the event sub you choose to use by supplying the LineAnnotation as the Sender object (the 1st parameter) such as shown in the Button1 Click event sub in the code below.  I used Tom`s example code for creating the chart for this example (hope Tom does not mind).  Every time you move the Annotation`s with code you will need to call the sub.

     I named my Annotation which i figured might make it easier to call the sub and supply the correct Annotation for the Sender parameter if you have more than one on the chart.  However, you can use the Index instead if you want.

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Private WithEvents Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart With {.Width = 500, .Height = 400}
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Me.Width = 700
            Me.Controls.Add(Chart1)
    
            Chart1.ChartAreas.Add(New ChartArea)
            Chart1.Legends.Add(New Legend)
            Chart1.Legends(0).Enabled = False
            Chart1.AntiAliasing = AntiAliasingStyles.All
            Chart1.TextAntiAliasingQuality = TextAntiAliasingQuality.High
    
            With Chart1.ChartAreas(0)
                .AxisX.Title = "Distance"
                .AxisX.MajorGrid.LineColor = Color.LightBlue
                .AxisX.Minimum = 0
                .AxisY.Title = "Elevation"
                .AxisY.MajorGrid.LineColor = Color.LightGray
                .AxisY.Minimum = -1
                .BackColor = Color.FloralWhite
                .BackSecondaryColor = Color.White
                .BackGradientStyle = GradientStyle.HorizontalCenter
                .BorderColor = Color.Blue
                .BorderDashStyle = ChartDashStyle.Solid
                .BorderWidth = 1
                .ShadowOffset = 2
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("")
            With Chart1.Series(Chart1.Series.Count - 1)
                .ChartType = DataVisualization.Charting.SeriesChartType.Line
                .BorderWidth = 2
    
                For x As Integer = 1 To 9
                    .Points.AddXY(x, Math.Sin(x))
                Next
            End With
    
            Dim annotation As New LineAnnotation()
            annotation.Name = "Annotation1"
            annotation.AnchorDataPoint = Chart1.Series(0).Points(3)
            annotation.Height = -10
            annotation.Width = 25
            annotation.LineWidth = 2
            annotation.StartCap = LineAnchorCapStyle.Arrow
            annotation.EndCap = LineAnchorCapStyle.Arrow
            annotation.AllowAnchorMoving = True
            annotation.AllowMoving = True
            annotation.AllowSelecting = True
            annotation.AllowResizing = True
            Chart1.Annotations.Add(annotation)
        End Sub
    
        Private Sub Chart1_AnnotationPositionChanging(ByVal sender As Object, ByVal e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging
            Dim la As LineAnnotation = DirectCast(sender, LineAnnotation) 'get the LineAnnotation from the Sender parameter Object
    
            Dim x2p As Double = Chart1.ChartAreas(0).AxisX.GetPosition(la.X) + la.Width
            Dim x2 As Double = Chart1.ChartAreas(0).AxisX.PositionToValue(x2p)
    
            Dim y2p As Double = Chart1.ChartAreas(0).AxisY.GetPosition(la.Y) + la.Height
            Dim y2 As Double = Chart1.ChartAreas(0).AxisY.PositionToValue(y2p)
    
            Me.Text = la.Name & "  X2 = " & x2.ToString & "  Y2 = " & y2.ToString
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Set the new position of the LineAnnotation
            Chart1.Annotations("Annotation1").X = 2
            Chart1.Annotations("Annotation1").Y = 0.5
    
            'Call the AnnotationPositionChanging by supplying the LineAnnotation you just moved as the Sender Object
            Chart1_AnnotationPositionChanging(Chart1.Annotations("Annotation1"), New AnnotationPositionChangingEventArgs)
        End Sub
    
    End Class

     

     PS - They are shutting my electric of from 9AM until 2PM so if you have questions i will not be available until after 2 sometime but, Tom or Monkeyboy can probably help if you need it.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, July 08, 2015 11:40 AM
    • Marked as answer by STShawnJK Thursday, July 09, 2015 12:38 AM
    Wednesday, July 08, 2015 11:24 AM
  • PS - They are shutting my electric of from 9AM until 2PM so if you have questions i will not be available until after 2 sometime but, Tom or Monkeyboy can probably help if you need it.


    If you say it can`t be done then i`ll try it


    Wow. That's a long time. Make em pay for the food in your fridge!

    La vida loca

    Wednesday, July 08, 2015 1:19 PM
  • Thanks IronRazerz, your help is much appreciated. Thanks for taking the time into doing all that. Can't wait to start implementing it....Much thanks

    Shawn

    Thursday, July 09, 2015 4:20 AM
  • You`re Welcome.

      I am not much of a Chart guy and this is the 2nd time i ever tried using a Chart control so, i was having fun learning it along with you.  8)



    If you say it can`t be done then i`ll try it

    Thursday, July 09, 2015 11:08 AM
  • I gotta turn to the master to figure this one out...

    I get the following exception...

    Position argument should be in range from 0 to 100

    at the following line

    Dim x2 As Double = Chart1.ChartAreas(0).AxisX.PositionToValue(x2p)

    ...when I move the annotation out of the chart area. It says the value should be 0 to 100, but yet the value in my chart's x-axis is 0 to 800 and it moves around just fine unless I try something funky like move the annotation out of the chart area, or even move it down a little bit.

    I'm not sure what exactly is causing this, especially when it's move the annotation around the chart with x values of 0 to 800...

    Tuesday, July 14, 2015 2:00 PM
  • I figured it out, here's the answer for anyone needing this in the future...

    Dim Corr_Line As LineAnnotation = DirectCast(sender, LineAnnotation), P2_X As Double = 0, P1_X As Double = 0, P2_Y As Double = 0, P1_Y As Double = 0

            P1_X = Chart1.ChartAreas(0).AxisX.GetPosition(Corr_Line.X) + Corr_Line.Width

            If Not P1_X < 0 AndAlso Not P1_X > 100 Then P2_X = Chart1.ChartAreas(0).AxisX.PositionToValue(P1_X)

            P1_Y = Chart1.ChartAreas(0).AxisY.GetPosition(Corr_Line.Y) + Corr_Line.Height
            If Not P1_Y < 0 AndAlso Not P1_Y > 100 Then P2_Y = Chart1.ChartAreas(0).AxisY.PositionToValue(P1_Y)

         
    Friday, July 17, 2015 2:58 PM