Question Get Y-Value where cursor intersects line graph

skessinger

Member
Joined
Apr 12, 2010
Messages
8
Programming Experience
10+
I have a chart using 2-D line graphs with date/time as X-Axis. I add several series to the chart area. When the chart is plotted, the line graph connects the points in the series. When I click on the chart with the cursor displayed, I can use the CursorPositionChanged event:
    Private Sub chart1_CursorPositionChanged(sender As Object, e As CursorEventArgs) Handles chart1.CursorPositionChanged
            SetPosition(e.Axis, e.NewPosition)

    End Sub
    Public Sub SetPosition(axis As Axis, position As Double)
        Dim XValue As Double
        Dim YValue As Double
        Dim dataPoint As DataPoint
        Dim i As Integer
        Dim seriesName As String

        If Double.IsNaN(position) Then
            Return
        End If
        If axis.AxisName = AxisName.X Then
            ' Convert Double to DateTime.
            Dim dateTimeX As DateTime = DateTime.FromOADate(position)

            ' Set X cursor position to edit Control
            Me.lblDateTimeStamp.Text = dateTimeX.ToString("MM/dd/yy HH:mm:ss")
            Dim strFormat As String

            For i = 1 To Me.gintNumOfPens
                seriesName = "Series" & i
                dataPoint = Me.chart1.Series(seriesName).Points.FindByValue(position, "X", 0)

                If dataPoint IsNot Nothing Then
                    YValue = dataPoint.YValues(0)
                    strFormat = "0." & StrDup(gDecimalPlaces(i - 1), "0")
                    lblYValue2(i - 1).Text = "Y" & (i) & " = " & YValue.ToString(strFormat)
                Else
                    lblYValue2(i - 1).Text = "Y" & (i) & " = "

                End If
            Next

    End Sub 'SetPosition

My problem is that no value is returned unless the cursor intersects a value in the series. How can I get the Y values of the line graphs even if it is not in the series, or how can I get the previous value in the series? Is there perhaps a method to get the Y-value of the graph without having to look in the series, using the cursor? I don't want to have to make my user click on the graph itself, the cursor should be sufficient as it intersects the graph. My previous charting tool worked this way, so I must be able to give my users the same functionality.
 
Last edited by a moderator:
Back
Top