-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo view controller for negative stacked bars
- Loading branch information
1 parent
fe28241
commit a00453f
Showing
6 changed files
with
320 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
236
ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
ChartsDemo/Classes/Demos/NegativeStackedBarChartViewController.xib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |