diff --git a/ChartsDemo/ChartsDemo.xcodeproj/project.pbxproj b/ChartsDemo/ChartsDemo.xcodeproj/project.pbxproj index c09adb037c..2b13e75ff6 100644 --- a/ChartsDemo/ChartsDemo.xcodeproj/project.pbxproj +++ b/ChartsDemo/ChartsDemo.xcodeproj/project.pbxproj @@ -30,6 +30,8 @@ 5B8EAF281AB32CF5009697AA /* DemoBaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B8EAF261AB32CF5009697AA /* DemoBaseViewController.m */; }; 5B8EAF301AB32E15009697AA /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5B8EAF2F1AB32E15009697AA /* Images.xcassets */; }; 5B8EAF3D1AB32F27009697AA /* Charts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B8EAF371AB32EA1009697AA /* Charts.framework */; }; + 5B9624411B38608C007763E2 /* NegativeStackedBarChartViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B96243F1B38608C007763E2 /* NegativeStackedBarChartViewController.m */; }; + 5B9624421B38608C007763E2 /* NegativeStackedBarChartViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5B9624401B38608C007763E2 /* NegativeStackedBarChartViewController.xib */; }; 5BB4B0721ACA710D00E2EF4D /* Charts.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 5B8EAF371AB32EA1009697AA /* Charts.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5BD47E5B1ABB0263008FCEC6 /* BalloonMarker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BD47E5A1ABB0263008FCEC6 /* BalloonMarker.swift */; }; 5BD47E601ABB3C91008FCEC6 /* LineChart2ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BD47E5E1ABB3C91008FCEC6 /* LineChart2ViewController.m */; }; @@ -126,6 +128,9 @@ 5B8EAF261AB32CF5009697AA /* DemoBaseViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoBaseViewController.m; sourceTree = ""; }; 5B8EAF2F1AB32E15009697AA /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 5B8EAF321AB32EA0009697AA /* Charts.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Charts.xcodeproj; path = ../Charts/Charts.xcodeproj; sourceTree = ""; }; + 5B96243E1B38608C007763E2 /* NegativeStackedBarChartViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NegativeStackedBarChartViewController.h; sourceTree = ""; }; + 5B96243F1B38608C007763E2 /* NegativeStackedBarChartViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NegativeStackedBarChartViewController.m; sourceTree = ""; }; + 5B9624401B38608C007763E2 /* NegativeStackedBarChartViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NegativeStackedBarChartViewController.xib; sourceTree = ""; }; 5BD47E5A1ABB0263008FCEC6 /* BalloonMarker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BalloonMarker.swift; sourceTree = ""; }; 5BD47E5C1ABB0273008FCEC6 /* ChartsDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ChartsDemo-Bridging-Header.h"; sourceTree = ""; }; 5BD47E5D1ABB3C91008FCEC6 /* LineChart2ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LineChart2ViewController.h; sourceTree = ""; }; @@ -323,6 +328,9 @@ 5BEAED221ABC0BE20013F194 /* MultipleLinesChartViewController.h */, 5BEAED231ABC0BE20013F194 /* MultipleLinesChartViewController.m */, 5BEAED241ABC0BE20013F194 /* MultipleLinesChartViewController.xib */, + 5B96243E1B38608C007763E2 /* NegativeStackedBarChartViewController.h */, + 5B96243F1B38608C007763E2 /* NegativeStackedBarChartViewController.m */, + 5B9624401B38608C007763E2 /* NegativeStackedBarChartViewController.xib */, 5B0CC7821ABB875400665592 /* PieChartViewController.h */, 5B0CC7831ABB875400665592 /* PieChartViewController.m */, 5B0CC7841ABB875400665592 /* PieChartViewController.xib */, @@ -435,6 +443,7 @@ 5BEAED321ABC18F00013F194 /* CubicLineChartViewController.xib in Resources */, 5BEAED281ABC0BE20013F194 /* MultipleLinesChartViewController.xib in Resources */, 5B4316381AB8D8B70009FCAA /* Default@2x.png in Resources */, + 5B9624421B38608C007763E2 /* NegativeStackedBarChartViewController.xib in Resources */, 5B0CC7861ABB875400665592 /* PieChartViewController.xib in Resources */, 5BEAED1C1ABBFB340013F194 /* ScatterChartViewController.xib in Resources */, 5BD8F0751AB89CE500566E05 /* LineChart1ViewController.xib in Resources */, @@ -480,6 +489,7 @@ 5BD47E601ABB3C91008FCEC6 /* LineChart2ViewController.m in Sources */, 5B57BBB51A9B26AA0036A6CC /* main.m in Sources */, 5BEAED361ABC192F0013F194 /* RadarChartViewController.m in Sources */, + 5B9624411B38608C007763E2 /* NegativeStackedBarChartViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ChartsDemo/Classes/DemoListViewController.m b/ChartsDemo/Classes/DemoListViewController.m index f4a82eee00..de3adaa572 100644 --- a/ChartsDemo/Classes/DemoListViewController.m +++ b/ChartsDemo/Classes/DemoListViewController.m @@ -20,6 +20,7 @@ #import "PieChartViewController.h" #import "ScatterChartViewController.h" #import "StackedBarChartViewController.h" +#import "NegativeStackedBarChartViewController.h" #import "AnotherBarChartViewController.h" #import "MultipleLinesChartViewController.h" #import "MultipleBarChartViewController.h" @@ -90,6 +91,11 @@ - (void)viewDidLoad @"subtitle": @"A simple demonstration of a bar chart with stacked bars.", @"class": StackedBarChartViewController.class }, + @{ + @"title": @"Stacked Bar Chart Negative", + @"subtitle": @"A simple demonstration of stacked bars with negative and positive values.", + @"class": NegativeStackedBarChartViewController.class + }, @{ @"title": @"Another Bar Chart", @"subtitle": @"Implementation of a BarChart that only shows values at the bottom.", diff --git a/ChartsDemo/Classes/Demos/MultipleBarChartViewController.m b/ChartsDemo/Classes/Demos/MultipleBarChartViewController.m index 6ed911b163..cadcb861fc 100644 --- a/ChartsDemo/Classes/Demos/MultipleBarChartViewController.m +++ b/ChartsDemo/Classes/Demos/MultipleBarChartViewController.m @@ -124,7 +124,7 @@ - (void)setDataCount:(int)count range:(double)range NSMutableArray *dataSets = [[NSMutableArray alloc] init]; [dataSets addObject:set1]; [dataSets addObject:set2]; - [dataSets addObject:set3]; + //[dataSets addObject:set3]; BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets]; data.groupSpace = 0.8; diff --git a/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.h b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.h new file mode 100644 index 0000000000..675515c50f --- /dev/null +++ b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.h @@ -0,0 +1,20 @@ +// +// NegativeStackedBarChartViewController.h +// ChartsDemo +// +// Created by Daniel Cohen Gindi on 17/3/15. +// +// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda +// A port of MPAndroidChart for iOS +// Licensed under Apache License 2.0 +// +// https://github.com/danielgindi/ios-charts +// + +#import +#import "DemoBaseViewController.h" +#import + +@interface NegativeStackedBarChartViewController : DemoBaseViewController + +@end diff --git a/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.m b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.m new file mode 100644 index 0000000000..4548ae7334 --- /dev/null +++ b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.m @@ -0,0 +1,236 @@ +// +// NegativeStackedBarChartViewController.m +// ChartsDemo +// +// Created by Daniel Cohen Gindi on 17/3/15. +// +// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda +// A port of MPAndroidChart for iOS +// Licensed under Apache License 2.0 +// +// https://github.com/danielgindi/ios-charts +// + +#import "NegativeStackedBarChartViewController.h" +#import "ChartsDemo-Swift.h" + +@interface NegativeStackedBarChartViewController () + +@property (nonatomic, strong) IBOutlet HorizontalBarChartView *chartView; + +@end + +@implementation NegativeStackedBarChartViewController + +- (void)viewDidLoad +{ + [super viewDidLoad]; + + self.title = @"Horizontal Bar Chart"; + + self.options = @[ + @{@"key": @"toggleValues", @"label": @"Toggle Values"}, + @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"}, + @{@"key": @"toggleHighlightArrow", @"label": @"Toggle Highlight Arrow"}, + @{@"key": @"animateX", @"label": @"Animate X"}, + @{@"key": @"animateY", @"label": @"Animate Y"}, + @{@"key": @"animateXY", @"label": @"Animate XY"}, + @{@"key": @"toggleStartZero", @"label": @"Toggle StartZero"}, + @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"}, + @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"}, + @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"}, + ]; + + NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init]; + customFormatter.negativePrefix = @""; + customFormatter.positiveSuffix = @"m"; + customFormatter.negativeSuffix = @"m"; + customFormatter.minimumSignificantDigits = 1; + customFormatter.minimumFractionDigits = 1; + + _chartView.delegate = self; + + _chartView.descriptionText = @""; + _chartView.noDataTextDescription = @"You need to provide data for the chart."; + + _chartView.drawBarShadowEnabled = NO; + _chartView.drawValueAboveBarEnabled = YES; + + // if false values are only drawn for the stack sum, else each value is drawn + _chartView.drawValuesForWholeStackEnabled = YES; + // scaling can now only be done on x- and y-axis separately + _chartView.pinchZoomEnabled = NO; + + _chartView.drawBarShadowEnabled = NO; + _chartView.drawValueAboveBarEnabled = YES; + + _chartView.leftAxis.enabled = NO; + _chartView.rightAxis.startAtZeroEnabled = NO; + _chartView.rightAxis.customAxisMax = 25.0; + _chartView.rightAxis.customAxisMin = -25.0; + _chartView.rightAxis.labelCount = 7; + _chartView.rightAxis.valueFormatter = customFormatter; + _chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f]; + + ChartXAxis *xAxis = _chartView.xAxis; + xAxis.labelPosition = XAxisLabelPositionBothSided; + xAxis.drawGridLinesEnabled = NO; + xAxis.drawAxisLineEnabled = NO; + _chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f]; + + ChartLegend *l = _chartView.legend; + l.position = ChartLegendPositionBelowChartRight; + l.formSize = 8.f; + l.formToTextSpace = 4.f; + l.xEntrySpace = 6.f; + + NSMutableArray *yValues = [NSMutableArray array]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-10, @10 ] xIndex: 0]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-12, @13 ] xIndex: 1]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-15, @15 ] xIndex: 2]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-17, @17 ] xIndex: 3]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-19, @20 ] xIndex: 4]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-19, @19 ] xIndex: 5]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-16, @16 ] xIndex: 6]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-13, @14 ] xIndex: 7]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-10, @11 ] xIndex: 8]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-5, @6 ] xIndex: 9]]; + [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-1, @2 ] xIndex: 10]]; + + BarChartDataSet *set = [[BarChartDataSet alloc] initWithYVals:yValues label:@"Age Distribution"]; + set.valueFormatter = customFormatter; + set.valueFont = [UIFont systemFontOfSize:7.f]; + set.axisDependency = AxisDependencyRight; + set.barSpace = 0.5f; + set.colors = @[ + [UIColor colorWithRed:67/255.f green:67/255.f blue:72/255.f alpha:1.f], + [UIColor colorWithRed:124/255.f green:181/255.f blue:236/255.f alpha:1.f] + ]; + set.stackLabels = @[ + @"Men", @"Women" + ]; + + NSArray *xVals = @[ @"0-10", @"10-20", @"20-30", @"30-40", @"40-50", @"50-60", @"60-70", @"70-80", @"80-90", @"90-100", @"100+" ]; + + BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSet:set]; + _chartView.data = data; + [_chartView setNeedsDisplay]; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +- (void)setDataCount:(int)count range:(double)range +{ + NSMutableArray *xVals = [[NSMutableArray alloc] init]; + + for (int i = 0; i < count; i++) + { + [xVals addObject:months[i % 12]]; + } + + NSMutableArray *yVals = [[NSMutableArray alloc] init]; + + for (int i = 0; i < count; i++) + { + double mult = (range + 1); + double val = (double) (arc4random_uniform(mult)); + [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]]; + } + + BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"]; + set1.barSpace = 0.35; + + NSMutableArray *dataSets = [[NSMutableArray alloc] init]; + [dataSets addObject:set1]; + + BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets]; + [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]]; + + _chartView.data = data; +} + +- (void)optionTapped:(NSString *)key +{ + if ([key isEqualToString:@"toggleValues"]) + { + for (ChartDataSet *set in _chartView.data.dataSets) + { + set.drawValuesEnabled = !set.isDrawValuesEnabled; + } + + [_chartView setNeedsDisplay]; + } + + if ([key isEqualToString:@"toggleHighlight"]) + { + _chartView.highlightEnabled = !_chartView.isHighlightEnabled; + + [_chartView setNeedsDisplay]; + } + + if ([key isEqualToString:@"toggleHighlightArrow"]) + { + _chartView.drawHighlightArrowEnabled = !_chartView.isDrawHighlightArrowEnabled; + + [_chartView setNeedsDisplay]; + } + + if ([key isEqualToString:@"toggleStartZero"]) + { + _chartView.leftAxis.startAtZeroEnabled = !_chartView.leftAxis.isStartAtZeroEnabled; + _chartView.rightAxis.startAtZeroEnabled = !_chartView.rightAxis.isStartAtZeroEnabled; + + [_chartView notifyDataSetChanged]; + } + + if ([key isEqualToString:@"animateX"]) + { + [_chartView animateWithXAxisDuration:3.0]; + } + + if ([key isEqualToString:@"animateY"]) + { + [_chartView animateWithYAxisDuration:3.0]; + } + + if ([key isEqualToString:@"animateXY"]) + { + [_chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0]; + } + + if ([key isEqualToString:@"saveToGallery"]) + { + [_chartView saveToCameraRoll]; + } + + if ([key isEqualToString:@"togglePinchZoom"]) + { + _chartView.pinchZoomEnabled = !_chartView.isPinchZoomEnabled; + + [_chartView setNeedsDisplay]; + } + + if ([key isEqualToString:@"toggleAutoScaleMinMax"]) + { + _chartView.autoScaleMinMaxEnabled = !_chartView.isAutoScaleMinMaxEnabled; + [_chartView notifyDataSetChanged]; + } +} + +#pragma mark - ChartViewDelegate + +- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight +{ + NSLog(@"chartValueSelected"); +} + +- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView +{ + NSLog(@"chartValueNothingSelected"); +} + +@end diff --git a/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.xib b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.xib new file mode 100644 index 0000000000..6c85605703 --- /dev/null +++ b/ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.xib @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +