如何使用C#使鼠标在图表中移动时光标线跟随移动

6

图片描述

下图显示了我的项目中的一个图表。您可以看到有两条虚线交叉。我被要求使它跟随鼠标移动,但现在只有在我点击图表时它才会移动。我尝试使用CursorPositionChanging,但它没有起作用。下面的命令中也没有显示CursorEventHandler:

 this.chart1.CursorPositionChanging += new System.Windows.Forms.DataVisualization.Charting.Chart.CursorEventHandler(this.chart1_CursorPositionChanging);

我们需要添加额外的库吗?

现在我有两个问题:

  1. 让线条跟随鼠标移动
  2. 缺少CursorEventHandler

该项目是一个C#窗体应用程序。

3个回答

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

    // ...
}

6
图表支持“MouseMove”事件,每当鼠标在图表内移动时就会触发该事件。MouseEventArgs包含鼠标的位置,因此每次事件触发时,您可以基于该数据移动虚线。

它在某种程度上起作用了,但是线条从图表中的一个值跳到另一个值。它们没有平滑地移动。 - Daniel
1
我明白了,我只是把移动间隔改为零。 - Daniel

0
一个更加概括的形式,可以在不需要任何额外逻辑的情况下同步所有区域,具体如下:
var mousePoint = new Point(e.X, e.Y);
var chart = (Chart)sender;
//foreach child
foreach (var ca in chart.ChartAreas)
{
    ca.CursorX.SetCursorPixelPosition(mousePoint, true);
    ca.CursorY.SetCursorPixelPosition(mousePoint, true);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接