VB.Net 图表:系列和像素位置的工具提示
VB.Net Chart: ToolTip for Series and Pixel Position
我编写了一个 Windows 表单应用程序,只需单击一个按钮即可绘制图表。我成功地显示了图表。我决定在图表上添加一些额外的功能,当我将鼠标移过图表区域时,我将有一个 Point 对象,该对象将为图表上的 X 轴设置光标的像素位置,同时,工具提示应指示像素位置和系列相交处的 X 和 Y 值。这是我的当前事件:
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
我得到的是:
当然这看起来不错,但我的问题是工具提示仅在我的光标触及结果系列时显示。当我将光标从结果系列移开时,它就消失了。有没有办法只要Series和像素位置线有交集就可以在图表上显示ToolTip?
非常感谢。
哈里
这是答案(在 c# 中,但很容易翻译成 VB.net)
有一些方法可以将注释放在更好的位置。
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)
{
}
}
我编写了一个 Windows 表单应用程序,只需单击一个按钮即可绘制图表。我成功地显示了图表。我决定在图表上添加一些额外的功能,当我将鼠标移过图表区域时,我将有一个 Point 对象,该对象将为图表上的 X 轴设置光标的像素位置,同时,工具提示应指示像素位置和系列相交处的 X 和 Y 值。这是我的当前事件:
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
我得到的是:
当然这看起来不错,但我的问题是工具提示仅在我的光标触及结果系列时显示。当我将光标从结果系列移开时,它就消失了。有没有办法只要Series和像素位置线有交集就可以在图表上显示ToolTip?
非常感谢。
哈里
这是答案(在 c# 中,但很容易翻译成 VB.net) 有一些方法可以将注释放在更好的位置。
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)
{
}
}