Show series value over any point on chart using to

2019-05-27 01:41发布

问题:

So I've seen lots of examples on using tooltip to show chart data when hovering over a data point, but I was hoping to show the values when my mouse is simply over the chart and not necessarily over a particular data point (i.e. the whitespace everywhere else). I would also eventually like to have a small circle or square on the data point itself but that can come later. Can anyone tell me how to do this? I will include my current code below.

 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);    
        }

    }

I'm going to guess I have to change the if statement argument but I'm not sure to what.
Any help is greatly appreciated!

回答1:

So the solution was to not use a HitTest. Instead if you use PixelPositionToValue it works much better. I'll include the code below.

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 { }
    }