cancel
Showing results for 
Search instead for 
Did you mean: 

WPF Graph - Get Clic position on Graph

SOLVED
Flow75
Member
Solved!

WPF Graph - Get Clic position on Graph

Message contains a hyperlink

Hello,

 

Four years ago, i posted a question on about to get the clic position on NI Scratter graph (https://forums.ni.com/t5/Measurement-Studio-for-NET/Maker-on-ScatterGraph-at-mouse-clic-position/m-p...)

 

Now I want to do the same but on wpf graph element.

 

Can you help me ?

Thx.

F.

6 REPLIES 6
phansen
NI Employee (retired)

Re: WPF Graph - Get Clic position on Graph

Message contains a hyperlink

I believe "how to set cursor on the position I clicked on the graph?" has the answer to your question: subscribing to the PlotAreaMouseLeftButtonDown event, you can use GetPlotAreaPosition to retrieve the click location (and then apply that screen position to a cursor or annotation child in the graph).

~ Paul H
Flow75
Member

Re: WPF Graph - Get Clic position on Graph

Well sort of, the previous post gave me the coordinates of the point in the graphic referencial not the position of the clic in pixel

phansen
NI Employee (retired)

Re: WPF Graph - Get Clic position on Graph

Message contains a hyperlink

For the pixel screen position, you can use the standard WPF GetPosition method on the MouseButtonEventArgs parameter in your event handler.

~ Paul H
Flow75
Member

Re: WPF Graph - Get Clic position on Graph

Message contains an image

Hello

 

Not the pixel screen position.

 

Flow75_0-1717393863454.png

 

For this screen, when I clic on the blue dot, I want X = 1 and Y = 1 and so on with the different color.

 

The winform version gave me an float value between 0 and 1 representing the position on the axis.

 

Thx.

 

phansen
NI Employee (retired)
Solution

Re: WPF Graph - Get Clic position on Graph

Ah, sorry for the confusion! It can be tricking when dealing with multiple coordinate systems (WPF screen pixels, screen positions within the plot area, scaled relative values, and data values).

 

To get the "X,Y" data position of a point, you can use the ScreenToData method on the plot area position. The returned values will match the type of the axes used by the specified plot. Assuming you have the default Axis<double> type, this would look like:

 

var plot = graph.AllPlots[0]; // (using the first plot in the graph for this example)
Point screen = graph.GetPlotAreaPosition(e);
IList data = graph.ScreenToData(plot, screen);
double x = (double)data[0];
double y = (double)data[1];

 

 

Note that this returns the equivalent data value for the pixel position of the mouse. If you want to find the closest point within your data, you can use FindNearestValue with the relative click position from ScreenToRelative:

 

Point screen = graph.GetPlotAreaPosition(e);
Point relative = graph.ScreenToRelative(screen);
var nearest = graph.FindNearestValue(plot, relative, new GraphQueryArgs(PlotsToSearch.Any));

 

~ Paul H
Flow75
Member

Re: WPF Graph - Get Clic position on Graph

Hi, 

 

Perfect ! 

 

Thank you for your help !