【发布时间】: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