在图表控件上显示鼠标轴坐标

有没有一种简单的方法来检索图表区域中任何点的X / Y坐标(当然相对于该图表轴)?

截至目前,我只是设法在鼠标在系列(不在外面)时检索坐标

private void chart_GetToolTipText(object sender, ToolTipEventArgs e) { if (e.HitTestResult.Series != null) { e.Text = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].YValues[0] + " \n " + DateTime.FromOADate(e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].XValue); } } 

无论如何,和MS Chart Controls一样,没有简单的方法可以做,
但是一个时髦的解决方法来完成任务。 我很遗憾地习惯了……

 private void chart1_MouseWhatever(object sender, MouseEventArgs e) { chartArea1.CursorX.SetCursorPixelPosition(new Point(eX, eY), true); chartArea1.CursorY.SetCursorPixelPosition(new Point(eX, eY), true); double pX = chartArea1.CursorX.Position; //X Axis Coordinate of your mouse cursor double pY = chartArea1.CursorY.Position; //Y Axis Coordinate of your mouse cursor } 

这适用于我的目的,并不会影响光标。

 private Tuple GetAxisValuesFromMouse(int x, int y) { var chartArea = _chart.ChartAreas[0]; var xValue = chartArea.AxisX.PixelPositionToValue(x); var yValue = chartArea.AxisY.PixelPositionToValue(y); return new Tuple(xValue, yValue); } 

我尝试了你的答案,但它对我不起作用。 它最终将光标放在一个位置,从不移动。 我相信这是因为我沿着两个轴使用十进制/双精度值,并且光标被舍入到最接近的整数。

经过多次尝试,我能够找到一种方法来确定光标在图表中的位置。 困难的部分是弄清楚图表元素的所有“位置”实际上是百分比值(从0到100)。

按照
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.elementposition.aspx
“以相对坐标定义图表元素的位置,范围从(0,0)到(100,100)。”

我希望你不介意,我在这里发布这个答案只是为了后人,万一其他人遇到这个问题,你的方法也不适合他们。 它在任何方面都不漂亮或优雅,但到目前为止它对我有用。

 private struct PointD { public double X; public double Y; public PointD(double X, double Y) { this.X = X; this.Y = Y; } } private void chart1_MouseMove(object sender, MouseEventArgs e) { var pos = LocationInChart(eX, eY); lblCoords.Text = string.Format("({0}, {1}) ... ({2}, {3})", eX, eY, pos.X, pos.Y); } private PointD LocationInChart(double xMouse, double yMouse) { var ca = chart1.ChartAreas[0]; //Position inside the control, from 0 to 100 var relPosInControl = new PointD ( ((double)xMouse / (double)execDetailsChart.Width) * 100, ((double)yMouse / (double)execDetailsChart.Height) * 100 ); //Verify we are inside the Chart Area if (relPosInControl.X < ca.Position.X || relPosInControl.X > ca.Position.Right || relPosInControl.Y < ca.Position.Y || relPosInControl.Y > ca.Position.Bottom) return new PointD(double.NaN, double.NaN); //Position inside the Chart Area, from 0 to 100 var relPosInChartArea = new PointD ( ((relPosInControl.X - ca.Position.X) / ca.Position.Width) * 100, ((relPosInControl.Y - ca.Position.Y) / ca.Position.Height) * 100 ); //Verify we are inside the Plot Area if (relPosInChartArea.X < ca.InnerPlotPosition.X || relPosInChartArea.X > ca.InnerPlotPosition.Right || relPosInChartArea.Y < ca.InnerPlotPosition.Y || relPosInChartArea.Y > ca.InnerPlotPosition.Bottom) return new PointD(double.NaN, double.NaN); //Position inside the Plot Area, 0 to 1 var relPosInPlotArea = new PointD ( ((relPosInChartArea.X - ca.InnerPlotPosition.X) / ca.InnerPlotPosition.Width), ((relPosInChartArea.Y - ca.InnerPlotPosition.Y) / ca.InnerPlotPosition.Height) ); var X = relPosInPlotArea.X * (ca.AxisX.Maximum - ca.AxisX.Minimum) + ca.AxisX.Minimum; var Y = (1 - relPosInPlotArea.Y) * (ca.AxisY.Maximum - ca.AxisY.Minimum) + ca.AxisY.Minimum; return new PointD(X, Y); } 

这很有效

 private void chart1_MouseWhatever(object sender, MouseEventArgs e) { Point chartLocationOnForm = chart1.FindForm().PointToClient(chart1.Parent.PointToScreen(chart1.Location)); double x = chart1.ChartAreas[0].AxisX.PixelPositionToValue(eX - chartLocationOnForm.X); double y = chart1.ChartAreas[0].AxisY.PixelPositionToValue(eY - chartLocationOnForm.Y); } 

这很有效

 private void chart1_MouseWhatever(object sender, MouseEventArgs e) { Point chartLocationOnForm = chart1.FindForm().PointToClient(chart1.Parent.PointToScreen(chart1.Location)); chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(new PointF(eX - chartLocationOnForm.X, eY - chartLocationOnForm.Y), true); chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(new PointF(eX - chartLocationOnForm.X, eY - chartLocationOnForm.Y), true); double x = chart1.ChartAreas[0].CursorX.Position; double y = chart1.ChartAreas[0].CursorY.Position; } 
 private void OnChartMouseMove(object sender, MouseEventArgs e) { var sourceChart = sender as Chart; HitTestResult result = sourceChart.HitTest(eX, eY); ChartArea chartAreas = sourceChart.ChartAreas[0]; if (result.ChartElementType == ChartElementType.DataPoint) { chartAreas.CursorX.Position = chartAreas.AxisX.PixelPositionToValue(eX); chartAreas.CursorY.Position = chartAreas.AxisY.PixelPositionToValue(eY); } } 

这就是我所得到的,我认为我们中的许多人都是沿着同样的路线,但对你正在寻找的东西有不同的解释。

这将为您提供绘图区域中任何位置的坐标。 我发现HitTest提供了一个干净而简单的解决方案,但是有一些检查要做,无论光标是在数据点,网格线还是在绘图区域(似乎优先顺序)。 我假设你会对坐标感兴趣,无论鼠标在哪个对象上。

 private void chart_GetToolTipText(object sender, ToolTipEventArgs e) { // If the mouse isn't on the plotting area, a datapoint, or gridline then exit HitTestResult htr = chart.HitTest(eX, eY); if (htr.ChartElementType != ChartElementType.PlottingArea && htr.ChartElementType != ChartElementType.DataPoint && htr.ChartElementType != ChartElementType.Gridlines) return; ChartArea ca = chart.ChartAreas[0]; // Assuming you only have 1 chart area on the chart double xCoord = ca.AxisX.PixelPositionToValue(eX); double yCoord = ca.AxisY.PixelPositionToValue(eY); e.Text = "X = " + Math.Round(xCoord, 2).ToString() + "\nY = " + Math.Round(yCoord, 2).ToString(); }