SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
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.
This example demonstrates how to use SeriesValueModifier in iOs and macOS charting applications with SciChart. It allows you to display the last series value as a marker on the YAxis. This modifier automatically creates one marker per series and places it at the series latest Y-Value on the Y axis. This feature is often used in Trading and Financial charting applications.
The Swift and Objective-C source code for the iOS and macOS Series Value Modifier 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).
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// UsingSeriesValueModifierView.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 "UsingSeriesValueModifierView.h"
static int const FifoCapacity = 100;
static double const TimeInterval = 0.05;
@implementation UsingSeriesValueModifierView {
NSTimer *_timer;
SCIXyDataSeries *_ds1;
SCIXyDataSeries *_ds2;
SCIXyDataSeries *_ds3;
double _t;
}
- (Class)associatedType { return SCIChartSurface.class; }
- (void)initExample {
id<ISCIAxis> xAxis = [SCINumericAxis new];
xAxis.autoRange = SCIAutoRange_Always;
xAxis.axisTitle = @"Time (Seconds)";
xAxis.textFormatting = @"0.0";
id<ISCIAxis> yAxis = [SCINumericAxis new];
yAxis.autoRange = SCIAutoRange_Always;
yAxis.axisTitle = @"Amplitude (Volts)";
yAxis.growBy = [[SCIDoubleRange alloc] initWithMin:0.1 max:0.1];
yAxis.textFormatting = @"0.00";
yAxis.cursorTextFormatting = @"0.00";
_ds1 = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
_ds1.fifoCapacity = FifoCapacity;
_ds1.seriesName = @"Orange Series";
_ds2 = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
_ds2.fifoCapacity = FifoCapacity;
_ds2.seriesName = @"Blue Series";
_ds3 = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
_ds3.fifoCapacity = FifoCapacity;
_ds3.seriesName = @"Green Series";
SCIFastLineRenderableSeries *rSeries1 = [SCIFastLineRenderableSeries new];
rSeries1.dataSeries = _ds1;
rSeries1.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:0xFFFF8C00 thickness:2];
SCIFastLineRenderableSeries *rSeries2 = [SCIFastLineRenderableSeries new];
rSeries2.dataSeries = _ds2;
rSeries2.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:0xFF4682B4 thickness:2];
SCIFastLineRenderableSeries *rSeries3 = [SCIFastLineRenderableSeries new];
rSeries3.dataSeries = _ds3;
rSeries3.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:0xFF556B2F thickness:2];
SCILegendModifier *legendModifier = [SCILegendModifier new];
legendModifier.margins = (SCIEdgeInsets){.left = 16, .top = 16, .right = 16, .bottom = 16};
[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.chartModifiers addAll:[SCISeriesValueModifier new], legendModifier, nil];
}];
_timer = [NSTimer scheduledTimerWithTimeInterval:TimeInterval target:self selector:@selector(updateData:) userInfo:nil repeats:YES];
}
- (void)updateData:(NSTimer *)timer {
double y1 = 3.0 * sin(((2 * M_PI) * 1.4) * _t * 0.02);
double y2 = 2.0 * cos(((2 * M_PI) * 0.8) * _t * 0.02);
double y3 = 1.0 * sin(((2 * M_PI) * 2.2) * _t * 0.02);
[_ds1 appendX:@(_t) y:@(y1)];
[_ds2 appendX:@(_t) y:@(y2)];
[_ds3 appendX:@(_t) y:@(y3)];
_t += TimeInterval;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_timer invalidate];
_timer = nil;
}
@end
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// UsingSeriesValueModifierView.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 FifoCapacity: Int = 100
// Data Sample Rate (sec) - 20 Hz
private let TimeInterval = 0.05
class UsingSeriesValueModifierView: SCDSingleChartViewController<SCIChartSurface> {
private var _timer: Timer?
private var _ds1: SCIXyDataSeries!
private var _ds2: SCIXyDataSeries!
private var _ds3: SCIXyDataSeries!
private var _t = 0.0
override var associatedType: AnyClass { return SCIChartSurface.self }
override func initExample() {
let xAxis = SCINumericAxis()
xAxis.autoRange = .always
xAxis.axisTitle = "Time (Seconds)"
xAxis.textFormatting = "0.0"
let yAxis = SCINumericAxis()
yAxis.autoRange = .always
yAxis.axisTitle = "Amplitude (Volts)"
yAxis.growBy = SCIDoubleRange(min: 0.1, max: 0.1)
yAxis.textFormatting = "0.00"
yAxis.cursorTextFormatting = "0.00"
_ds1 = SCIXyDataSeries(xType: .double, yType: .double)
_ds1.fifoCapacity = FifoCapacity
_ds1.seriesName = "Orange Series"
_ds2 = SCIXyDataSeries(xType: .double, yType: .double)
_ds2.fifoCapacity = FifoCapacity
_ds2.seriesName = "Blue Series"
_ds3 = SCIXyDataSeries(xType: .double, yType: .double)
_ds3.fifoCapacity = FifoCapacity
_ds3.seriesName = "Green Series"
let rSeries1 = SCIFastLineRenderableSeries()
rSeries1.dataSeries = _ds1
rSeries1.strokeStyle = SCISolidPenStyle(color: 0xFFFF8C00, thickness: 2)
let rSeries2 = SCIFastLineRenderableSeries()
rSeries2.dataSeries = _ds2
rSeries2.strokeStyle = SCISolidPenStyle(color: 0xFF4682B4, thickness: 2)
let rSeries3 = SCIFastLineRenderableSeries()
rSeries3.dataSeries = _ds3
rSeries3.strokeStyle = SCISolidPenStyle(color: 0xFF556B2F, thickness: 2)
let legendModifier = SCILegendModifier()
legendModifier.margins = SCIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
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.chartModifiers.add(items: SCISeriesValueModifier(), legendModifier)
}
_timer = Timer.scheduledTimer(timeInterval: TimeInterval, target: self, selector: #selector(updateData), userInfo: nil, repeats: true)
}
@objc func updateData(_ timer: Timer) {
let y1: Double = 3.0 * sin(((2 * .pi) * 1.4) * _t * 0.02)
let y2: Double = 2.0 * cos(((2 * .pi) * 0.8) * _t * 0.02)
let y3: Double = 1.0 * sin(((2 * .pi) * 2.2) * _t * 0.02)
_ds1.append(x: _t, y: y1)
_ds2.append(x: _t, y: y2)
_ds3.append(x: _t, y: y3)
_t += TimeInterval;
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
_timer?.invalidate()
_timer = nil
}
}