10-19-2016 05:49 PM
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
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.
Solved! Go to Solution.
10-20-2016 10:48 AM
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 );
10-26-2016 11:14 AM
Thank you for the quick reply! One last question, how do I set the PlotAreaMargins?
Thanks,
10-26-2016 11:18 AM
If you want to set the plot area margin, instead of just reading the desired value, you can use the PlotAreaMargin
property.
10-26-2016 11:22 AM
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
10-26-2016 12:27 PM
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?
10-26-2016 12:43 PM - edited 10-26-2016 12:46 PM
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.
10-26-2016 12:56 PM
PlotAreaMargin
is a property of the graph (i.e. "<ni:Graph PlotAreaMargin="42" ...>
" in XAML, or "graph.PlotAreaMargin = new Thickness( 42 );
" in code).
10-26-2016 12:58 PM
Okay, I'm using ni:Graph and it doesn't show up. I'm using MS2013, is this a new feature?
Thanks.
10-26-2016 01:09 PM - edited 10-26-2016 01:10 PM
Ah, yes: I believe this was added in Measurement Studio 2015.