Skip to content

Commit 7743562

Browse files
Merge branch 'release/1.1.0'
2 parents 1a1b382 + d42b477 commit 7743562

File tree

10 files changed

+92
-18
lines changed

10 files changed

+92
-18
lines changed

GraphKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = "GraphKit"
4-
s.version = "1.0.0"
4+
s.version = "1.1.0"
55
s.summary = "A lightweight library of animated charts for iOS."
66
s.homepage = "https://github.com/michalkonturek/GraphKit"
77
s.license = 'MIT'
@@ -15,7 +15,7 @@ Pod::Spec.new do |s|
1515
s.social_media_url = 'https://twitter.com/michalkonturek'
1616
s.source = {
1717
:git => "https://github.com/michalkonturek/GraphKit.git",
18-
:tag => "1.0.0"
18+
:tag => "1.1.0"
1919
}
2020

2121
s.source_files = 'Source/**/*.{h,m}'

GraphKit/Example/ExampleBarGraphVC.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ - (UIColor *)colorForBarAtIndex:(NSInteger)index {
8080
return [colors objectAtIndex:index];
8181
}
8282

83+
//- (UIColor *)colorForBarBackgroundAtIndex:(NSInteger)index {
84+
// return [UIColor redColor];
85+
//}
86+
8387
- (CFTimeInterval)animationDurationForBarAtIndex:(NSInteger)index {
8488
CGFloat percentage = [[self valueForBarAtIndex:index] doubleValue];
8589
percentage = (percentage / 100);

GraphKit/Example/ExampleLineGraph.m

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,68 @@ - (void)viewDidLoad {
2424

2525
self.view.backgroundColor = [UIColor gk_cloudsColor];
2626

27+
[self _setupExampleGraph];
28+
// [self _setupTestingGraphLow];
29+
// [self _setupTestingGraphHigh];
30+
}
31+
32+
- (void)_setupExampleGraph {
33+
2734
self.data = @[
2835
@[@20, @40, @20, @60, @40, @140, @80],
2936
@[@40, @20, @60, @100, @60, @20, @60],
3037
@[@80, @60, @40, @160, @100, @40, @110],
31-
@[@120, @150, @80, @120, @140, @100, @0]
38+
@[@120, @150, @80, @120, @140, @100, @0],
39+
// @[@620, @650, @580, @620, @540, @400, @0]
40+
];
41+
42+
self.labels = @[@"2001", @"2002", @"2003", @"2004", @"2005", @"2006", @"2007"];
43+
44+
self.graph.dataSource = self;
45+
self.graph.lineWidth = 3.0;
46+
47+
self.graph.valueLabelCount = 6;
48+
49+
[self.graph draw];
50+
}
51+
52+
- (void)_setupTestingGraphLow {
53+
54+
/*
55+
A custom max and min values can be achieved by adding
56+
values for another line and setting its color to clear.
57+
*/
58+
59+
self.data = @[
60+
@[@10, @4, @8, @2, @9, @3, @6],
61+
@[@1, @2, @3, @4, @5, @6, @10]
62+
];
63+
64+
self.labels = @[@"2001", @"2002", @"2003", @"2004", @"2005", @"2006", @"2007"];
65+
66+
self.graph.dataSource = self;
67+
self.graph.lineWidth = 3.0;
68+
69+
// self.graph.startFromZero = YES;
70+
self.graph.valueLabelCount = 10;
71+
72+
[self.graph draw];
73+
}
74+
75+
- (void)_setupTestingGraphHigh {
76+
77+
self.data = @[
78+
@[@1000, @2000, @3000, @4000, @5000, @6000, @10000]
3279
];
80+
3381
self.labels = @[@"2001", @"2002", @"2003", @"2004", @"2005", @"2006", @"2007"];
3482

3583
self.graph.dataSource = self;
3684
self.graph.lineWidth = 3.0;
3785

86+
// self.graph.startFromZero = YES;
87+
self.graph.valueLabelCount = 10;
88+
3889
[self.graph draw];
3990
}
4091

@@ -43,7 +94,7 @@ - (void)didReceiveMemoryWarning {
4394
}
4495

4596

46-
#pragma mark -
97+
#pragma mark - Event Handlers
4798

4899
- (IBAction)onButtonDraw:(id)sender {
49100
[self.graph reset];

GraphKit/Example/ExampleLineGraph.xib

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1717
<subviews>
1818
<view contentMode="scaleToFill" id="4t0-gr-3LL" customClass="GKLineGraph">
19-
<rect key="frame" x="0.0" y="40" width="320" height="200"/>
19+
<rect key="frame" x="0.0" y="40" width="320" height="220"/>
2020
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
2121
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
2222
</view>
2323
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="muc-bY-Ywz" customClass="BButton">
24-
<rect key="frame" x="76" y="257" width="80" height="30"/>
24+
<rect key="frame" x="76" y="268" width="80" height="30"/>
2525
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2626
<state key="normal" title="Draw">
2727
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
@@ -31,7 +31,7 @@
3131
</connections>
3232
</button>
3333
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Bog-3S-GhQ" customClass="BButton">
34-
<rect key="frame" x="164" y="257" width="80" height="30"/>
34+
<rect key="frame" x="164" y="268" width="80" height="30"/>
3535
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
3636
<state key="normal" title="Reset">
3737
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>

GraphKit/GraphKit/GraphKit-Info.plist

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@
2424
<string>1.0</string>
2525
<key>LSRequiresIPhoneOS</key>
2626
<true/>
27-
<key>UIViewControllerBasedStatusBarAppearance</key>
28-
<false/>
2927
<key>UIRequiredDeviceCapabilities</key>
3028
<array>
3129
<string>armv7</string>
3230
</array>
3331
<key>UISupportedInterfaceOrientations</key>
3432
<array>
3533
<string>UIInterfaceOrientationPortrait</string>
36-
<string>UIInterfaceOrientationLandscapeLeft</string>
37-
<string>UIInterfaceOrientationLandscapeRight</string>
34+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
3835
</array>
36+
<key>UIViewControllerBasedStatusBarAppearance</key>
37+
<false/>
3938
</dict>
4039
</plist>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Please see [example][BAR].
5858

5959
@optional
6060
- (UIColor *)colorForBarAtIndex:(NSInteger)index;
61+
- (UIColor *)colorForBarBackgroundAtIndex:(NSInteger)index;
6162
- (CFTimeInterval)animationDurationForBarAtIndex:(NSInteger)index;
6263
- (NSString *)titleForBarAtIndex:(NSInteger)index;
6364
```

Source/BarGraph/GKBarGraph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
@optional
5656
- (UIColor *)colorForBarAtIndex:(NSInteger)index;
57+
- (UIColor *)colorForBarBackgroundAtIndex:(NSInteger)index;
5758
- (CFTimeInterval)animationDurationForBarAtIndex:(NSInteger)index;
5859

5960
- (NSString *)titleForBarAtIndex:(NSInteger)index;

Source/BarGraph/GKBarGraph.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ - (void)_drawBars {
215215
item.foregroundColor = [source colorForBarAtIndex:idx];
216216
}
217217

218+
if ([source respondsToSelector:@selector(colorForBarBackgroundAtIndex:)]) {
219+
item.backgroundColor = [source colorForBarBackgroundAtIndex:idx];
220+
}
221+
218222
item.percentage = [[source valueForBarAtIndex:idx] doubleValue];
219223
idx++;
220224
}];

Source/LineGraph/GKLineGraph.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@
3535
@property (nonatomic, assign) id<GKLineGraphDataSource> dataSource;
3636

3737
@property (nonatomic, assign) CGFloat lineWidth;
38-
3938
@property (nonatomic, assign) CGFloat margin;
4039

40+
@property (nonatomic, assign) NSInteger valueLabelCount;
41+
//@property (nonatomic, strong) NSNumber *maxValue;
42+
43+
@property (nonatomic, assign) CGFloat *minValue;
44+
@property (nonatomic, assign) BOOL startFromZero;
45+
4146
- (void)draw;
4247
- (void)reset;
4348

Source/LineGraph/GKLineGraph.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
#import <MKFoundationKit/NSArray+MK.h>
3030

3131
static CGFloat kDefaultLabelWidth = 40.0;
32-
static CGFloat kDefaultLabelHeight = 15.0;
32+
static CGFloat kDefaultLabelHeight = 12.0;
3333
static NSInteger kDefaultValueLabelCount = 5;
3434

3535
static CGFloat kDefaultLineWidth = 3.0;
3636
static CGFloat kDefaultMargin = 10.0;
37-
static CGFloat kDefaultMarginBottom = 10.0;
37+
static CGFloat kDefaultMarginBottom = 20.0;
3838

3939
static CGFloat kAxisMargin = 50.0;
4040

@@ -68,6 +68,7 @@ - (void)_init {
6868
self.animationDuration = 1;
6969
self.lineWidth = kDefaultLineWidth;
7070
self.margin = kDefaultMargin;
71+
self.valueLabelCount = kDefaultValueLabelCount;
7172
self.clipsToBounds = YES;
7273
}
7374

@@ -150,7 +151,7 @@ - (CGFloat)_stepX {
150151

151152
- (void)_constructValueLabels {
152153

153-
NSInteger count = kDefaultValueLabelCount + 1;
154+
NSInteger count = self.valueLabelCount;
154155
id items = [NSMutableArray arrayWithCapacity:count];
155156

156157
for (NSInteger idx = 0; idx < count; idx++) {
@@ -165,6 +166,7 @@ - (void)_constructValueLabels {
165166
item.centerY = [self _positionYForLineValue:value];
166167

167168
item.text = [@(ceil(value)) stringValue];
169+
// item.text = [@(value) stringValue];
168170

169171
[items addObject:item];
170172
[self addSubview:item];
@@ -173,7 +175,7 @@ - (void)_constructValueLabels {
173175
}
174176

175177
- (CGFloat)_stepValueLabelY {
176-
return (([self _maxValue] - [self _minValue]) / kDefaultValueLabelCount);
178+
return (([self _maxValue] - [self _minValue]) / (self.valueLabelCount - 1));
177179
}
178180

179181
- (CGFloat)_maxValue {
@@ -182,6 +184,7 @@ - (CGFloat)_maxValue {
182184
}
183185

184186
- (CGFloat)_minValue {
187+
if (self.startFromZero) return 0;
185188
id values = [self _allValues];
186189
return [[values mk_min] floatValue];
187190
}
@@ -207,6 +210,10 @@ - (CGFloat)_plotWidth {
207210
return (self.width - (2 * self.margin) - kAxisMargin);
208211
}
209212

213+
- (CGFloat)_plotHeight {
214+
return (self.height - (2 * kDefaultLabelHeight + kDefaultMarginBottom));
215+
}
216+
210217
- (void)_drawLines {
211218
for (NSInteger idx = 0; idx < [self.dataSource numberOfLines]; idx++) {
212219
[self _drawLineAtIndex:idx];
@@ -253,8 +260,10 @@ - (void)_drawLineAtIndex:(NSInteger)index {
253260
}
254261

255262
- (CGFloat)_positionYForLineValue:(CGFloat)value {
256-
CGFloat result = (self.height - value);
257-
result -= kDefaultLabelHeight + kDefaultMarginBottom;
263+
CGFloat scale = (value - [self _minValue]) / ([self _maxValue] - [self _minValue]);
264+
CGFloat result = [self _plotHeight] * scale;
265+
result = ([self _plotHeight] - result);
266+
result += kDefaultLabelHeight;
258267
return result;
259268
}
260269

0 commit comments

Comments
 (0)