Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Pixel position of a data point

Solved!
Go to solution

Hello all,

 

I would like to know where the mouse position location of a data point is relative to the graph. Basically, the reverse action of this person's post:

 

WPF graph display point value on hover

 

http://forums.ni.com/t5/Measurement-Studio-for-NET/WPF-graph-display-point-value-on-hover/m-p/243395...

 

Instead of getting data point,  would like the mouse position. I am overlaying shapes over the graph and would like to know where a specific point in the graph is in pixel location coordinates.

 

Thank you.

 

 


 

0 Kudos
Message 1 of 10
(3,964 Views)
Solution
Accepted by topic author gabe0

From your question, I assume you have a data value you want to translate (as opposed to a mouse event args, which already has a GetPosition method).

 

You can use the DataToScreen method to get a screen position within the plot area, and use the DesiredPlotAreaMargin to offset that screen position to reference the outer graph control (i.e. point.X += margin.Left; point.Y += margin.Top).

 

You can also use the DataToRelative method to get a relative position for a data point, and then position an object in the Children collection (much like a PointAnnotation😞

 

XAML

xmlns:ni="http://schemas.ni.com/controls/2009/xaml/presentation"
xmlns:niPrimitives="http://schemas.ni.com/controls/2009/xaml/presentation/primitives"
...
<ni:Graph x:Name="graph">
    <ni:Graph.Children>
        <Ellipse x:Name="dataMarker"
                 Width="10"
                 Height="10"
                 Stroke="Black"
                 StrokeThickness="2"
                 niPrimitives:RelativePanel.RelativeHorizontalAlignment="Center"
                 niPrimitives:RelativePanel.RelativeVerticalAlignment="Center"
                 />
    </ni:Graph.Children>
</ni:Graph>

Code

Point relative = graph.DataToRelative( dataValue );
RelativePanel.SetRelativeHorizontalPosition( dataMarker, relative.X );
RelativePanel.SetRelativeVerticalPosition( dataMarker, relative.Y );
~ Paul H
Message 2 of 10
(3,948 Views)

Thank you for the quick reply! One last question, how do I set the PlotAreaMargins?

 

Thanks,

0 Kudos
Message 3 of 10
(3,900 Views)

If you want to set the plot area margin, instead of just reading the desired value, you can use the PlotAreaMargin property.

~ Paul H
0 Kudos
Message 4 of 10
(3,896 Views)

Are the plot area margins the distance from the bounds of a plot to the edge of each Axes? I'm looking to get that offset value from the graph. 

 

Thanks,

Gabe

0 Kudos
Message 5 of 10
(3,893 Views)

There is a single margin for the "plot area", which is the central part of the graph where data is drawn. In addition to the plot area, the graph allocates space around the plot area to display the coordinate axes. The DesiredPlotAreaMargin property indicates the desired size for all the axes, but you can override the desired size by specifying an explicit PlotAreaMargin.

 

Could you explain what you mean by "from the bounds of a plot to the edge of each axis"? Are you referring to the "plot area", or to the data values for an individual plot? Could you describe how you are using the margin value?

~ Paul H
0 Kudos
Message 6 of 10
(3,890 Views)

The space around the plot area to display coordinate axes is what I mean by from the bounds of a plot to the edge of each axes. So you did answer my question, thank you. So, where can I set the plot area margin because I don't see the property anywhere.

 

Thanks.

0 Kudos
Message 7 of 10
(3,885 Views)

PlotAreaMargin is a property of the graph (i.e. "<ni:Graph PlotAreaMargin="42" ...>" in XAML, or "graph.PlotAreaMargin = new Thickness( 42 );" in code).

~ Paul H
0 Kudos
Message 8 of 10
(3,878 Views)

Okay, I'm using ni:Graph and it doesn't show up. I'm using MS2013, is this a new feature?

 

Thanks.

0 Kudos
Message 9 of 10
(3,876 Views)

Ah, yes: I believe this was added in Measurement Studio 2015.

~ Paul H
0 Kudos
Message 10 of 10
(3,874 Views)