I am writing an application to plot a line chart. It gets a row from a CSV file and pass it to a method (There are a few hundred rows):
Private Function plotChart(ByVal sortedRow As String())
count = sortedRow(8) 'Represents X value
Dim spcRawPoint = sortedRow(7) 'Represents Y value
Try
Me.Chart1.Series("Result").Points.AddXY(count, spcPoint)
Me.Chart1.Series("Result").ToolTip = "Value: " + spcRawPoint + Environment.NewLine + "Date: " + count.ToString
Catch ex As Exception
MessageBox.Show(ErrorToString)
End Try
End Function
When I am hovering the mouse to the series at the chart, I wanted it to display the respective X(Decimal) and Y(DateTime) values. With the above method, once the chart is plotted, when I hover to the series, I see that the displayed X and Y values are the same at all points on the series.
I did some searching in the internet and I saw a suggestion to include a MouseMove or MouseHover event in my chart. I did this:
Private Sub Chart1_MouseHover(ByVal sender As System.Object, ByVal e As ToolTipEventArgs) Handles Chart1.MouseHover
If e.HitTestResult.PointIndex >= 0 Then
If e.HitTestResult.ChartElementType = DataVisualization.Charting.ChartElementType.DataPoint Then
Dim Xvalue As Decimal = e.X
Dim Yvalue As DateTime = Convert.ToDateTime(e.Y)
Me.Chart1.Series("Result").ToolTip = "Value: " + Xvalue + Environment.NewLine + "Date: " + Yvalue.ToString
End If
End If
End Sub
The code is able to build, but when I run it an exception occurs:
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs'.
I see that it is a casting problem but I am out of idea on how to solve it. What I need is when I hover my mouse to the mentioned series at the chart area, the tooltip shall show the X and Y values.
I managed to fix the MouseHover event:
Private Sub Form1_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseHover
Me.Chart1.Series("Result").ToolTip = "#VALY{F}\n#VALX"
End Sub
Now, I can see the X and Y values whenever I Hover my mouse on the series of the chart.