How to display the X and Y values while doing MouseMove on the chart?
You can display the X and Y values while doing any Mouse Events like MouseUp, MouseDown, MouseHover, MouseLeave on the chart by using the methods GetValueByPoint which returns the X and Y values of the ChartSeries calculated from the mousepoint and GetPointByValue which returns the X and Y values of the mousepoint calculated from the ChartPoint. Using tooltip we can display the above data. The following code snippet must be given under the Mouse Event handler
C#
private void chartControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//This gives the corresponding X and Y coordinates of the mouse point.
Point mousePoint = new Point( e.X, e.Y );
//The GetValueByPoint method returns the X and Y values of the ChartSeries calculated from the mousepoint.
ChartPoint chpt = chartControl1.ChartArea.GetValueByPoint( new Point( e.X, e.Y ) );
//The GetPointByValue method returns the X and Y values of the mousepoint calculated from the ChartPoint.
Point pt = chartControl1.ChartArea.GetPointByValue( chpt );
string text = "Mouse point: " + mousePoint.ToString() + "\nResult of method GetValueByPoint: {" + chpt.X .ToString() + "," + chpt.YValues[0].ToString() + "}" + "\nResult of method GetPointByValue: " + pt.ToString();
//As mouse moves over the chartcontrol this displays the values as ToolTip.
toolTip1.SetToolTip(this.chartControl1,text);
}
VB
Private Sub chartControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ChartControl1.MouseMove
'This gives the corresponding X and Y coordinates of the mouse point.
Dim mousePoint As Point = New Point(e.X, e.Y)
'The GetValueByPoint method returns the X and Y values of the ChartSeries calculated from the mousepoint.
Dim chpt As ChartPoint = ChartControl1.ChartArea.GetValueByPoint(New Point(e.X, e.Y))
'The GetPointByValue method returns the X and Y values of the mousepoint calculated from the ChartPoint.
Dim pt As Point = ChartControl1.ChartArea.GetPointByValue(chpt)
Dim [text] As String = "Mouse point: " + mousePoint.ToString() + vbLf + "Result of method GetValueByPoint: {" + chpt.X.ToString() + "," + chpt.YValues(0).ToString() + "}" + vbLf + "Result of method GetPointByValue: " + pt.ToString()
'As mouse moves over the chartcontrol this displays the values as ToolTip.
ToolTip1.SetToolTip(Me.ChartControl1, text)
End Sub