Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF Graph - Get Clic position on Graph

Solved!
Go to solution

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.

0 Kudos
Message 1 of 7
(394 Views)

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
0 Kudos
Message 2 of 7
(345 Views)

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

0 Kudos
Message 3 of 7
(323 Views)

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

~ Paul H
0 Kudos
Message 4 of 7
(317 Views)

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.

 

0 Kudos
Message 5 of 7
(294 Views)
Solution
Accepted by topic author Flow75

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
Message 6 of 7
(283 Views)

Hi, 

 

Perfect ! 😁

 

Thank you for your help ! 

0 Kudos
Message 7 of 7
(262 Views)