Author Topic: Chart values based on cursor position  (Read 1069 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Chart values based on cursor position
« on: February 24, 2016, 12:18:54 AM »
Hey Archie.  I would like to add a feature to my trend charts that will display the value of the plotted line in a label text field based on the vertical cursor position.  The attached chart has 5 series that are plotted based on the text of 5 BasicLabel controls in the left margin when the chart is in Auto.  What I would like to add is when the chart is stopped, the Vertical cursor can be placed anywhere on the chart and the value of the line at that point is displayed in the BasicLabels.  This would be similar to examples using the tooltip but some of the lines may overlay and be hard to distinguish from each other.  Would you be able to get me started with some sample code?  Thank you.

Phrog30

  • Guest
Re: Chart values based on cursor position
« Reply #1 on: May 01, 2017, 09:37:53 PM »
I think annotation is what you want.  Here's something that may work for you, I have been playing with this and it works ok:
Code: [Select]
Dim CA As ChartArea
    Dim S1 As Series
    Dim VA As VerticalLineAnnotation

    Private Sub Create_Annotation()

        CA = Chart1.ChartAreas(0) 
        S1 = Chart1.Series(0)     

        ' the vertical line
        VA = New VerticalLineAnnotation()
        VA.AxisX = CA.AxisX
        VA.AllowMoving = True
        VA.IsInfinitive = True
        VA.ClipToChartArea = CA.Name
        VA.Name = "myLine"
        VA.LineColor = Color.Black
        VA.LineWidth = 6   
        VA.X = 12

        Chart1.Annotations.Add(VA)

    End Sub
    Dim pt1 As Integer
    Dim pt As Integer
    Private Sub chart1_AnnotationPositionChanging(sender As Object, e As AnnotationPositionChangingEventArgs) Handles Chart1.AnnotationPositionChanging

        pt1 = CInt(e.NewLocationX)
        pt = Math.Floor(e.NewLocationX)

        If pt < 1 Then
            e.NewLocationX = 1
        ElseIf pt >= S1.Points.Count - 1 Then
            e.NewLocationX = S1.Points.Count
        End If

        If pt > 1 And pt < S1.Points.Count - 1 Then

            Dim [step] As Double = (S1.Points(pt + 1).YValues(0) - S1.Points(pt).YValues(0))
            Dim deltaX As Double = e.NewLocationX - S1.Points(pt).XValue
            Dim val As Double = S1.Points(pt).YValues(0) + [step] * deltaX
            Dim valueY = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.NewLocationY)


            'TextBox1.Text = [String].Format("X = {0:0.00}  Y = {1:0.00}", e.NewLocationX, val) 'Textbox is just for testing - you use what you want...
           
        End If
        Chart1.Update()

    End Sub