- What is wrong with you code?
As for chart.ChartAreas[0].CursorX
: A Chart Cursor
is an object created by and used for zooming and which then
Represents a horizontal or vertical line that defines a position along
an axis.
So it has not much to do with the mouse cursor and is not valid when not zooming or (when zooming is disabled) for selecting.
- How to solve your problem then?
There are a few options you have; unfortunately none are both simple and exactly what you asked for.
One simple thing is to use the HitTest
for Charts
:
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult hit = chart1.HitTest(e.X, e.Y);
if (hit.PointIndex >= 0)
infoLabel.Text = "Over DataPoint No " + hit.PointIndex;
}
This is simple and safe but will work only when the cursor is actually over a DataPoint
. How well it suits you may depend on the ChartType
; Very well for Columns
or Bars
but not so well for Points
or Bubbles
..
And you could call PixelPositionToValue
:
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
var xv = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
var yv = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
infoLabel.Text = "x = " + xv + " y =" + yv;
}
In theory this is only safe to call from one of the Paint
events but in practice it seems to work fine when called from user interactions as well. (If you run into problems you could do a dummy call on one of the Paint
events and abort it with a flag after pulling the value(s) you want; the example does a lot more than what you need, but I doubt it will be necessary at all..)
However it will only bring back the Values
according to the axis, not the nearest DataPoint
. If you really need to get at the actual DataPoint
you would then have to do a search over the Points
of your Series
..:
Series S = chart1.Series[0]; // short reference
DataPoint pPrev = S.Points.Select(x => x)
.Where(x => x.XValue >= xv)
.DefaultIfEmpty(S.Points.First()).First();
DataPoint pNext = S.Points.Select(x => x)
.Where(x => x.XValue <= xv)
.DefaultIfEmpty(S.Points.Last()).Last();
This should bring up the previous and next DataPoint
. It is up to you to decide which one to use..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…