Assuming you have completed the previous tutorial, we will now make some changes to update the data dynamically.
Updating Data Values
In our ISCIDataSeries, we have some static data so far. Let’s update them in real-time now.
We are going to add a Timer and schedule updating the data on timer tick.
To update data in a DataSeries, we will need to call one of the available Update methods on that DataSeries.
Since we are using SCIXyDataSeries, we are going to use the -[ISCIXyDataSeries updateValuesX:y:at:] method.
But first of all, we need to adjust some previously created code and save DataSeries instances to be able update them later.
And since we are going to change a DataSeries setup, it worth mentioning that the code from the previous tutorials works, but it wasn’t very efficient:
Calling any of the Update methods triggers a chart update, which redraws the entire chart.
The values are passed in as NSNumber objects, which require boxing/unboxing, which slows down the process as well
No worries, in SciChart there is an easy way to improve that:
Make sure to always Append or Update data in a DataSeries in batches instead of one at a time.
Use one of the ISCIValues implementation such as SCIDoubleValues. It stores data in an primitive array internally and doesn’t requires boxing/unboxing.
The code from above can be updated as follows to append new data constantly to the dataSeries:
- (void)updateData {
NSInteger x = _lineDataSeries.count;
[SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
[_lineDataSeries appendX:@(x) y:@(sin(x * 0.1))];
[_scatterDataSeries appendX:@(x) y:@(cos(x * 0.1))];
// zoom series to fit viewport size into X-Axis direction
[self.surface zoomExtents];
}];
}
@objc fileprivate func updateData(_ timer: Timer) {
let x = lineDataSeries.count
SCIUpdateSuspender.usingWith(surface) {
self.lineDataSeries.append(x: x, y: sin(Double(x) * 0.1))
self.scatterDataSeries.append(x: x, y: cos(Double(x) * 0.1))
// zoom series to fit viewport size into X-Axis direction
self.surface.zoomExtents()
}
}
private void UpdateData(object sender, ElapsedEventArgs e)
{
InvokeOnMainThread(() =>
{
if (!_isRunning) return;
var x = lineDataSeries.Count;
using (Surface.SuspendUpdates())
{
lineDataSeries.Append(x, Math.Sin(x * 0.1));
scatterDataSeries.Append(x, Math.Cos(x * 0.1));
// zoom series to fit viewport size into X-Axis direction
Surface.ZoomExtents();
}
});
}
Scrolling Realtime Charts
What if you wanted to scroll as new data was appended? You have a few choices.
If you want to be memory efficient, and you don’t mind if you discard old data, you can use our FIFO (first-in-first-out) functionality.
Since updating VisibleRange is fairly self-explanatory, we are going to explain the FIFO method.
Discarding Data when Scrolling using FifoCapacity
The most memory efficient way to achieve scrolling is to use ISCIDataSeries.fifoCapacity to set the maximum size of a DataSeries before old points are discarded.
DataSeries in FIFO mode act as a circular - first-in-first-out - buffer. Once the capacity is exceeded, old points are discarded.
You cannot zoom back to see the old points, once they are lost, they are lost.
To make a DataSeries use the FIFO buffer, all you need to do is just set fifo capacity on the DataSeries, e.g.:
Of course, this is not the limit of what you can achieve with the SciChart iOS.
Our documentation contains lots of useful information, some of the articles you might want to read are listed below:
Finally, start exploring. The SciChart iOS is quite extensive.
You can look into our SciChart iOS Examples Suite which are full of 2D and 3D examples, which are also available on our GitHub.