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

时间:2011-03-24 20:29:31

标签: c# .net mschart

是否有一种简单的方法可以检索图表区域中任意点的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);
    }
}

6 个答案:

答案 0 :(得分:10)

无论如何,与MS Chart Controls一样,没有简单的方法可以做事,但这是一个时髦的解决方法来完成工作。我很遗憾地习惯了......

private void chart1_MouseWhatever(object sender, MouseEventArgs e)
{
    chartArea1.CursorX.SetCursorPixelPosition(new Point(e.X, e.Y), true);
    chartArea1.CursorY.SetCursorPixelPosition(new Point(e.X, e.Y), 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
}

答案 1 :(得分:9)

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

private Tuple<double,double> 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<double, double>(xValue, yValue);
}

答案 2 :(得分:6)

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

经过多次尝试,我能够找到一种方法来确定光标在图表中的位置。困难的部分是弄清楚图表元素的所有“位置”实际上是百分比值(从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(e.X, e.Y);
  lblCoords.Text = string.Format("({0}, {1}) ... ({2}, {3})", e.X, e.Y, 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);
}

答案 3 :(得分:1)

这有效

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(e.X - chartLocationOnForm.X);    
    double y = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y - 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(e.X - chartLocationOnForm.X, e.Y - chartLocationOnForm.Y), true);
    chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(new PointF(e.X - chartLocationOnForm.X, e.Y - chartLocationOnForm.Y), true);

    double x = chart1.ChartAreas[0].CursorX.Position;
    double y = chart1.ChartAreas[0].CursorY.Position;
}

答案 4 :(得分:1)

private void OnChartMouseMove(object sender, MouseEventArgs e)
{
    var sourceChart = sender as Chart;
    HitTestResult result = sourceChart.HitTest(e.X, e.Y);
    ChartArea chartAreas = sourceChart.ChartAreas[0];

    if (result.ChartElementType == ChartElementType.DataPoint)  
    {
        chartAreas.CursorX.Position = chartAreas.AxisX.PixelPositionToValue(e.X);
        chartAreas.CursorY.Position = chartAreas.AxisY.PixelPositionToValue(e.Y);
    }
}

答案 5 :(得分:0)

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

这将为您提供绘图区域中任何位置的坐标。我发现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(e.X, e.Y);
    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(e.X);
    double yCoord = ca.AxisY.PixelPositionToValue(e.Y);

    e.Text = "X = " + Math.Round(xCoord, 2).ToString() + "\nY = " + Math.Round(yCoord, 2).ToString();
}