移动鼠标时如何显示X轴和Y轴值?

当鼠标移动到图表区域内的任何位置时(如图所示),如何显示X轴和Y轴的值?

在此处输入图像描述

HitTest方法不能应用于移动,也只能用于点击图表?

请帮我。 提前致谢。

工具提示显示绘制的实际区域数据之外的数据

实际上, Hittest方法在MouseMove运行得很好; 它的问题是它只会在你实际通过 DataPoint

可以通过这些轴函数从像素坐标检索/转换轴上的值:

 ToolTip tt = null; Point tl = Point.Empty; private void chart1_MouseMove(object sender, MouseEventArgs e) { if (tt == null ) tt = new ToolTip(); ChartArea ca = chart1.ChartAreas[0]; if (InnerPlotPositionClientRectangle(chart1, ca).Contains(e.Location)) { Axis ax = ca.AxisX; Axis ay = ca.AxisY; double x = ax.PixelPositionToValue(eX); double y = ay.PixelPositionToValue(eY); string s = DateTime.FromOADate(x).ToShortDateString(); if (e.Location != tl) tt.SetToolTip(chart1, string.Format("X={0} ; {1:0.00}", s, y)); tl = e.Location; } else tt.Hide(chart1); } 

请注意,在图表忙于布置图表元素时或图表元素执行之前 ,它们将无法工作。 MouseMove还不错。

在此处输入图像描述

另请注意,该示例显示原始数据,而x轴标签将数据显示为DateTimes 。 使用

 string s = DateTime.FromOADate(x).ToShortDateString(); 

或类似的东西将值转换为日期!

在实际plotarea内的检查使用这两个有用的function:

 RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA) { RectangleF CAR = CA.Position.ToRectangleF(); float pw = chart.ClientSize.Width / 100f; float ph = chart.ClientSize.Height / 100f; return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height); } RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA) { RectangleF IPP = CA.InnerPlotPosition.ToRectangleF(); RectangleF CArp = ChartAreaClientRectangle(chart, CA); float pw = CArp.Width / 100f; float ph = CArp.Height / 100f; return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y, pw * IPP.Width, ph * IPP.Height); } 

如果你想,你可以缓存InnerPlotPositionClientRectangle ; 您需要在更改数据布局或调整图表大小时执行此操作。