how to make the cursor lines to follow the mouse i

2019-04-09 02:11发布

enter image description here

The picture below shows a chart in my project. As you can see there are two dotted crossing lines. I’m asked to make it to follow the mouse, but now only if I click on the chart it moves. I tried to use CursorPositionChanging but it didn’t work. CursorEventHandler also is not shown in the command below:

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

do we need to add extra lib for that? So I have two problems now: 1. Make the lines to follow the mouse 2. Missing CursorEventHandler

the project is window form application with C#

3条回答
家丑人穷心不美
2楼-- · 2019-04-09 02:36

The chart supports a 'MouseMove' event which is fired each time the mouse is moved inside the chart. The MouseEventArgs contain the position of the mouse so u can move the dotted lines based on that data each time the event fires.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-09 02:38
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);

    // ...
}
查看更多
We Are One
4楼-- · 2019-04-09 02:59

A more generalized form to synchronized all areas without any additional logic is as follows:

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);
}
查看更多
登录 后发表回答