【问题标题】:How to create vertical line which shows y1, y2 coordinates on the chart [c#]?如何在图表[c#]上创建显示y1、y2坐标的垂直线?
【发布时间】:2020-02-23 11:50:22
【问题描述】:

我正在寻找显示红色垂直线的示例代码,并从图表中获取 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 坐标示例。

【问题讨论】:

  • 我会添加一两个 LineAnnotations

标签: c# charts coordinates


【解决方案1】:

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

【讨论】:

  • Kristopher 也许你有什么想法,当我移动这条红色垂直线时,如何比较和显示值 y1 和 y2?我使用了这个 Axis.PixelPositionToValue,但我不知道接下来要做什么来同时比较和显示 y1 和 y2 值。
猜你喜欢
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多