3

我在 C# windows 窗体项目中使用图表控件。我想要的是让虚线跟随我的鼠标在图表上移动。我可以使线条以光标或数据点为中心;在这一点上我很灵活。我在下面包含了我正在寻找的内容的屏幕截图。
光标上的黑色虚线

所以在这里你可以看到黑色的虚线(光标没有出现,因为它是一个屏幕抓取)。我已经有一个 mouseMove 事件,但我不确定要在该 mousemove 中包含哪些代码才能使其正常工作(现在它仅在我单击鼠标时才有效,但我认为这只是因为我启用了 CursorX.IsUserSelection)。我已经在图表创建函数中格式化了线条,但是是否有一些 CursorX.LineEnable 函数或类似的函数?我一直找不到。我知道我可以通过绘制对象来完成此操作,但我希望避免麻烦。
提前致谢!我将在下面包括我的行格式。这是在图表创建部分。

        chData.ChartAreas[0].CursorX.IsUserEnabled = true;
        chData.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
        chData.ChartAreas[0].CursorY.IsUserEnabled = true;
        chData.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

        chData.ChartAreas[0].CursorX.Interval = 0;
        chData.ChartAreas[0].CursorY.Interval = 0;
        chData.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
        chData.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

        chData.ChartAreas[0].CursorX.LineColor = Color.Black;
        chData.ChartAreas[0].CursorX.LineWidth = 1;
        chData.ChartAreas[0].CursorX.LineDashStyle = ChartDashStyle.Dot;
        chData.ChartAreas[0].CursorX.Interval = 0;
        chData.ChartAreas[0].CursorY.LineColor = Color.Black;
        chData.ChartAreas[0].CursorY.LineWidth = 1;
        chData.ChartAreas[0].CursorY.LineDashStyle = ChartDashStyle.Dot;
        chData.ChartAreas[0].CursorY.Interval = 0;
4

1 回答 1

10

在图表的 MouseMove 事件处理程序中,您可以执行以下操作来移动光标:

private void chData_MouseMove(object sender, MouseEventArgs e)
{
    Point mousePoint = new Point(e.X, e.Y);

    Chart.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint, true);
    Chart.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint, true);

    // ...
}

这是 SetCursorPixelPosition 方法的文档:http: //msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.cursor.setcursorpixelposition.aspx

于 2013-01-23T19:03:03.323 回答