LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

GetGraphCursorIndex() doesn't work as expected

Hello,

I have a problem using the function GetGraphCursorIndex(). On my front panel there is a graph-control (control mode: hot, data mode: retain) with a snap-to-point cursor.
In my callback function I draw several graphs using PlotXY(). After drawing I want to determine, on which plot the cursor has "snapped". To do this I use the function GetGraphCursorIndex(), which returns the plothandle the cursor points to.
The code is like this:

...
// Make Graph invisible (to accelerate plotting)
SetCtrlAttribute (hMainPanel, MAIN_GRAPH, ATTR_VISIBLE, 0);

// Delete old plots
DeleteGraphPlot (hMainPanel, MAIN_GRAPH, -1, VAL_IMMEDIATE_DRAW);

// Plot new graphs
for (i=0; i<20; i++)
{
    data[i].plothandle = PlotXY (hMainPanel, MAIN_GRAPH,
                                data[i].xarray, data[i].yarray,
                                 data[i].points, VAL_DOUBLE,
                                 VAL_DOUBLE, data[i].plotstyle,
                                 VAL_NO_POINT, data[i].linestyle,
                                 1, data[i].plotcolor);
}

// Make Graph visible
SetCtrlAttribute (hMainPanel, MAIN_GRAPH, ATTR_VISIBLE, 1);

// Determine cursor position
GetGraphCursorIndex (hMainPanel, MAIN_GRAPH, 1, &plot, &index);
...

But plot and index are both -1. I tried several things to solve the problem.
If I call GetGraphCursor(), then I receive the x and y values of the graph the cursor points to. But GetGraphCursorIndex() doesn't work. If I call GetGraphCursorIndex() later from another callback-function, then it works as expected.
What is the problem?
If I let the graph visible then it works, but plotting is very slow. It would be nice if anyone can help me.

Best regards,

Martin
0 Kudos
Message 1 of 3
(3,111 Views)

I had a look at some old code I used a while back to do a very similar thing. After drawing the graphs, I invoked a function call to the graph callback routine, with an EVENT_COMMIT. Don't remember the details, but the comment in my source code suggested that it was needed to force the cursor(s) to update ie for them to actually attach to plots. (This is the equivalent of the user clicking on the graph and snapping a cursor to a plotted point.)

JR

0 Kudos
Message 2 of 3
(3,107 Views)
Hi Martin.

JR is correct.

The fact that the values of plot and index are both -1 after you call GetGraphCursorIndex() indicates that the cursor is not attached to a plot (maybe because the control is invisible?). You need to force this to happen after you call PlotXY().

Something like this should work (add appropriate error-checking):

...
// Plot new graphs
for (i=0; i<20; i++)
{
    data[i].plothandle = PlotXY (hMainPanel, MAIN_GRAPH,
                                 data[i].xarray, data[i].yarray,
                                 data[i].points, VAL_DOUBLE,
                                 VAL_DOUBLE, data[i].plotstyle,
                                 VAL_NO_POINT, data[i].linestyle,
                                 1, data[i].plotcolor);
}

// Forcibly attach the cursor to a plot
// (the values of n and index could be saved between callbacks if necessary)
SetGraphCursorIndex (hMainPanel, MAIN_GRAPH, 1, data[n].plothandle, index);

// If using 2 y-axes, get the y-axis for the plot...
GetPlotAttribute (hMainPanel, MAIN_GRAPH, data[n].plothandle, ATTR_PLOT_YAXIS, &yAxis);
// ... and associate the cursor with that axis
SetCursorAttribute (hMainPanel, MAIN_GRAPH, 1, ATTR_CURSOR_YAXIS, yAxis);

// Now get the cursor position and use it however you need to
GetGraphCursor (hMainPanel, MAIN_GRAPH, 1, &xPos, &yPos);

// Make Graph visible
SetCtrlAttribute (hMainPanel, MAIN_GRAPH, ATTR_VISIBLE, 1);
...

Note that your control callback is executed before the cursor position is updated (which happens when the control receives the event). So, for proper operation once the user is able to interact with the graph, you may need to do this:

...
// In the control callback
case EVENT_COMMIT:
    PostDeferredCall (HandleCommitEventCB, (void *) someData);
    break;
...

In HandleCommitEventCB():
...
GetGraphCursorIndex (hMainPanel, MAIN_GRAPH, 1, &plot, &index);
// etc.
...

Cheers,
Colin.

0 Kudos
Message 3 of 3
(3,103 Views)