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 you how to use functionality of adding and removing series on demand (on a button action). This can be done programmatically using SCIRenderableSeriesCollection. Users of your application will be able to change the RenderableSeriesCollection on the SCIChartSurface at runtime!
The Swift and Objective-C source code for the iOS and macOS Chart Add Remove Seriesexample 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-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// AddRemoveSeriesChartView.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 "AddRemoveSeriesChartView.h"
#import "SCDButtonsTopPanel.h"
#import "SCDToolbarButton.h"
#import "SCDDataManager.h"
#import "SCDRandomUtil.h"
#import "SCDToolbarButtonsGroup.h"
@implementation AddRemoveSeriesChartView
- (Class)associatedType { return SCIChartSurface.class; }
- (NSArray<id<ISCDToolbarItemModel>> *)panelItems {
__weak typeof(self) wSelf = self;
return @[
[[SCDToolbarButton alloc] initWithTitle:@"Add Series" image:nil andAction:^{ [wSelf add]; }],
[[SCDToolbarButton alloc] initWithTitle:@"Remove Series" image:nil andAction:^{ [wSelf remove]; }],
[[SCDToolbarButton alloc] initWithTitle:@"Clear" image:nil andAction:^{ [wSelf.surface.renderableSeries clear]; }],
];
}
#if TARGET_OS_OSX
- (NSArray<id<ISCDToolbarItem>> *)provideExampleSpecificToolbarItems {
return @[[[SCDToolbarButtonsGroup alloc] initWithToolbarItems:self.panelItems]];
}
#elif TARGET_OS_IOS
- (SCIView *)providePanel {
return [[SCDButtonsTopPanel alloc] initWithToolbarItems:self.panelItems];
}
#endif
- (void)initExample {
id<ISCIAxis> xAxis = [SCINumericAxis new];
xAxis.autoRange = SCIAutoRange_Always;
xAxis.visibleRange = [[SCIDoubleRange alloc] initWithMin:0.0 max:150.0];
xAxis.axisTitle = @"X Axis";
id<ISCIAxis> yAxis = [SCINumericAxis new];
yAxis.autoRange = SCIAutoRange_Always;
yAxis.axisTitle = @"Y Axis";
[self.surface.xAxes add:xAxis];
[self.surface.yAxes add:yAxis];
[self.surface.chartModifiers add:[SCDExampleBaseViewController createDefaultModifiers]];
}
- (void)add {
[SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
SCDDoubleSeries *randomDoubleSeries = [SCDDataManager getRandomDoubleSeriesWithCount:150];
SCIXyDataSeries *dataSeries = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
[dataSeries appendValuesX:randomDoubleSeries.xValues y:randomDoubleSeries.yValues];
SCIFastMountainRenderableSeries *mountainRenderSeries = [[SCIFastMountainRenderableSeries alloc]init];
mountainRenderSeries.dataSeries = dataSeries;
[SCIColor colorWithRed:randi(0, 255) green:randi(0, 255) blue:randi(0, 255) alpha:1.0];
mountainRenderSeries.areaStyle = [[SCISolidBrushStyle alloc] initWithColor:[SCIColor colorWithRed:randi(0, 255) green:randi(0, 255) blue:randi(0, 255) alpha:1.0]];
mountainRenderSeries.strokeStyle = [[SCISolidPenStyle alloc] initWithColor:[SCIColor colorWithRed:randi(0, 255) green:randi(0, 255) blue:randi(0, 255) alpha:1.0] thickness:1.0];
[self.surface.renderableSeries add:mountainRenderSeries];
}];
}
- (void)remove {
[SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
if (self.surface.renderableSeries.count > 0) {
[self.surface.renderableSeries removeAt:0];
}
}];
}
@end
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// AddRemoveSeriesChartView.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.
//******************************************************************************
class AddRemoveSeriesChartView: SCDSingleChartWithTopPanelViewController<SCIChartSurface> {
override var associatedType: AnyClass { return SCIChartSurface.self }
private var panelItems: [ISCDToolbarItemModel] {
return [
SCDToolbarButton(title: "Add Series", image: nil, andAction: { [weak self] in self?.add() }),
SCDToolbarButton(title: "Remove Series", image: nil, andAction: { [weak self] in self?.remove() }),
SCDToolbarButton(title: "Clear", image: nil, andAction: { [weak self] in self?.surface.renderableSeries.clear() })
]
}
#if os(OSX)
override func provideExampleSpecificToolbarItems() -> [ISCDToolbarItem] {
return [SCDToolbarButtonsGroup(toolbarItems: panelItems)]
}
#elseif os(iOS)
override func providePanel() -> SCIView {
return SCDButtonsTopPanel(toolbarItems: self.panelItems)
}
#endif
override func initExample() {
let xAxis = SCINumericAxis()
xAxis.autoRange = .always
xAxis.visibleRange = SCIDoubleRange(min: 0.0, max: 150.0)
xAxis.axisTitle = "X Axis"
let yAxis = SCINumericAxis()
yAxis.autoRange = .always
yAxis.axisTitle = "Y Axis"
self.surface.xAxes.add(xAxis)
self.surface.yAxes.add(yAxis)
surface.chartModifiers.add(SCDExampleBaseViewController.createDefaultModifiers())
}
fileprivate func add() {
SCIUpdateSuspender.usingWith(surface) {
let randomDoubleSeries = SCDDataManager.getRandomDoubleSeries(withCount: 150)
let dataSeries = SCIXyDataSeries.init(xType: .double, yType: .double)
dataSeries.append(x: randomDoubleSeries.xValues, y: randomDoubleSeries.yValues)
let mountainRenderSeries = SCIFastMountainRenderableSeries()
mountainRenderSeries.dataSeries = dataSeries
mountainRenderSeries.areaStyle = SCISolidBrushStyle.init(color: SCIColor.init(red: CGFloat(randi(0, 255)), green: CGFloat(randi(0, 255)), blue: CGFloat(randi(0, 255)), alpha: 1.0))
mountainRenderSeries.strokeStyle = SCISolidPenStyle(color: SCIColor.init(red: CGFloat(randi(0, 255)), green: CGFloat(randi(0, 255)), blue: CGFloat(randi(0, 255)), alpha: 1.0), thickness: 1.0)
self.surface.renderableSeries.add(mountainRenderSeries)
}
}
fileprivate func remove() {
SCIUpdateSuspender.usingWith(surface) {
if (self.surface.renderableSeries.count > 0) {
self.surface.renderableSeries.remove(at: 0)
}
}
}
}