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.
Generates an iOS Stacked Bars chart. A Stacked Bars chart is a Stacked Column chart rotated 90 degrees, so that the columns are rendered horizontally. In order to achieve this effect, we create a Stacked Column chart, then use the XAxis and YAxis alignment to rotate the chart. See Documentation on how to use this type here: The iOS Stacked Column Chart.
The SCIStackedColumnRenderableSeries API can be used to render columns stacked above each other, or side by side in either Swift or Objective C. Grouping is performed when you place multiple SCIStackedColumnRenderableSeries into a SCIVerticallyStackedMountainsCollection or for horizontal column groups, the SCIHorizontallyStackedColumnsCollection.
Data is provided by a DataSeries, e.g. the SCIXyDataSeries, SCIXyyDataSeries (uses Y1 only) or SCIOhlcDataSeries (renders close).
The Swift and Objective-C source code for the iOS and macOS Stacked Bars Chart 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-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// RealTimeGhostTracesChartView.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 "RealTimeGhostTracesChartView.h"
#import "SCDRealTimeGhostTracesToolbarItem.h"
#import "SCDDataManager.h"
#import "SCDRandomUtil.h"
@interface RealTimeGhostTracesChartView ()
@property (nonatomic, strong) SCDRealTimeGhostTracesToolbarItem *actionItem;
@property (nonatomic) NSTimer *timer;
@end
@implementation RealTimeGhostTracesChartView {
double _lastAmplitude;
double _phase;
}
- (Class)associatedType { return SCIChartSurface.class; }
- (BOOL)showDefaultModifiersInToolbar { return NO; }
#if TARGET_OS_OSX
- (NSArray<id<ISCDToolbarItem>> *)provideExampleSpecificToolbarItems {
return @[self.actionItem];
}
#elif TARGET_OS_IOS
- (SCIView *)providePanel {
return [self.actionItem createView];
}
#endif
- (SCDRealTimeGhostTracesToolbarItem *)actionItem {
if (!_actionItem) {
__weak typeof(self) wSelf = self;
_actionItem = [[SCDRealTimeGhostTracesToolbarItem alloc] initWithAction:^(double doubleValue) {
if (doubleValue > 0) {
if (wSelf.timer != nil) {
[wSelf.timer invalidate];
}
[wSelf p_SCD_startTimerWithValue:doubleValue];
}
}];
}
return _actionItem;
}
- (void)initExample {
_lastAmplitude = 1.0;
id<ISCIAxis> xAxis = [SCINumericAxis new];
xAxis.autoRange = SCIAutoRange_Always;
id<ISCIAxis> yAxis = [SCINumericAxis new];
yAxis.autoRange = SCIAutoRange_Never;
yAxis.visibleRange = [[SCIDoubleRange alloc] initWithMin:-2.0 max:2.0];
[self.surface.xAxes add:xAxis];
[self.surface.yAxes add:yAxis];
uint limeGreen = 0xFF32CD32;
[self p_SCD_addLineRenderableSeries:limeGreen opacity:1.0];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.9];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.8];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.7];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.62];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.55];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.45];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.35];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.25];
[self p_SCD_addLineRenderableSeries:limeGreen opacity:0.15];
[self p_SCD_startTimerWithValue:_actionItem.sliderValue];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_timer invalidate];
_timer = nil;
}
- (void)p_SCD_startTimerWithValue:(double)value {
_timer = [NSTimer scheduledTimerWithTimeInterval:value / 1000.0 target:self selector:@selector(p_SCD_updateData:) userInfo:nil repeats:YES];
}
- (void)p_SCD_updateData:(NSTimer *)timer {
[SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
SCIXyDataSeries *dataSeries = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
double randomAmplitude = self->_lastAmplitude + ([SCDRandomUtil nextDouble] - 0.5);
if (randomAmplitude < -2.0) {
randomAmplitude = -2.0;
} else if (randomAmplitude > 2.0) {
randomAmplitude = 2.0;
}
SCDDoubleSeries *noisySinewave = [SCDDataManager getNoisySinewaveWithAmplitude:randomAmplitude Phase:self->_phase PointCount:1000 NoiseAmplitude:0.25];
self->_lastAmplitude = randomAmplitude;
[dataSeries appendValuesX:noisySinewave.xValues y:noisySinewave.yValues];
[self p_SCD_reassignRenderSeriesWithDataSeries:dataSeries];
}];
}
- (void)p_SCD_reassignRenderSeriesWithDataSeries:(SCIXyDataSeries *)dataSeries {
SCIRenderableSeriesCollection *rSeries = self.surface.renderableSeries;
// shift old dataSeries
[rSeries itemAt:9].dataSeries = [rSeries itemAt:8].dataSeries;
[rSeries itemAt:8].dataSeries = [rSeries itemAt:7].dataSeries;
[rSeries itemAt:7].dataSeries = [rSeries itemAt:6].dataSeries;
[rSeries itemAt:6].dataSeries = [rSeries itemAt:5].dataSeries;
[rSeries itemAt:5].dataSeries = [rSeries itemAt:4].dataSeries;
[rSeries itemAt:4].dataSeries = [rSeries itemAt:3].dataSeries;
[rSeries itemAt:3].dataSeries = [rSeries itemAt:2].dataSeries;
[rSeries itemAt:2].dataSeries = [rSeries itemAt:1].dataSeries;
[rSeries itemAt:1].dataSeries = [rSeries itemAt:0].dataSeries;
// use new dataSeries to draw first renderableSeries
[rSeries itemAt:0].dataSeries = dataSeries;
}
- (void)p_SCD_addLineRenderableSeries:(uint)color opacity:(float)opacity {
SCIFastLineRenderableSeries *lineRenerableSeries = [SCIFastLineRenderableSeries new];
lineRenerableSeries.opacity = opacity;
lineRenerableSeries.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:color thickness:1];
[self.surface.renderableSeries add:lineRenerableSeries];
}
@end
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// StackedBarChartView.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 StackedBarChartView: SCDSingleChartViewController<SCIChartSurface> {
override var associatedType: AnyClass { return SCIChartSurface.self }
override func initExample() {
let xAxis = SCINumericAxis()
xAxis.axisAlignment = .left
let yAxis = SCINumericAxis()
yAxis.axisAlignment = .bottom
yAxis.flipCoordinates = true
let yValues1 = [0.0, 0.1, 0.2, 0.4, 0.8, 1.1, 1.5, 2.4, 4.6, 8.1, 11.7, 14.4, 16.0, 13.7, 10.1, 6.4, 3.5, 2.5, 5.4, 6.4, 7.1, 8.0, 9.0]
let yValues2 = [2.0, 10.1, 10.2, 10.4, 10.8, 1.1, 11.5, 3.4, 4.6, 0.1, 1.7, 14.4, 16.0, 13.7, 10.1, 6.4, 3.5, 2.5, 1.4, 0.4, 10.1, 0.0, 0.0]
let yValues3 = [20.0, 4.1, 4.2, 10.4, 10.8, 1.1, 11.5, 3.4, 4.6, 5.1, 5.7, 14.4, 16.0, 13.7, 10.1, 6.4, 3.5, 2.5, 1.4, 10.4, 8.1, 10.0, 15.0]
let ds1 = SCIXyDataSeries(xType: .double, yType: .double)
ds1.seriesName = "Data 1"
let ds2 = SCIXyDataSeries(xType: .double, yType: .double)
ds2.seriesName = "Data 2"
let ds3 = SCIXyDataSeries(xType: .double, yType: .double)
ds3.seriesName = "Data 3"
for i in 0 ..< yValues1.count {
ds1.append(x: i, y: yValues1[i])
ds2.append(x: i, y: yValues2[i])
ds3.append(x: i, y: yValues3[i])
}
SCIUpdateSuspender.usingWith(surface) {
let columnCollection = SCIVerticallyStackedColumnsCollection()
columnCollection.add(self.getRenderableSeriesWith(dataSeries: ds1, startColor: 0xff567893, endColor:0xff3D5568))
columnCollection.add(self.getRenderableSeriesWith(dataSeries: ds2, startColor: 0xffACBCCA, endColor:0xff439AAF))
columnCollection.add(self.getRenderableSeriesWith(dataSeries: ds3, startColor: 0xffDBE0E1, endColor:0xffB6C1C3))
self.surface.xAxes.add(xAxis)
self.surface.yAxes.add(yAxis)
self.surface.renderableSeries.add(columnCollection)
self.surface.chartModifiers.add(items: SCIZoomExtentsModifier(), SCICursorModifier())
}
}
fileprivate func getRenderableSeriesWith(dataSeries: SCIXyDataSeries, startColor: uint, endColor: uint) -> SCIStackedColumnRenderableSeries {
let rSeries = SCIStackedColumnRenderableSeries()
rSeries.dataSeries = dataSeries
rSeries.fillBrushStyle = SCILinearGradientBrushStyle(start: CGPoint(x: 0.5, y: 0.0), end: CGPoint(x: 0.5, y: 1.0), startColorCode: startColor, endColorCode: endColor)
rSeries.strokeStyle = SCISolidPenStyle(color: .black, thickness: 0.5)
SCIAnimations.wave(rSeries, duration: 3.0, andEasingFunction: SCICubicEase())
return rSeries
}
}