From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF graph display point value on hover

Solved!
Go to solution

Hello,

 

Is there an easy way to display the value of a point (with similar formatting to a Cursor) when hovering the mouse over the point? I'm using the WPF graph control (MStudio 2013) with multiple plots graphed on an AxisDouble.(y axis) and AxisPrecisionDateTime (x axis). Let me know if you need any more information and thanks in advance.

0 Kudos
Message 1 of 8
(8,583 Views)
Solution
Accepted by topic author jsacks

When a Cursor is set to snap to data, it uses the FindNearestValue method to find the nearest plot value. When it is not set to snap to data, it reports the current screen value using the ScreenToData method. The code example below shows how to use both to display the current value under the mouse in a graph using a tool tip:


    private static readonly GraphQueryArgs query = new GraphQueryArgs(
        PlotsToSearch.Any, SearchDimensions.HorizontalAndVertical,
        SearchDirections.ForwardAndReverse, isInclusive: true );

    public MainWindow( ) {
        InitializeComponent( );

        graph.PlotAreaMouseMove += this.OnPlotAreaMouseMove;
        graph.PlotAreaMouseLeave += delegate { graph.ToolTip = null; };
        ToolTipService.SetInitialShowDelay( graph, 0 );
        ToolTipService.SetShowDuration( graph, int.MaxValue );
    }

    private void OnPlotAreaMouseMove( object sender, MouseEventArgs e ) {
        IPlot plot = graph.AllPlots[0];
        Point screenPosition = graph.GetPlotAreaPosition( e );
        IList pixelValue = graph.ScreenToData( plot, screenPosition );
        string tip = string.Format(
            "Absolute value at point {0} is {1}.",
            screenPosition, FormatValue( pixelValue ) );
        Point relativePosition = graph.ScreenToRelative( screenPosition );

        PlotValue nearestValue = graph.FindNearestValue( plot, relativePosition, query );
        if( nearestValue != null )
            tip += Environment.NewLine + string.Format(
                "Nearest value on {0} is {1}.",
                nearestValue.PlotObserver.Label,
                FormatValue( nearestValue.Value ) );

        graph.ToolTip = tip;
    }

    private static string FormatValue( IList values ) {
        return string.Join( ", ", values.Cast<object>( ) );
    }

~ Paul H
0 Kudos
Message 2 of 8
(8,575 Views)

Is there any way to make this work if the plot is a scattergraph?  In the code, graph is not defined and it turns out it is a Controls.Graph.  I have plots on Scattergraphs and I would love to be able to get the nearest point to the mouse and not be limited to requiring the user to click directly on a data point. 

 

Thanks,

Steve

0 Kudos
Message 3 of 8
(8,332 Views)

Is there any way to do this in WinForms?  It seems the point class required by FindNearestValue (systems.windows.point) is WPF specific. 

 

Steve

0 Kudos
Message 4 of 8
(8,330 Views)

This white paper might give you some useful information.http://www.ni.com/white-paper/10526/en/

National Instruments
Applications Engineer
0 Kudos
Message 5 of 8
(8,313 Views)

Looking through the list of ScatterGraph members, it looks like GetPlotAt may provide similar functionality in Windows Forms.


You may also want to post your Windows Forms question separately, instead of adding it to an answered WPF question, so that more people will see it.

~ Paul H
0 Kudos
Message 6 of 8
(8,306 Views)

Hello Paul,

 

i have a chart with multiple plot, i'm trying to get the point (X,Y) on i click on specific plot,

can you help me?

 

NB: i'm working with VB.NET

0 Kudos
Message 7 of 8
(6,270 Views)

Hi Akarim,

 

I'm going to suggest you make a new discussion about this since this thread was already solved and your question seems different. However, this seems like a good place to start.

https://www.linkedin.com/in/trentweaver
0 Kudos
Message 8 of 8
(6,228 Views)