Skip to content

Commit

Permalink
Demo view controller for negative stacked bars
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Jun 22, 2015
1 parent fe28241 commit a00453f
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ChartsDemo/ChartsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -126,6 +128,9 @@
5B8EAF261AB32CF5009697AA /* DemoBaseViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoBaseViewController.m; sourceTree = "<group>"; };
5B8EAF2F1AB32E15009697AA /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
5B8EAF321AB32EA0009697AA /* Charts.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Charts.xcodeproj; path = ../Charts/Charts.xcodeproj; sourceTree = "<group>"; };
5B96243E1B38608C007763E2 /* NegativeStackedBarChartViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NegativeStackedBarChartViewController.h; sourceTree = "<group>"; };
5B96243F1B38608C007763E2 /* NegativeStackedBarChartViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NegativeStackedBarChartViewController.m; sourceTree = "<group>"; };
5B9624401B38608C007763E2 /* NegativeStackedBarChartViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NegativeStackedBarChartViewController.xib; sourceTree = "<group>"; };
5BD47E5A1ABB0263008FCEC6 /* BalloonMarker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BalloonMarker.swift; sourceTree = "<group>"; };
5BD47E5C1ABB0273008FCEC6 /* ChartsDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ChartsDemo-Bridging-Header.h"; sourceTree = "<group>"; };
5BD47E5D1ABB3C91008FCEC6 /* LineChart2ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LineChart2ViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
};
Expand Down
6 changes: 6 additions & 0 deletions ChartsDemo/Classes/DemoListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Classes/Demos/MultipleBarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.h
Original file line number Diff line number Diff line change
@@ -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 <UIKit/UIKit.h>
#import "DemoBaseViewController.h"
#import <Charts/Charts.h>

@interface NegativeStackedBarChartViewController : DemoBaseViewController

@end
236 changes: 236 additions & 0 deletions ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.m
Original file line number Diff line number Diff line change
@@ -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 () <ChartViewDelegate>

@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
47 changes: 47 additions & 0 deletions ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="NegativeStackedBarChartViewController">
<connections>
<outlet property="chartView" destination="Oqd-Ej-1xl" id="tSA-aU-J9W"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Zdz-nd-u7k">
<rect key="frame" x="289" y="4" width="78" height="35"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="17"/>
<inset key="contentEdgeInsets" minX="10" minY="7" maxX="10" maxY="7"/>
<state key="normal" title="Options">
<color key="titleColor" red="0.29803921570000003" green="0.56078431370000004" blue="0.74117647060000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="optionsButtonTapped:" destination="-1" eventType="touchUpInside" id="ig5-8o-JhO"/>
</connections>
</button>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Oqd-Ej-1xl" customClass="HorizontalBarChartView" customModule="Charts">
<rect key="frame" x="0.0" y="47" width="375" height="620"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" red="0.94117647059999998" green="0.94117647059999998" blue="0.94117647059999998" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="Oqd-Ej-1xl" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" constant="47" id="3NA-if-rAO"/>
<constraint firstItem="Oqd-Ej-1xl" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="6Mc-iO-BuY"/>
<constraint firstItem="Zdz-nd-u7k" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" constant="4" id="QYu-uI-rC8"/>
<constraint firstAttribute="bottom" secondItem="Oqd-Ej-1xl" secondAttribute="bottom" id="caj-8a-75N"/>
<constraint firstAttribute="trailing" secondItem="Zdz-nd-u7k" secondAttribute="trailing" constant="8" id="hkP-f4-aXC"/>
<constraint firstAttribute="trailing" secondItem="Oqd-Ej-1xl" secondAttribute="trailing" id="mC3-xy-2CS"/>
</constraints>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
<point key="canvasLocation" x="157.5" y="222.5"/>
</view>
</objects>
</document>

0 comments on commit a00453f

Please sign in to comment.