I'm working on a diagnostic tool and receives data every 25 ms. I need this data to be drawn in my chart using a lineSeries. I'm using a a wpf chart with a lineSeries which I bind in xaml to an ObservebleCollection.
The problem is that I need the collection needs to contain atleast 1600 datapoints before starting to remove them at the front.
I understand that 25 ms is a short tiem then it comes to drawing in wpf. Dose anyone have any solution to my problem?
Regards
You should check out SciChart, which can handle data updates at 1ms. It renders like a game-loop, so only draws when new data is appended and the UI thread is free. Some more information at www.scichart.com/wpf-chart-features which shows the features and talks about performance.
Disclosure: It's my own component, so ask any questions if you have them!
I'd suggest you use a custom made drawing routine utilizing WritebleBitmap instead of shapes and draw your lines in a fixed area one segment at a time without any scrolling. The, when you reach 1600 points at the right side of your plot area, start over. Maybe some visual indicator (like vertical line or something) would help perception.
I don't think there any charting libraries targeted at updating 1600 point line every 25ms.
I have a .NET application that charts many measurements on a 33ms interval. I tested a number of charting solutions to accomplish this task, but the only real viable option I found was Arction LightningChart (http://www.arction.com). Lightning Chart will do what you want pretty easily, and I think you can get the basic version for free. The downside will be a dependency on DirectX and the SlimDX library. The trial that you can download from the site will give you pretty good examples of how to use this control in WPF and Windows Forms.
Use Microsoft Chart for WinForms using the WindowsFormsHost control. MS-Chart can handle such data rates well. It internally uses the WritableBitmap method, so it is both fast and stable. Also, don't forget to set the series type to FastLine to avoid slowdown.
I've created a "Tip" article on codeproject which tries to highlight this type of high-data volume usage on MS-Chart. You can change the timer interval to even 1 ms. and see that there is no issue in showing the data.
The article is at http://www.codeproject.com/Tips/1006180/Using-Microsoft-Chart-in-WPF
Related
I'm currently looking at writing a sound wave visualisation control for WPF. In fact it can render any sort of line graph data; it doesn't have to be a sound wave. As long as the data is a discrete set of samples it will render it.
I have it working; however the performance is not that desirable. I have done a lot of code optimisation to make the OnRender method work quickly. I've tested it with a profiler and it shows it runs at around 110ms, which should be fine.
However I'm seeing the application stall a lot and the profiler isn't showing why.
During my tests I've noticed it may have something to do with fill rate. By that I mean the number of pixels being drawn. My test data consists of a sin wave, at 41000hz over 20 seconds. This produces 812000 samples. Now my control optimises this data depending on the zoom scale the user is viewing the wave at. The entire wave will be drawn using around 6000 lines draws.
If I zoom a long way into the wave so I only render two full sin waves, I still do around 6000 line draws. The CPU time is around the same, at 110ms but the application is smooth and doesn't stall, which seemed strange at first.
However when looking at it the full wave draw touches almost every pixel. The chart draws a green line, which when zoomed out overwrite the entire background of the graph control. When zoomed all the way in only a small amount the background is overdrawn.
I work as a game programmer so I recognise this issue could be caused by fill rate limits. If the line draw is touching every pixel then it becomes slow to draw, irrespective of the number of line draws. However I would not expect this to be the case as the graph isn’t that large on screen. If I change the size of the window then it does get slower, which again reinforces by guess at fill rate issues. A modern graphics card should be able to handle this so maybe the control is using software rendering. I'm not sure how I can prove that!!
So my OnRender method is pretty optimised as far as the logic goes. In both of the above cases the time it takes to draw, on the CPU, doesn't change much. However in some cases there is a terrible lag of around one second if a lot of pixels are touched.
Does anyone know how I could improve this?
One way I have considered is to render an off screen texture on another thread and once complete, then call InvalidateVisual once the texture has been updated. I could also do the invalidation every so often so the graph rendering updates over time, rather than just suddenly appears.
Anyone have experience of this sort of thing and how do I actually profile the internals of WPF?
Does anyone know a profiler that will show me what is actually causing this, but as I said I think it maybe down to the actually hardware render and a fill rate problem.
Just to note, my graph control inherits from Canvas and the line draw is done using StreamGeometryContext. I also freeze the geometry and all the brushes and pens.
Thanks
I write software that needs to graph large amounts of frequency sprectrum data very fast. WPF's retained graphics system isn't very good for drawing (graphing) large amounts of data that is changing frequently. We use D3D9 to draw our graphs. WPF provides a way to get this into their rendering system through the D3DImage class where it can become an ImageBrush. Thus, it avoids problems relating to airspace restrictions at the cost of a little performance, but will still be much faster than rendering with WPF objects.
There are also two very good graphing libraries that I have seen out there that have different pros/cons.
SciChart is actually a software based rendering charting tool for WPF but has good performance.
Lightning Chart Uses DirectX (Via SlimDX library which is a managed .NET wrapper) and is extremely performance oriented.
Regarding your question about software rendering... The WPF Performance Suite has profiling tools and overlays that can show which portions of your application are being software rendered. You may want to download it from Microsoft and give it a try.
From what I've read, WPF's rendering system is hardly optimal (A Critical Deep Dive into the WPF Rendering System). For what WPF is designed for, it is great, but it has its limits as well.
My recommendation is that if you have experience in game development and DirectX, and you need a method to graph larger amounts of data quickly, to look through interop with D3D via the D3DImage class in WPF.
In our WPF application, we have a need to display about 64 real-time level meters for an audio application. The tests we've thrown at WPF, even when rendering basic primitives as efficiently as we can still show it to be nowhere near where our application needs to be, often times bogging down the main thread so much to the point that it's non-responsive to input.
As such, we have to go with something more optimized for graphics performance such as DirectX (via SlimDX or SharpDX) or OpenGL/ES (via Atlas which converts it to DirectX calls.)
My question is if it's possible to create multiple, small DirectX-based areas, each representing an individual meter, or for that matter, is that even the right approach? I was under the understanding that you have to run it as at a minimum, the entire window, not a portion thereof.
The issues I see with the latter are airspace issues wherein you can't have WPF content in front of DirectX content in the same window, and we really don't want to have to redo all of our controls in DirectX since for the other non-meter 95% of our UI WPF is great!
I have read that you can render DirectX to a brush, then use that inside WPF, or using the WriteableBitmap class which gives you direct access to the buffers WPF then uses in its Render thread, both of which don't seem to suffer from the Airspace issues, but that seems we'd be right back at the same place with WPF being the bottleneck since it still has to do the rendering.
We are of course going to dedicate a few weeks to sample applications testing all of the above, but I'm wondering if I'm even headed in the right direction, and/or if there are any caveats we can avoid by talking to people with experience doing something like this to avoid common pitfalls, etc. As such, any comments will be appreciated.
I'm hoping we can perhaps even start a wiki somewhere to discuss this topic as it seems to be a popular one, albeit spread all over the place making it hard for new entrants to get the information they seek.
With wpf / d3d interop, You should always try to create the smallest number of interop calls. So you should prefer rendering all 64 level meters in a single render target (also it allows you to batch your primitive rendering and draw everything in the smallest number of gpu calls).
you should try to use the D3DImage API that allows you to share your own D3D texture with the wpf renderer.
If WPF can't really handle these 64 moving bars, you could go with a single D3DImage and use Direct3D9 for rendering all bars at once directly to it. For your specific scenario, you shouldn't have any performance problem.
I have my main window at something like 6000 x 6000 px. In that window, I can have a lot of controls (more than 5000) and we can zoom/pan where we want.
I added 10 "television screens" controls in my application, and each of them have a refresh rate of 100ms.
When I start this, everything crash...
10 objects with a refresh rate of 100ms each is too much, WPF cannot draw them at the time. I conclude that WPF is very slow to draw what I have....
Everything is drawn in vector, but I tried to add .CacheMode on them and it doesn't work either..
What can I do?
P.S. My PC is 8 core, 8 GB Ram, 256 Mb video card (nvidia quadro nvs 295) on win7 64bits
WPF uses software rendering in case you exceed maximum supported resolution of you graphics adapter. Moreover, 5k controls! Incredible. WPF is not a silver bullet for everything you imagine. You need to redesign your app or change technology you're using.
Work on reducing the number of controls in your application. 5K is quite a bit, and I think there is probably a way to reduce that number.
For example, use TextBlocks instead of Labels as TextBlocks render as 1 control while Labels render as 3 in 4.0 (I think its more than 3 in older versions)
Also, check and see if you can use UI Virtualization with some of your controls. Something like a ListBox of 100 items can be virtualized to only render 10 of them at a time.
Take a look at ZoomableCanvas - 1 million items. This is the best example out there of what WPF can do when using every optimisation in the book. I suggest you take that example and run it at the maximum resolution on your monitors. If that fails to perform well, then WPF cannot do this.
I was unaware that WPF default to software rendering if the resolution exceeds that of the video card. A good caveat to know about!
Regarding high performance graphical APIs I second Keiren and say use DirectX (SlimDX provides a managed wrapper) instead.
I've been looking into Silverlight charting controls to display a large number of samples, (~10,000 data points in five separate series - ~50k points all up).
I have found the existing options produced by Dundas, Visifire, Microsoft etc to be extremely poor performers when displaying more than a few hundred data points.
I believe the performance issues with existing chart controls is caused by the heavy use of vector graphics.
Ergo one solution would be a client-side chart control that uses the WritableBitmap class to generate a raster chart.
Before I fall too far down the wheel re-invention rabbit hole - has anyone found a third party or OSS control that will manage large numbers of data points on a sparkline?
Check out Visiblox ( http://www.visiblox.com/ ) Charts. I'm working on a telemetry application at the minute which uses three of their charts in the same Silverlight component, plotting about 36,000 points in total, and there is very little slowdown in terms of performance, if any at all.
They have a high performance example on their website here - http://www.visiblox.com/examples/LargeDataSets. I chose to use Visiblox after loooking at the following blog post:
New performance comparison: http://www.visifire.com/blog/2011/12/02/fast-silverlight-charts/
Old performance comparison: http://www.scottlogic.co.uk/blog/colin/2010/12/visiblox-visifire-dynamicdatadisplay-charting-performance-comparison/
My telemetry example is now part of a CodeProject article that I have created, if you want to take a look!
Based on the recommendations here, I have just trialled Visiblox and Infragistics.
Visiblox has a good developer experience, clean APIs, no bloat. While the performance is OK for a few thousand datapoints, performance breaks down as you go into the 100k range. Here's the Visiblox sample with a few thousand points (you can download the sample and just tweak the number of datapoints).
Now, the common argument here is that you should not bind that much data to a chart anyway, but should do some trimming beforehand. I do agree with that, but ideally, I want a chart control that does take care of that for me. Getting Zooming, Panning and all that right are non-trivial tasks and I'd be happy to shell out a few 100$ to get that functionality. It's plain economic common sense to not reinvent the wheel here.
Infragistics certainly gets that right. I can bind a million odatapoints and get smooth peformance. Here's the Infragistics sample.
However, Infragistics installs a lot of junk (a local IIS web app with the samples you can also see online) and besides the (excellent) samples and the code-level documentation, I do miss some high-level introduction to each component and it's individual modules.
Since a lot of people on SO recommend Telerik controls for their clean APIs, Documentation and Developer Support, I did also take a look at their chart control. However, I must admit I find it rather slow (Zooming takes roughly 500-1000ms, which is too long to feel smooth).
Here's the Telerik sample.
Visifire is the fastest chart now. Check out the blog link below.
http://www.visifire.com/blog/2011/12/02/fast-silverlight-charts/
The Chart image says Visifire is 3 to 4x faster than Visiblox..!
Before you start playing around with WritableBitmap on your own take a look at WriteableBitmapEx from René Schulte, he blogs about it here. Awesome stuff.
I don't know of any OSS or 3rd party controls that'll do what you need.
The one case where I've had the need for 100K+ of datapoints, we made it work by keeping it simple. The biggest perf killer was the number of elements within each datapoint. By keeping the element a simple ellipse with a static brush fill the app was rather snappy.
Infragistics claims to be able to handle massive amounts of data in their recently released xamDataChart.
http://www.infragistics.com/dotnet/netadvantage/silverlight/data-visualization/xam-web-data-chart.aspx#Overview
I believe the Telerik sample you are looking for can be found here. It shows large amounts of data that you can actually zoom in/out of. They also have a Server Load "Performance" Demo as well. Here is an example of the Scatter Chart with hundreds of data points.
Have you looked at the Telerik charting controls? Here is a link to one of their demos where they are using 100.000 data points:
http://demos.telerik.com/silverlight/#Chart/Sampling
I should also mention that I have not used the charting controls myself, so I cannot vouch for them. But the demos look good to me.
EDIT:
The link above has gone stale. Here is a link to the latest incarnation of the Telerik charting control They call it ChartView now:
http://demos.telerik.com/silverlight/#ChartView/FirstLook
Approaching Silverlight development is a rather daunting task as it seems to require a rather different mindset to work I have done in the past.
I have been working on it for several months and we have already released an application that presents form-based pages. So I have the basics of XAML for layout but what I need to do now is move into graphically representing data. For example transform a list of objects representing vehicle speed recordings into a line graph of speed. I am at a loss on what the best way is to approach this.
Can anyone point me to articles or tutorials that present this kind of thing?
Your first port of call for Silverlight learning should be the official site http://silverlight.net/Learn/
If you want to do any data visualization/charting then first try the Silverlight Toolkit on codeplex. It's fantastic if you want to get anything up and running quickly.
Also check out Delay's Blog on charting and the chartbuilder code
Bang your head against it for 3-6 months. That's how I did it and it's worked out pretty well so far.
But seriously, the learning curve sucks.
There's charting libraries for Silverlight out there, you could grab one of those but I wouldn't waste money on it. It's relatively easy to write this kind of code yourself.
All you really need is a DrawingVisual. Once you have that you can render what you need on to it's surface. The trick is to make sure that you have sufficient layout information when you render. Because this is vector graphics, you can use the ScaleTransform to match your content bounds instead of repainting on size changed. Other than that, you'll wanna host your DrawingVisual in a UIFrameworkElement and let the dimension of that object govern where and how you draw your data. This will give you all the layout goodness of WPF/Silverlight.
For drawing there are plenty of Geometry classes you can rely on but there's one thing that you'll wanna do and that's to adjust the level of detail in your data points with respect to your drawing. This is the number one trick to make sure you don't hog the CPU.
Avoid drawing more than one data point per pixel. If you have a lot of data points, and a small drawing surface you can use a rolling average to smooth the result.
If you approach this with the above things in mind you should be able to write a flexible graph UI element that you can visualize data with, in no time at all.
I did this in a WPF application, I'm pretty much assuming that you can do the exact same thing with Silverlight 2.0, you'll just yell at me if you cant?