4

Is there any ways around to find the index position of a curve, based on the current xPosition,

let's say I have a curve Item - MyCurve, which has 20k points and when the mouse moves I could get the mouse location & then I could get the x & y positions by simply using the following function.

double xPos=0, yPos=0;

this.zedGraphControl1.GraphPane.ReverseTransform(MouseLoc, out xPos, out yPos);

but I want to find the data points from the curve item, any suggestions...?

enter image description here Thanks in advance....:)

SanVEE
  • 2,009
  • 5
  • 34
  • 55

2 Answers2

3

Bear in mind that the following is only an approximation, it should be accurate especially when you the mouse gets closer to the point, but as you are looking at the mouse position you may not be directly on a point on your curve. It also assumes that your CurveItem Curve has points, and that they are evenly distributed.

double startPos = Curve.Points[0].X
double xStep = Curve.Points[Curve.NPts - 1].X / Curve.NPts;
int xIndex = (int)(xPos / xStep + startPos);
// Make sure it is in bounds
xIndex = xIndex < 0 ? 0 : xIndex > Curve.NPts - 1 ? Curve.NPts - 1 : xIndex;

OR you can use the following function:

CurveItem n_curve;
int index;
zedGraphControl1.GraphPane.FindNearestPoint(mousePt, out n_curve, out index);

But keep in mind that that will look for the nearest curve and the index of the nearest point within that curve.

NominSim
  • 8,447
  • 3
  • 28
  • 38
  • Thanks a lot NominSim, I've tried both the methods,but nothings suits my need, I need get the exact data points not the approximates. In order to use Method-2, I have to place the mouse position over curve item, which is good for single curve. – SanVEE Aug 02 '12 at 15:05
  • 2
    @San Instead of `(int)(xPos / xStep + startPos);` you can play around with doing `Math.Round()` , `.Floor()` or `.Ceiling()`. Since you are using your mouse position, you're going to be _close_ to the point, but not exactly on it, so there will have to be some manipulation of the index you obtain to get a more accurate result. – NominSim Aug 02 '12 at 15:09
  • 1
    @San There is a `FindNearestPoint` [overload](http://zedgraph.sourceforge.net/documentation/html/M_ZedGraph_GraphPane_FindNearestPoint.htm) that allows you to specify the target curve for which you are trying to find your point index, and you can also configure the search tolerance using [GraphPane.Default.NearestTol](http://zedgraph.sourceforge.net/documentation/html/T_ZedGraph_GraphPane_Default.htm). Would this be enough for your needs? – Anders Gustafsson Aug 02 '12 at 15:39
  • @AndersGustafsson, Cheers again for pointing out the overloading option, but there is an issue with this approach, please check my updated image, it will only get the nearest point not according to x axis. – SanVEE Aug 02 '12 at 15:54
  • 1
    @San The first method then in my answer is going to be your safest bet. – NominSim Aug 02 '12 at 15:56
  • @NominSim, I guess I have no other go apart from your solution, anyways thanks....:) – SanVEE Aug 02 '12 at 16:04
1

If you are not concerned with using the positions programmatically, but only want to see the positions displayed in your graph, you can set zedGraphControl1.IsShowPointValues to true:

Display point values in graph

Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • 1
    ,Thanks for the suggestion, but it makes no sense to show all the 20k labels on the zedgraph. – SanVEE Aug 02 '12 at 15:10
  • 2
    @San Just to point out, setting `IsShowPointValues` to `true` will only show the closest point to the mouse of the curve, not all at once. – NominSim Aug 02 '12 at 15:14
  • @NominSim, Sorry & Yeah You're Correct....as mentioned in my quesstion, I'm trying to find the exact index, so that I could read Power & Time(x&y) and display those values on a marker table not on the graph. – SanVEE Aug 02 '12 at 15:21