使用工具提示c#在图表上的任何点上显示系列值

时间:2012-12-05 05:55:40

标签: c# charts tooltip hittest

所以我看到很多关于使用工具提示在悬停在数据点上时显示图表数据的示例,但我希望在我的鼠标只是在图表上方而不一定在特定数据点上时显示值(即其他地方的空白)。我最终还希望在数据点上有一个小圆圈或正方形,但这可能会在以后出现。谁能告诉我怎么做?我将在下面包含我当前的代码。

 private void chData_MouseMove(object sender, MouseEventArgs e)
    {            
        HitTestResult pos = chData.HitTest(e.X, e.Y);
        if (pos.ChartElementType == ChartElementType.DataPoint)
        {
            string tipInfo;
            tipInfo = "Bat 1: " + ch1Array[pos.PointIndex].ToString("0.00") + Environment.NewLine + "Bat 2: " + ch2Array[pos.PointIndex].ToString("0.00") + Environment.NewLine;                
            tooltip.SetToolTip(chData, tipInfo);    
        }

    }

我猜我必须改变if语句,但我不确定是什么 非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

所以解决方案是不使用HitTest。相反,如果你使用PixelPositionToValue它会更好。我将在下面提供代码。

private void chData_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {   
            int cursorX = Convert.ToInt32(chData.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X));

            tipInfo = "Bat 1: " + ch1Array[cursorX].ToString("0.00") + Environment.NewLine + "Bat 2: " + ch2Array[cursorX].ToString("0.00") + Environment.NewLine;

            tooltip.SetToolTip(chData, tipInfo);

        }
        catch { }
    }
相关问题