如何创建在图表[c#]上显示y1,y2坐标的垂直线?

时间:2014-04-26 11:32:04

标签: c# charts coordinates

我正在寻找显示红色垂直线的示例代码,并从图表中获取y1,y2坐标,当我们有2个系列时,红色垂直线在系列上(我在下图)?

我在stackoverflow上找到了这段代码(显示红色垂直线,我得到x值,但我不知道如何得到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;
}

我也找到了这个代码(显示十字准线和后面的x,y坐标,但我不需要水平线,因为程序运行速度慢):

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

是否有任何简单的方法可以进行修改,例如显示y1,y2坐标?也许有人知道怎么做或者可以给我任何有用的提示?我看了很多,但我发现只有x,y坐标的例子。

enter image description here

1 个答案:

答案 0 :(得分:0)

Axis.PixelPositionToValue应该做你想做的事,除了它说它只能从Paint方法调用