LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sample code to match mouse position with plot point?

Hi all you experts,
 
I have an application that plots several types of data vs. time.  My client would like to be able to move the mouse over any point in time and have the application pop up the y-axis data for that point of time in the plot.
 
The data is being plotted from an array, so I have all the information.  What I need to figure out is how to convert the x-y position of the pointer to the time element of the array.  I can get the position of any plot then figure out where in the plot the pointer is, but this is relative to the x-y position of the plot, not the x-y origin of the plot.  The x-y origin of the plot can move inside the panel based on the left and right y-axis labels and scaling.
 
Does anyone have any sample code or can you point me somewhere where I can figure out how to do this?
 
Thanks!
 
Dave
0 Kudos
Message 1 of 9
(4,821 Views)

It sounds like you might be able to put the built-in cursors in graph controls to good use. You can have a cursor that snaps to the nearest data point and easily determine which data point it is. Check out ..\samples\userint\graphcursors.prj, and take a look at all the various forms and behaviors that the cursors can have.

Hope this helps,

Ian

0 Kudos
Message 2 of 9
(4,802 Views)
Below is a somewhat old example I wrote that does essentialy what you are asking. It is somewhat complex since it works for most plot types (including intensity plots) but it should be a good starting point. I should point out that since this example was written (and to some extent, because of it), some features have been added to CVI would make recreating it much easier. For example, a GetGraphCoordsFromPoint function was added to convert pixel coordinates within the plot area to "graph" coordinates (a job this example does by hand). Also, a mouse pointer move event was added, making the use of extended mouse events unnecessary.

At any rate, I hope you find it useful.

http://zone.ni.com/devzone/cda/epd/p/id/4869

Regards,

-alex

Message Edited by Alex D on 05-31-2007 05:19 PM

Message Edited by Alex D on 05-31-2007 05:19 PM

Message Edited by Alex D on 05-31-2007 05:20 PM

0 Kudos
Message 3 of 9
(4,793 Views)

Thanks Ian and Alex,

I tried the graph sample project, but apparently the "snap to" feature does not work when you use PlotLine rather than PlotY.  I've asked NI about this but don't expect to hear back until next week.

I'll keep trying the other suggestions.

Dave

0 Kudos
Message 4 of 9
(4,768 Views)
Hi Dave,

What you are seeing is indeed expected behavior. In the CVI help section "ATTR_PLOT_SNAPPABLE", notice the plot types remark:
Plot Types: Point, polygon, waveform, X, XY, Y.

Basically, snapping will not occur for plots created using PlotLine and PlotRectangle, etc., as these are interpolated plots with no user defined points to snap to.

LuiS confirms this behavior on another post here (last message).

Regards,
0 Kudos
Message 5 of 9
(4,725 Views)
Hi Dave.

Would this work for you / your client?

From your source data, generate a new array for each variable. Include the original points and linearly-interpolated points at suitable intervals between them. Generate the plots from the new data using PlotXY(). If your time values are of integral type, you may need to convert them to float or double to make this work.

Regards,
Colin.
0 Kudos
Message 6 of 9
(4,709 Views)

Hi Colin,

Thanks for the response.  There is a piece of the puzzle missing (sorry).  That is, I can plot up to 72,000 points.  Each point is plotted as the data is received.  Snap to only works with PlotXY (and maybe the other plot functions too) if you plot all the data at once.  So I think the standard NI functions aren't going to be able to do it for me.  I'm going to take a look at what Alex mentioned and see if there is something there I can use.

Thanks again.

Dave

0 Kudos
Message 7 of 9
(4,705 Views)

Well, this must be combined with your need for execution speed...

You could:

1. Allocate an array when your process starts
2. Keep an index of how many data you collected

Processing loop:
   3. Acquire one measure and append to the array (index++)
   4. Delete all plots from the graph
   5. PlotXY the whole array for <index> samples

Delaying the DeleteGraphPlot will improve user interface performance since the graph is update only once on PlotXY.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 9
(4,686 Views)

Thanks Roberto.  After talking with James today, it doesn't appear that the standard cursor functions for a graph will work for me.  I have too many data points and my data is arriving far too quickly (as fast as every 50 msec) to delete the graphs and redraw them from the beginning for each point.  Without plotting the entire graph, the cursor functions won't give me what I want.

I'm going to try to capture the mouse position and convert that into an index on my x-axis.

Dave

0 Kudos
Message 9 of 9
(4,662 Views)