Pre loader

iOS & macOS Chart Series Performance Demo

iOS & macOS charts - Examples

Please note! These examples are new to SciChart iOS v4 release! SciChart’s OpenGL ES and Metal iOS and Metal macOS Chart library ships with hundred of Objective-C and Swift iOS & macOS Chart Examples which you can browse, play with and view the source-code. All of this is possible with the new and improved SciChart iOS Examples Suite and demo application for Mac, which ships as part of the SciChart SDK.

Download Examples

Demonstrates the speed and power of SciChart’s iOS Charts when hundreds of series are added to the chart. High-performance is very important for charts used in Trading, Medical and Scientific applications.

With the help of SCIFastLineRenderableSeries and ISCIRange that adds animation by changing axis’ visible range it shows how in realtime visible range of Y axis is changing and triggers redraw of line series.

The Swift and Objective-C source code for the iOS and macOS Chart Series Performance Demo example is included below (Scroll down!).

Did you know that we have the source code for all our example available for free on Github?

Clone the SciChart.iOS.Examples from Github.

Also the SciChart iOS and Scichart macOS Trials contain the full source for the examples (link below).

DOWNLOAD THE IOS CHART EXAMPLES

PerformanceDemoView.m
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// PerformanceDemoView.m is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
//******************************************************************************

#import "PerformanceDemoView.h"
#import "SCDToolbarItem.h"
#import "SCDRandomUtil.h"
#import "SCDMovingAverage.h"
#import "SCDToolbarButtonsGroup.h"

static int const MaLow = 200;
static int const MaHigh = 1000;
static double const TimeInterval = 10.0;
static int const MaxPointCount = 1000000;

@implementation PerformanceDemoView {
    SCITextAnnotation *_annotation;
    SCDMovingAverage *_maLow;
    SCDMovingAverage *_maHigh;
    
    NSTimer *_timer;
    BOOL _isRunning;
}

- (void)commonInit {
    [super commonInit];
    
    _maLow = [[SCDMovingAverage alloc] initWithLength:MaLow];
    _maHigh = [[SCDMovingAverage alloc] initWithLength:MaHigh];
}

- (void)initExample {
    id<ISCIAxis> xAxis = [SCINumericAxis new];
    xAxis.autoRange = SCIAutoRange_Always;
    
    id<ISCIAxis> yAxis = [SCINumericAxis new];
    yAxis.autoRange = SCIAutoRange_Always;
    
    id<ISCIRenderableSeries> rSeries1 = [self createRenderableSeriesWithColorCode:0xFF4083B7];
    id<ISCIRenderableSeries> rSeries2 = [self createRenderableSeriesWithColorCode:0xFFFFA500];
    id<ISCIRenderableSeries> rSeries3 = [self createRenderableSeriesWithColorCode:0xFFE13219];
    
    _annotation = [SCITextAnnotation new];
    _annotation.x1 = @(0);
    _annotation.y1 = @(0);
    _annotation.padding = (SCIEdgeInsets){ .left = 5, .top = 5, .right = 0, .bottom = 0 };
    _annotation.coordinateMode = SCIAnnotationCoordinateMode_Relative;
    
    _annotation.fontStyle = [[SCIFontStyle alloc] initWithFontSize:14 andTextColor:SCIColor.whiteColor];
    
    [SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
        [self.surface.xAxes add:xAxis];
        [self.surface.yAxes add:yAxis];
        [self.surface.renderableSeries add:rSeries1];
        [self.surface.renderableSeries add:rSeries2];
        [self.surface.renderableSeries add:rSeries3];
        [self.surface.annotations add:self->_annotation];
        [self.surface.chartModifiers add:[SCDExampleBaseViewController createDefaultModifiers]];
    }];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:TimeInterval / 1000.0 target:self selector:@selector(updateData:) userInfo:nil repeats:YES];
#if TARGET_OS_OSX
    if (_timer) {
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
#endif
    _isRunning = YES;
}

- (void)updateData:(NSTimer *)timer {
    if (!_isRunning || [self getPointsCount] > MaxPointCount) return;
    
    SCIIntegerValues *xValues = [[SCIIntegerValues alloc] initWithCapacity:self.pointsCount];
    SCIFloatValues *firstYValues = [[SCIFloatValues alloc] initWithCapacity:self.pointsCount];
    SCIFloatValues *secondYValues = [[SCIFloatValues alloc] initWithCapacity:self.pointsCount];
    SCIFloatValues *thirdYValues = [[SCIFloatValues alloc] initWithCapacity:self.pointsCount];
    
    SCIXyDataSeries *mainSeries = (SCIXyDataSeries *)[self.surface.renderableSeries itemAt:0].dataSeries;
    SCIXyDataSeries *maLowSeries = (SCIXyDataSeries *)[self.surface.renderableSeries itemAt:1].dataSeries;
    SCIXyDataSeries *maHighSeries = (SCIXyDataSeries *)[self.surface.renderableSeries itemAt:2].dataSeries;
    
    id<ISCIMath> xMath = mainSeries.xMath;
    id<ISCIMath> yMath = mainSeries.yMath;
    int xValue = mainSeries.count > 0 ? [xMath toDouble:[mainSeries.xValues valueAt:mainSeries.count - 1]] : 0;
    float yValue = mainSeries.count > 0 ? [yMath toDouble:[mainSeries.yValues valueAt:mainSeries.count - 1]] : 10;
    for (int i = 0; i < self.pointsCount; i++) {
        xValue++;
        yValue = yValue + randf(0.0, 1.0) - 0.5;
        [xValues add:xValue];
        [firstYValues add:yValue];
        [secondYValues add:[_maLow push:yValue].current];
        [thirdYValues add:[_maHigh push:yValue].current];
    }
    
    [mainSeries appendValuesX:xValues y:firstYValues];
    [maLowSeries appendValuesX:xValues y:secondYValues];
    [maHighSeries appendValuesX:xValues y:thirdYValues];
    
    long count = mainSeries.count + maLowSeries.count + maHighSeries.count;
    _annotation.text = [NSString stringWithFormat:@"Amount of points: %li", count];
}

- (int)getPointsCount {
    int result = 0;
    SCIRenderableSeriesCollection *rsCollection = self.surface.renderableSeries;
    for (NSInteger i = 0, count = rsCollection.count; i < count; i++) {
        result += [rsCollection itemAt:i].dataSeries.count;
    }
    
    return result;
}

- (void)updateIsRunningWith:(BOOL)isRunning {
    _isRunning = isRunning;
    
    [self updateAutoRangeBehavior:_isRunning];
    [self updateModifiers:!_isRunning];
}

- (void)updateAutoRangeBehavior:(BOOL)isEnabled {
    SCIAutoRange autoRangeMode = isEnabled ? SCIAutoRange_Always : SCIAutoRange_Never;
    
    [self.surface.xAxes itemAt:0].autoRange = autoRangeMode;
    [self.surface.yAxes itemAt:0].autoRange = autoRangeMode;
}

- (void)updateModifiers:(BOOL)isEnabled {
    SCIChartModifierCollection *modifiers = self.surface.chartModifiers;
    for (int i = 0; i < modifiers.count; i++) {
        [modifiers itemAt:i].isEnabled = isEnabled;
    }
}

- (void)resetChart {
    [SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
        for (int i = 0; i < self.surface.renderableSeries.count; i++) {
            [self.surface.renderableSeries[i].dataSeries clear];
        }
        self->_maLow = [[SCDMovingAverage alloc] initWithLength:MaLow];
        self->_maHigh = [[SCDMovingAverage alloc] initWithLength:MaHigh];
    }];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [_timer invalidate];
    _timer = nil;
}

- (void)onStartPress {
    [self updateIsRunningWith:YES];
}

- (void)onPausePress {
    [self updateIsRunningWith:NO];
}

- (void)onStopPress {
    [self updateIsRunningWith:NO];
    [self resetChart];
}

@end
PerformanceDemoView.swift
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// PerformanceDemoView.swift is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
//******************************************************************************

private let MaLow = 200;
private let MaHigh = 1000;
private let TimeInterval = 10.0;
private let MaxPointCount = 1000000;

class PerformanceDemoView: SCDPerformanceDemoViewControllerBase {
    
    private let _annotation = SCITextAnnotation()
    private let _maLow = SCDMovingAverage(length: MaLow)
    private var _maHigh = SCDMovingAverage(length: MaHigh)
    
    private var _timer: Timer?
    private var _isRunning = false
    
    override func initExample() {
        let xAxis = SCINumericAxis()
        xAxis.autoRange = .always
        
        let yAxis = SCINumericAxis()
        yAxis.autoRange = .always
        
        let rSeries1 = createRenderableSeries(withColorCode: 0xFF4083B7)
        let rSeries2 = createRenderableSeries(withColorCode: 0xFFFFA500)
        let rSeries3 = createRenderableSeries(withColorCode: 0xFFE13219)
        
        _annotation.set(x1: 0)
        _annotation.set(y1: 0)
        _annotation.coordinateMode = .relative
        _annotation.padding = SCIEdgeInsets(top: 5, left: 5, bottom: 0, right: 0)
        _annotation.fontStyle = SCIFontStyle(fontSize: 14, andTextColor: .white)

        SCIUpdateSuspender.usingWith(surface) {
            self.surface.xAxes.add(xAxis)
            self.surface.yAxes.add(yAxis)
            self.surface.renderableSeries.add(rSeries1)
            self.surface.renderableSeries.add(rSeries2)
            self.surface.renderableSeries.add(rSeries3)
            self.surface.annotations.add(self._annotation)
            self.surface.chartModifiers.add(SCDExampleBaseViewController.createDefaultModifiers())
        }
        
        _timer = Timer.scheduledTimer(timeInterval: TimeInterval / 1000, target: self, selector: #selector(updateData), userInfo: nil, repeats: true)
        if let timer = _timer {
            RunLoop.main.add(timer, forMode: .common)
        }
        
        _isRunning = true
    }
    
    @objc func updateData(_ timer:Timer) {
        if (!_isRunning || getPointsCount() > MaxPointCount) { return }
        
        let xValues = SCIIntegerValues(capacity: Int(pointsCount))
        let firstYValues = SCIFloatValues(capacity: Int(pointsCount))
        let secondYValues = SCIFloatValues(capacity: Int(pointsCount))
        let thirdYValues = SCIFloatValues(capacity: Int(pointsCount))
        
        let mainSeries = surface.renderableSeries[0].dataSeries as! SCIXyDataSeries
        let maLowSeries = surface.renderableSeries[1].dataSeries as! SCIXyDataSeries
        let maHighSeries = surface.renderableSeries[2].dataSeries as! SCIXyDataSeries
        
        let xMath = mainSeries.xMath
        let yMath = mainSeries.yMath
        
        var xValue: Int32 = mainSeries.count > 0 ? Int32(xMath.toDouble(mainSeries.xValues.value(at: mainSeries.count - 1))) : 0
        var yValue: Float = mainSeries.count > 0 ? Float(yMath.toDouble(mainSeries.yValues.value(at: mainSeries.count - 1))) : 10
        for _ in 0 ..< pointsCount {
            xValue += 1
            yValue += Float(randf(0.0, 1.0) - 0.5)
            xValues.add(xValue)
            firstYValues.add(yValue)
            secondYValues.add(Float(_maLow.push(Double(yValue)).current()))
            thirdYValues.add(Float(_maHigh.push(Double(yValue)).current()))
        }
        
        mainSeries.append(x: xValues, y: firstYValues)
        maLowSeries.append(x: xValues, y: secondYValues)
        maHighSeries.append(x: xValues, y: thirdYValues)

        let count = mainSeries.count + maLowSeries.count + maHighSeries.count
        _annotation.text = "Amount of points: \(count)"
    }

    fileprivate func getPointsCount() -> Int {
        var result: Int = 0
        let rsCollection = self.surface.renderableSeries
        for i in 0 ..< rsCollection.count {
            result += rsCollection[i].dataSeries!.count
        }
        
        return result
    }
    
    fileprivate func updateIsRunningWith(_ isRunning: Bool) {
        _isRunning = isRunning
        
        updateAutoRangeBehavior(_isRunning)
        updateModifiers(!_isRunning)
    }
    
    fileprivate func updateAutoRangeBehavior(_ isEnabled: Bool) {
        let autoRangeMode: SCIAutoRange = isEnabled ? .always : .never
        
        surface.xAxes.item(at: 0).autoRange = autoRangeMode;
        surface.yAxes.item(at: 0).autoRange = autoRangeMode;
    }
    
    fileprivate func updateModifiers(_ isEnabled: Bool) {
        for i in 0 ..< surface.chartModifiers.count {
            surface.chartModifiers.item(at: i).isEnabled = isEnabled
        }
    }
    
    fileprivate func resetChart() {
        SCIUpdateSuspender.usingWith(surface) {
            for i in 0 ..< self.surface.renderableSeries.count {
                self.surface.renderableSeries[i].dataSeries!.clear()
            }
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        _timer?.invalidate()
        _timer = nil
    }
    
    override func onStartPress() {
        updateIsRunningWith(true)
    }
    
    override func onPausePress() {
        updateIsRunningWith(false)
    }
    
    override func onStopPress() {
        updateIsRunningWith(false)
        resetChart()
    }
}
Back to iOS & macOS charts Examples