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.
Changing the scale of the axis is useful when, for example, the chart values are large numbers that you want to appear shorter and more readable. This example showcases how to make the chart panning while dragging the axes with the finger for both Y and X axes. You can make Axis to Scale on SciChart iOS Charts by adding add Axis-Drag scaling to a SciChartSurface using theSCIXAxisDragModifier and SCIYAxisDragModifier. ChartModifiers can be added to the SCIChartSurface by adding them to the SciChartSurface.chartModifiers collection.
Finally, drag modifiers can be used in Absolute mode (when YAxis.getAutoRange()=false) as well as Relative mode (when YAxis.getAutoRange()=true).
TIP!
You can control how the series behaves as you pan to the edge of the data with the SCIXAxisDragModifier setClipModeX() method.
The Swift and Objective-C source code for the iOS and macOS Chart Drag Axis to Scale 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
//
// DragAxisToScaleChartView.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 "DragAxisToScaleChartView.h"
#import "SCDDataManager.h"
#import "SCDRandomWalkGenerator.h"
@implementation DragAxisToScaleChartView
- (void)initExample {
id<ISCIAxis> xAxis = [SCINumericAxis new];
xAxis.growBy = [[SCIDoubleRange alloc] initWithMin:0.1 max:0.1];
xAxis.visibleRange = [[SCIDoubleRange alloc] initWithMin:3 max:6];
id<ISCIAxis> rightYAxis = [SCINumericAxis new];
rightYAxis.growBy = [[SCIDoubleRange alloc] initWithMin:0.1 max:0.1];
rightYAxis.axisId = @"RightAxisId";
rightYAxis.axisAlignment = SCIAxisAlignment_Right;
rightYAxis.tickLabelStyle = [[SCIFontStyle alloc] initWithFontSize:12 andTextColorCode:0xFF279B27];
rightYAxis.titleStyle = [[SCIFontStyle alloc] initWithFontSize:18 andTextColorCode:0xFF279B27];
id<ISCIAxis> leftYAxis = [SCINumericAxis new];
leftYAxis.growBy = [[SCIDoubleRange alloc] initWithMin:0.1 max:0.1];
leftYAxis.axisId = @"LeftAxisId";
leftYAxis.axisAlignment = SCIAxisAlignment_Left;
leftYAxis.tickLabelStyle = [[SCIFontStyle alloc] initWithFontSize:12 andTextColorCode:0xFF4083B7];
leftYAxis.titleStyle = [[SCIFontStyle alloc] initWithFontSize:18 andTextColorCode:0xFF4083B7];
SCDDoubleSeries *fourierSeries = [SCDDataManager getFourierSeriesWithAmplitude:1.0 phaseShift:0.1 count:5000];
SCDDoubleSeries *dampedSinewave = [SCDDataManager getDampedSinewaveWithPad:1500 Amplitude:3.0 Phase:0.0 DampingFactor:0.005 PointCount:5000 Freq:10];
SCIXyDataSeries *mountainDataSeries = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
SCIXyDataSeries *lineDataSeries = [[SCIXyDataSeries alloc] initWithXType:SCIDataType_Double yType:SCIDataType_Double];
[mountainDataSeries appendValuesX:fourierSeries.xValues y:fourierSeries.yValues];
[lineDataSeries appendValuesX:dampedSinewave.xValues y:dampedSinewave.yValues];
SCIFastMountainRenderableSeries *mountainSeries = [SCIFastMountainRenderableSeries new];
mountainSeries.dataSeries = mountainDataSeries;
mountainSeries.areaStyle = [[SCISolidBrushStyle alloc] initWithColorCode:0x771964FF];
mountainSeries.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:0xFF0944CF thickness:2.0];
mountainSeries.yAxisId = @"LeftAxisId";
SCIFastLineRenderableSeries *lineSeries = [SCIFastLineRenderableSeries new];
lineSeries.dataSeries = lineDataSeries;
lineSeries.strokeStyle = [[SCISolidPenStyle alloc] initWithColorCode:0xFF279B27 thickness:2.0];
lineSeries.yAxisId = @"RightAxisId";
self.xAxisDragModifier = [SCIXAxisDragModifier new];
self.xAxisDragModifier.dragMode = self.selectedDragMode;
self.xAxisDragModifier.isEnabled = self.selectedDirection == SCIDirection2D_XDirection || self.selectedDirection == SCIDirection2D_XyDirection;
self.yAxisDragModifier = [SCIYAxisDragModifier new];
self.yAxisDragModifier.dragMode = self.selectedDragMode;
self.yAxisDragModifier.isEnabled = self.selectedDirection == SCIDirection2D_YDirection || self.selectedDirection == SCIDirection2D_XyDirection;
[SCIUpdateSuspender usingWithSuspendable:self.surface withBlock:^{
[self.surface.xAxes add:xAxis];
[self.surface.yAxes add:leftYAxis];
[self.surface.yAxes add:rightYAxis];
[self.surface.renderableSeries add:mountainSeries];
[self.surface.renderableSeries add:lineSeries];
[self.surface.chartModifiers addAll:self.xAxisDragModifier, self.yAxisDragModifier, [SCIZoomExtentsModifier new], nil];
[SCIAnimations sweepSeries:lineSeries duration:3.0 andEasingFunction:[SCICubicEase new]];
[SCIAnimations scaleSeries:mountainSeries duration:3.0 andEasingFunction:[SCICubicEase new]];
}];
}
@end
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2019. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// DragAxisToScaleChartView.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 DragAxisToScaleChartView: SCDDragAxisToScaleChartViewControllerBase {
override func initExample() {
let xAxis = SCINumericAxis()
xAxis.growBy = SCIDoubleRange(min: 0.1, max: 0.1)
xAxis.visibleRange = SCIDoubleRange(min: 3, max: 6)
let rightYAxis = SCINumericAxis()
rightYAxis.growBy = SCIDoubleRange(min: 0.1, max: 0.1)
rightYAxis.axisId = "RightAxisId"
rightYAxis.axisAlignment = .right
rightYAxis.titleStyle = SCIFontStyle(fontSize: 18, andTextColorCode: 0xFF279B27)
rightYAxis.tickLabelStyle = SCIFontStyle(fontSize: 12, andTextColorCode: 0xFF279B27)
let leftYAxis = SCINumericAxis()
leftYAxis.growBy = SCIDoubleRange(min: 0.1, max: 0.1)
leftYAxis.axisId = "LeftAxisId"
leftYAxis.axisAlignment = .left
leftYAxis.titleStyle = SCIFontStyle(fontSize: 18, andTextColorCode: 0xFF279B27)
leftYAxis.tickLabelStyle = SCIFontStyle(fontSize: 12, andTextColorCode: 0xFF279B27)
let fourierSeries = SCDDataManager.getFourierSeries(withAmplitude: 1.0, phaseShift: 0.1, count: 5000)
let dampedSinewave = SCDDataManager.getDampedSinewave(withPad: 1500, amplitude: 3.0, phase: 0.0, dampingFactor: 0.005, pointCount: 5000, freq: 10)
let mountainDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
let lineDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
mountainDataSeries.append(x: fourierSeries.xValues, y: fourierSeries.yValues)
lineDataSeries.append(x: dampedSinewave.xValues, y: dampedSinewave.yValues)
let mountainSeries = SCIFastMountainRenderableSeries()
mountainSeries.dataSeries = mountainDataSeries
mountainSeries.areaStyle = SCISolidBrushStyle(color: 0x771964FF)
mountainSeries.strokeStyle = SCISolidPenStyle(color: 0xFF0944CF, thickness: 2.0)
mountainSeries.yAxisId = "LeftAxisId"
let lineSeries = SCIFastLineRenderableSeries()
lineSeries.dataSeries = lineDataSeries
lineSeries.strokeStyle = SCISolidPenStyle(color: 0xFF279B27, thickness: 2.0)
lineSeries.yAxisId = "RightAxisId"
xAxisDragModifier = SCIXAxisDragModifier()
xAxisDragModifier.dragMode = selectedDragMode;
xAxisDragModifier.isEnabled = selectedDirection == .xDirection || selectedDirection == .xyDirection;
yAxisDragModifier = SCIYAxisDragModifier()
yAxisDragModifier.dragMode = selectedDragMode;
yAxisDragModifier.isEnabled = selectedDirection == .yDirection || selectedDirection == .xyDirection;
SCIUpdateSuspender.usingWith(surface) {
self.surface.xAxes.add(xAxis)
self.surface.yAxes.add(items: leftYAxis, rightYAxis)
self.surface.renderableSeries.add(items: mountainSeries, lineSeries)
self.surface.chartModifiers.add(items: self.xAxisDragModifier, self.yAxisDragModifier, SCIZoomExtentsModifier())
SCIAnimations.sweep(lineSeries, duration: 3.0, easingFunction: SCICubicEase())
SCIAnimations.scale(mountainSeries, duration: 3.0, andEasingFunction: SCICubicEase())
}
}
}
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2020. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// SCDDragAxisToScaleChartViewControllerBase.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 "SCDDragAxisToScaleChartViewControllerBase.h"
#import "SCDSettingsPresenter.h"
#import "SCDToolbarButtonsGroup.h"
#import "SCDToolbarItem.h"
#import "SCDToolbarPopupItem.h"
#import "SCDLabeledSettingsItem.h"
#import "SCDConstants.h"
@implementation SCDDragAxisToScaleChartViewControllerBase {
SCDSettingsPresenter *_settingsPresenter;
}
@synthesize xAxisDragModifier = _xAxisDragModifier;
@synthesize yAxisDragModifier = _yAxisDragModifier;
@synthesize selectedDragMode = _selectedDragMode;
@synthesize selectedDirection = _selectedDirection;
- (Class)associatedType { return SCIChartSurface.class; }
- (void)commonInit {
_selectedDragMode = SCIAxisDragMode_Scale;
_selectedDirection = SCIDirection2D_XyDirection;
}
- (NSArray<id<ISCDToolbarItem>> *)provideExampleSpecificToolbarItems {
__weak typeof(self) wSelf = self;
SCDToolbarButtonsGroup *settingsToolbar = [[SCDToolbarButtonsGroup alloc] initWithToolbarItems:@[
[[SCDToolbarItem alloc] initWithTitle:@"Legend settings" image:[SCIImage imageNamed:@"chart.settings"] andAction:^{ [wSelf p_SCD_openSettings]; }]
]];
settingsToolbar.identifier = TOOLBAR_MODIFIERS_SETTINGS;
return @[settingsToolbar];
}
- (void)p_SCD_openSettings {
_settingsPresenter = [[SCDSettingsPresenter alloc] initWithSettingsItems:[self p_SCD_createLegendModifierSettingsItems] andIdentifier:TOOLBAR_MODIFIERS_SETTINGS];
}
- (NSArray<id<ISCDToolbarItem>> *)p_SCD_createLegendModifierSettingsItems {
__weak typeof(self) wSelf = self;
SCDToolbarPopupItem *dragModePopupItem = [[SCDToolbarPopupItem alloc] initWithTitles:@[@"Scale", @"Pan"] selectedIndex:_selectedDragMode andAction:^(NSUInteger selectedDragMode) {
[wSelf p_SCD_onDragModeChange:selectedDragMode];
}];
SCDToolbarPopupItem *directionPopupItem = [[SCDToolbarPopupItem alloc] initWithTitles:@[@"XDirection", @"YDirection", @"XyDirection"] selectedIndex:_selectedDirection andAction:^(NSUInteger selectedDirection) {
[wSelf p_SCD_onDirectionChange:selectedDirection];
}];
return @[
[[SCDLabeledSettingsItem alloc] initWithLabelText:@"Axis drag mode:" item:dragModePopupItem],
[[SCDLabeledSettingsItem alloc] initWithLabelText:@"Direction:" item:directionPopupItem]
];
}
- (void)p_SCD_onDragModeChange:(SCIAxisDragMode)selectedDragMode {
_selectedDragMode = selectedDragMode;
_xAxisDragModifier.dragMode = _selectedDragMode;
_yAxisDragModifier.dragMode = _selectedDragMode;
}
- (void)p_SCD_onDirectionChange:(SCIDirection2D)selectedDirection {
_selectedDirection = selectedDirection;
switch (_selectedDirection) {
case SCIDirection2D_XDirection:
_xAxisDragModifier.isEnabled = YES;
_yAxisDragModifier.isEnabled = NO;
break;
case SCIDirection2D_YDirection:
_xAxisDragModifier.isEnabled = NO;
_yAxisDragModifier.isEnabled = YES;
break;
case SCIDirection2D_XyDirection:
_xAxisDragModifier.isEnabled = YES;
_yAxisDragModifier.isEnabled = YES;
break;
default:
break;
}
}
@end