IMPORTANT! Ensure Data is Sorted in the X-Direction
The number one fundamental for high performance in SciChart is to ensure that data is sorted in the X-Direction increasing. As of SciChart 3.0 and above, this is not mandatory, but several key algorithms such as HitTest, Indexing and Resampling require that data is sorted in the X-Direction for optimum performance. With unsorted data, SciChart will still render, but it will do so using vastly inferior algorithms.
Since SciChart v6 we have vastly improved the performance of charts with unsorted data, however, with sorted, you can achieve 1-Billion data-points. With unsorted, about 10,000,000 points.
We recommend if you can, sort.
To enable unsorted data
SciChart will throw an exception if you append unsorted data to a DataSeries. This is intended to warn you and prevent unintentional appending of unsorted data to a DataSeries.
Since unsorted data is detrimental to performance, in SciChart v4+ we now throw an InvalidOperationException if you append or insert data which causes the data-series to be unsorted in the X-Direction. If you upgrade and see this exception, and you intend to have unsorted data in a data-series, you can disable the error by setting the following property:
To enable unsorted data |
Copy Code
|
---|---|
// Disables exception on append unsorted data // Please note, data unsorted in X is bad for // performance of large datasets. Don't do it // unless you need to! DataSeries.AcceptsSortedData = true; |
Batch Appends to the DataSeries, using the overloaded API
Batching appends using the overloaded API to append IEnumerable, IList or Arrays has a massive impact on DataSeries performance. Arrays have the biggest impact as these can be indexed using unsafe code. If you have a lot of data to append, create a small buffer (say 10-100 points) and append them in blocks.
Batch Appends to the DataSeries, using the overloaded API |
Copy Code
|
---|---|
double [] xBuffer = new double[100]; double [] yBuffer = new double[100]; for(int i = 0; i < 100; i++) { xBuffer[i] = GetNextXValue(i); yBuffer[i] = GetNextYValue(i); } // Single recalculation of Min, Max and Single // redraw call is executed when appending a block // Appending arrays is much faster than IEnumerables dataSeries.Append(xBuffer, yBuffer); |
NOTE: Why does this work? When data is appended in blocks, you get a single draw call at the end. You also reduce thread-contention if you are appending on a background thread and drawing on the UI thread. Finally, the above is simply more memory efficient as we only need to recalculate the additional memory required once, rather than per-point appended.
SciChart processes data so fast, we can process >100,000 updates to the DataSeries per second. However, if you update that fast, then SciChart will spend a lot of time in static overhead for dataseries updates. It's better for performance to update more data, less often.
Batch updates inside a SciChartSurface.SuspendUpdates() using block
When performing multiple updates, such as VisibleRange changes, DataSeries changes and RenderableSeries or Axis Property changes, each change can potentially trigger a redraw in SciChart. To prevent this, use the methods from the ISuspendable implementation on SciChartSurface.
Batch updates inside a SciChartSurface.SuspendUpdates() using block |
Copy Code
|
---|---|
using (sciChartSurface.SuspendUpdates()) { dataSeries.Append(x1, y1); dataSeries.Append(x2, y2); dataSeries.Append(x3, y4); sciChartSurface.YAxis.VisibleRange = new DoubleRange(200, 300); sciChartSurface.XAxis.VisibleRange = new DoubleRange(-10, 10); } // Single redraw call is executed on exit of using block |
Batch Add Annotations if UIElement Annotations are Slow
When appending lots of annotaitons you will notice WPF really slows down and is unable to cope with more than a few thousand annotations.
If you need to append a large amount of annotations to a SciChartSurface, we recommend manipulating a new AnnotationCollection and attaching the whole collection to the chart, as opposed to manipulating the SciChartSurface.Annotations collection one by one.
Batch Add Annotations if UIElement Annotations are Slow |
Copy Code
|
---|---|
using (sciChartSurface.SuspendUpdates()) { // Create temporary AnnotationCollection var myAnnotations = new AnnotationCollection(); double minx = double.MaxValue; double maxx = double.MinValue; for (int i = 0; i < 1000; i++) { var a = new BoxAnnotation { // Assign X1,Y1, Background etc... }; myAnnotations.Add(a); } // Reassign annotation collection sciChartSurface.Annotations = myAnnotations; sciChartSurface.ZoomExtents(); } |