Andre de Cavaignac

Let's blog it out...

Live Updating Line Graph in WPF

WPF Live Line Graph

See a ClickOnce (.NET 3.5) Sample Here

Download the Source Code (VS 2008 Beta 2) Here

A live, updating graph in Windows Presentation Foundation and .NET 3.5 is a natural use of the new framework, and is something that WPF is well suited to do.  You can make great visually appealing animated components, and a graph is just one example.

 In this example, I have built an attractive, glassy (Vista style)  control that presents an array of points ticking accross a graph in real time.  The goal was to use this with a market simulator so that you could see the data that the simulation was spitting out.  The graph uses the ticks as its concept of "time" in the market, and can readjust its speed based on the speed of the simulator.  Cool stuff.

The graph always ticks from the right-most side to the left-most side, with the right-most side being "now" in the market.  This is not a simple task, because with the market speed consistently changing, you need to constantly re-adjust your animation speed to ensure that the right most side is always where the next point appears.

Key Points 

  • The graph "glows" in intensity based on how far off the median value you are (not zero)
  • A moving average is used to determine how fast the graph is currently moving, and this is a good example of readjusting animation speed to outside variables
  • The control resizes dynamically, the Grid was used to allow most of the resizing, with some code to remove UI elements that are "in the way" when the graph gets really small
  • ControlTemplate were used to style buttons and other elements used in making the graph appealing
  • Extension properties were used to extend Button with additional properties that could be used to find graph glow intensities.
  • The graph duration (time from start to end of the view area) can be changed by using the slider at the bottom of the application

 

Creating Your Own Data Source

Care to use this in your project?  Making the graph use your own data source is simple.  Derrive from TickerAdapter class (if you want to see an example look at the RandomTickerAdapter that is supplied in the demo).  Add new securities to the collection by calling TickerAdapter.AddSecurity, and add new ticks by calling TickerAdapter.NotifyNewTick.

That's it!

 

Known Issues

This control was originally made for a demo, not production use.  Although it is near production ready, some issues exist.

  • The MiniTicker.Ticks collection is never cleared
  • The Security.Ticks collection is never populated (and not used)
  • There is no way to remove a ticker from the collection of tickers managed by a TickerAdapter.
  • Some performance issues exist when adjusting the time scale.  There is no "resolution" of the graph, so if you push the graph to a high duration, all the ticks show, rather than dropping some of the ticks for the scale selected.  This is both easy to fix, and not an issue if you're only showing a time window

See a ClickOnce (.NET 3.5) Sample Here

Download the Source Code (VS 2008 Beta 2) Here

Comments

Daniel Chait said:

Andre - These look great.  However I tried the clickonce - got this error:

=====

System Update Required

Unable to install or run the application. The application requires that assembly System.Core Version 3.5.0.0 be installed in the Global Assembly Cache (GAC) first.

Please contact your system administrator.

=====

I have .NET 3.5 (and Orcas) installed, what else do I need to do.

# August 25, 2007 10:26 PM

Vince said:

Very nice UI. Thanks for putting this together.

# October 17, 2007 10:30 PM

Michael Casey said:

Andre,

Very cool application...

I'm trying to see if I can convert this into a WPF UserControl for the purpose of embedding within a WinForms application.  Although I can get it to compile, I'm not able to view the controls in design mode, because it cannot find the resources defined within the App.xaml.  Therefore, my question is how to define references to the app resources, when the hosting application is not a WPF application?  Otherwise, is there someway to revise how the resources are referenced from the other XAML files, such that it doesn't rely upon App level resources?

# October 18, 2007 3:10 AM

Rudi Grobler said:

This control package is for anybody who have been wondering if WPF is ready for the enterprise. WPF has

# February 2, 2008 9:44 AM

Pointernil said:

I really dont want to ruin the fun, but the sample app consumes 60% CPU on my T7200 powered intel core 2 notebook (which does have a x1400 grfx card)

Can this be optimized somehow?

# March 3, 2008 10:31 PM

Christopher Steen said:

Link Listing - March 4, 2008

# March 5, 2008 1:28 PM

radioactive said:

I wish to ask that how to modify the component to receive the tickers second by second?

thank you.

# March 8, 2008 6:29 PM

radioactive said:

hi may i know how to change the thing to be updating second by second?

thank you

# March 8, 2008 7:18 PM

WPF Dev - WPF Line Graph Application said:

Pingback from  WPF Dev -   WPF Line Graph Application

# April 7, 2008 6:44 PM

WPF Dev - WPF Line Graph Application said:

Pingback from  WPF Dev -   WPF Line Graph Application

# April 7, 2008 6:44 PM

mrdecav said:

The answer to the optimization question is yes and no.  There is some optimization work that could be done, but overall, all the animation going on and replacement of the line is taking its toll.

Although the consumption seems like alot, what is interesting is even when you add more items (be it graphs or more complex visual effects) it does not seem to slow down.

The first version of WPF is not particuarly optimized when sending rendering commands to the graphics hardware.  The next version (or perhaps service pack?) is intended to fix this problem.  When that comes out, I think you'll see a good performance boost with this app.

# May 9, 2008 7:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)