Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Draw a line connecting two points in WPF graph

Solved!
Go to solution

I'm trying to figure out the best way to draw a line between two existing points on my graph in WPF.  At first, I thought I had it working quite well; I just added another plot to my graph and gave it exactly two points to render.  But I quickly noticed that the app became strangely sluggish and the cpu was rather high (adding other plots doesn't affect the cpu usage as long as they have a full set of points (?)).  So I looked at using the DataToScreen method on the graph but I can't figure out what parameters it wants from me.  The second parameter is just an IList which doesn't tell me much.  This seems like the method I want to use to get the screen coordinates of a point on the graph.  

 

Does anyone know what the second parameter is supposed to be for the DataToScreen method?  Everything I've tried throws an Argument Exception.

 

Thanks,
Dan

0 Kudos
Message 1 of 3
(6,674 Views)
Solution
Accepted by topic author danxia

DataToScreen will return a screen coordinate in the plot area of a graph for the raw horizontal and vertical data value in the given list. The value parameter is an IList for compatibility with other graph query methods like FindNearestValue, which return IList values containing raw data.


As a concrete example, say you had a graph setup with an integer horizontal axis with a range from 0 to 100, and a double-precision vertical axis with a range from 10 to 20:


    <ni:Graph>
        <ni:Graph.Axes>
            </ni:AxisInt32 Orientation="Horizontal" Range="0,100,Int32"/>
            </ni:AxisDouble Orientation="Vertical" Range="10,20,Double"/>
        </ni:Graph.Axes>
    </ni:Graph>


To get the screen coordinate for the value at the center of the two axes, you could call:


    graph.DataToScreen(plot, new object[] { 50, 15.0 })


Note that the raw data values match the type of the axes (the horizontal AxisInt32 gets the integer 50, and the vertical AxisDouble gets the value 15.0).




You also mentioned a performance issue where "adding other plots doesn't affect the cpu usage as long as they have a full set of points". I made a simplistic test app that added an array of two points to a graph on every button press (e.g. graph.Data.Add(new[] { new Point(x1, y1), new Point(x2, y2) });), but could not reproduce the issue you were seeing. Would you mind sharing the code you are using to draw the connecting line?

~ Paul H
Message 2 of 3
(6,664 Views)

Thanks Phil!  You rock!  I'll try stripping away everying from my graph and see if I can reproduce the performance problem that I'm seeing (by the way, it didn't happen immediately; only after I interacted with the graph using a zoom in/out gesture).  I admittedly have a lot going on in the UI but I hadn't noticed any problems with responsiveness until I added that innocent 2-point plot.  Could be coincidence.  If I reproduce it in a stripped down app, I'll post it here.

 

Thanks again,

Dan

0 Kudos
Message 3 of 3
(6,655 Views)