Show series value over any point on chart using tooltip c#

左心房为你撑大大i 提交于 2019-12-08 01:32:13

问题


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


来源:https://stackoverflow.com/questions/13717189/show-series-value-over-any-point-on-chart-using-tooltip-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!