VB.Net图表:用于系列和像素位置的工具提示

问题描述 投票:0回答:1

我编写了一个Windows窗体应用程序,只需单击一下按钮即可绘制图表。我成功地显示了图表。我决定在图表上添加一些额外的功能,当我在图表区域移动鼠标时,我将有一个Point对象,它将在图表上设置光标的X轴像素位置,同时,ToolTip将指示像素位置和系列交叉处的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

我得到了什么:enter image description here

当然这看起来不错,但我的问题是当我的光标触及Result系列时仅显示ToolTip。当我将光标移离结果系列时,它会消失。只要系列和像素位置线之间有交叉,有没有办法在图表上显示工具提示?

非常感谢。

vb.net charts tooltip
1个回答
1
投票

这是答案(在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)
        {

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.