I have written a Windows form application that will plot a chart on a click of a button. I managed to successfully display the chart. I decided to add some extra features on the chart where when I move the mouse across the chart area, I will have a Point object that will set the cursor's pixel position for the X-axis on chart and at the same time, a ToolTip shall indicate the X and Y value at the intersection of the pixel position and the series. This is my current event:
Private Sub Chart1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
Dim mousepoint As Point = New Point(e.X, e.Y)
Chart1.ChartAreas(0).CursorX.Interval = 0
Chart1.ChartAreas(0).CursorX.SetCursorPixelPosition(mousepoint, True)
Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y)
If result.PointIndex > -1 AndAlso result.ChartArea IsNot Nothing Then
Me.Chart1.Series("Result").ToolTip = "Value: #VALY{F}mA\nDate: #VALX"
End If
End Sub
Of course this looks good, but my problem is the ToolTip only show when my cursor touches the Result series. When I move the cursor away from the Result series, it vanishes. Is there a way to show the ToolTip on the chart as long as there is an intersect between the Series and the pixel position line?
Thanks a lot.
Hari
Here is the answer (in c#, but easy to translate into VB.net) There are ways to put the annotations in a better position.
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
chart1.Annotations.Clear();
try
{
ChartArea ca = chart1.ChartAreas[0];
Double y = ca.AxisY.PixelPositionToValue(e.Y);
Double x = ca.AxisX.PixelPositionToValue(e.X);
if (y < ca.AxisY.Minimum || y > ca.AxisY.Maximum || x < ca.AxisX.Minimum || x > ca.AxisX.Maximum) return;
TextAnnotation taX = new TextAnnotation();
taX.Name = "cursorX";
taX.Text = x.ToString("0.##");
taX.X = ca.AxisX.ValueToPosition(x);
taX.Y = ca.AxisY.ValueToPosition(y);
TextAnnotation taY = new TextAnnotation();
taY.Name = "cursorY";
taY.Text = y.ToString("0.##");
taY.X = ca.AxisX.ValueToPosition(x);
taY.Y = ca.AxisY.ValueToPosition(y) + 5;
chart1.Annotations.Add(taX);
chart1.Annotations.Add(taY);
}
catch (Exception ex)
{
}
}