Reputation: 2413
I'm using a chart control within a C# windows forms project. What I would like is to have dotted lines follow my mouse around as it moves about the chart. I may make the lines centered about the cursor or the data point; at this point I am flexible. I've included a screen shot of what it is I'm looking for below.
So here you can see black dotted lines (the cursor doesn't appear because it was a screen grab). I already have a mouseMove event but I'm not sure which code to include in that mousemove to get this working (right now it only works when I click the mouse, but I think taht is just because I have CursorX.IsUserSelection enabled). I already formatted the lines in the chart creation function but is there some CursorX.LineEnable function or something similar? I've not been able to find any. I know that I could accomplish this with a painted object but I was hoping to avoid the hassle.
Thanks in advance! I'll include my line formatting below. This is in the chart creation section.
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;
Upvotes: 3
Views: 19469
Reputation: 959
Inside the chart's MouseMove event handler, you can do the following to make the cursor move:
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);
// ...
}
This is the documentation for the SetCursorPixelPosition method: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.cursor.setcursorpixelposition.aspx
Upvotes: 10