Skip to content

Commit 00d8081

Browse files
Setup graph.
1 parent fec5aa9 commit 00d8081

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed

GraphKit/Example/ExampleLineGraph.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ - (void)viewDidLoad {
2525
self.view.backgroundColor = [UIColor gk_cloudsColor];
2626

2727
self.data = @[
28-
@[@20, @40, @20, @60, @40, @140, @80],
29-
@[@40, @20, @60, @100, @60, @20, @60],
30-
@[@80, @60, @40, @160, @100, @40, @110],
31-
@[@120, @150, @80, @120, @140, @100, @0]
28+
// @[@20, @40, @20, @60, @40, @140, @80],
29+
// @[@40, @20, @60, @100, @60, @20, @60],
30+
// @[@80, @60, @40, @160, @100, @40, @110],
31+
// @[@120, @150, @80, @120, @140, @100, @0]
32+
// @[@620, @650, @580, @620, @540, @400, @0]
33+
@[@10, @4, @8, @2, @9, @3, @6],
34+
@[@1, @2, @3, @4, @5, @6, @10]
3235
];
3336
self.labels = @[@"2001", @"2002", @"2003", @"2004", @"2005", @"2006", @"2007"];
3437

3538
self.graph.dataSource = self;
3639
self.graph.lineWidth = 3.0;
3740

41+
// self.graph.startFromZero = YES;
42+
self.graph.valueLabelCount = 10;
43+
3844
[self.graph draw];
3945
}
4046

@@ -63,7 +69,7 @@ - (NSInteger)numberOfLines {
6369

6470
- (UIColor *)colorForLineAtIndex:(NSInteger)index {
6571
id colors = @[[UIColor gk_turquoiseColor],
66-
[UIColor gk_peterRiverColor],
72+
[UIColor clearColor],
6773
[UIColor gk_alizarinColor],
6874
[UIColor gk_sunflowerColor]
6975
];

GraphKit/Example/ExampleLineGraph.xib

Lines changed: 4 additions & 4 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="320"/>
2020
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
21-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
21+
<color key="backgroundColor" name="controlHighlightColor" catalog="System" colorSpace="catalog"/>
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="368" 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="368" 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>

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: 38 additions & 8 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,7 +68,8 @@ - (void)_init {
6868
self.animationDuration = 1;
6969
self.lineWidth = kDefaultLineWidth;
7070
self.margin = kDefaultMargin;
71-
self.clipsToBounds = YES;
71+
self.valueLabelCount = kDefaultValueLabelCount;
72+
// self.clipsToBounds = YES;
7273
}
7374

7475
- (void)draw {
@@ -150,7 +151,8 @@ - (CGFloat)_stepX {
150151

151152
- (void)_constructValueLabels {
152153

153-
NSInteger count = kDefaultValueLabelCount + 1;
154+
// NSInteger count = self.valueLabelCount + 1;
155+
NSInteger count = self.valueLabelCount;
154156
id items = [NSMutableArray arrayWithCapacity:count];
155157

156158
for (NSInteger idx = 0; idx < count; idx++) {
@@ -164,7 +166,8 @@ - (void)_constructValueLabels {
164166
CGFloat value = [self _minValue] + (idx * [self _stepValueLabelY]);
165167
item.centerY = [self _positionYForLineValue:value];
166168

167-
item.text = [@(ceil(value)) stringValue];
169+
// item.text = [@(ceil(value)) stringValue];
170+
item.text = [@(value) stringValue];
168171

169172
[items addObject:item];
170173
[self addSubview:item];
@@ -173,7 +176,8 @@ - (void)_constructValueLabels {
173176
}
174177

175178
- (CGFloat)_stepValueLabelY {
176-
return (([self _maxValue] - [self _minValue]) / kDefaultValueLabelCount);
179+
// return (([self _maxValue] - [self _minValue]) / self.valueLabelCount);
180+
return (([self _maxValue] - [self _minValue]) / (self.valueLabelCount - 1));
177181
}
178182

179183
- (CGFloat)_maxValue {
@@ -182,6 +186,7 @@ - (CGFloat)_maxValue {
182186
}
183187

184188
- (CGFloat)_minValue {
189+
if (self.startFromZero) return 1;
185190
id values = [self _allValues];
186191
return [[values mk_min] floatValue];
187192
}
@@ -207,6 +212,11 @@ - (CGFloat)_plotWidth {
207212
return (self.width - (2 * self.margin) - kAxisMargin);
208213
}
209214

215+
- (CGFloat)_plotHeight {
216+
// return (self.height - (kDefaultLabelHeight + kDefaultMarginBottom));
217+
return (self.height - (2 * kDefaultLabelHeight + kDefaultMarginBottom));
218+
}
219+
210220
- (void)_drawLines {
211221
for (NSInteger idx = 0; idx < [self.dataSource numberOfLines]; idx++) {
212222
[self _drawLineAtIndex:idx];
@@ -253,9 +263,29 @@ - (void)_drawLineAtIndex:(NSInteger)index {
253263
}
254264

255265
- (CGFloat)_positionYForLineValue:(CGFloat)value {
256-
CGFloat result = (self.height - value);
257-
result -= kDefaultLabelHeight + kDefaultMarginBottom;
266+
267+
// CGFloat result = ((value - 0) / [self _plotHeight] - 0);
268+
// result = (result * ([self _maxValue] - [self _minValue])) + [self _minValue];
269+
//
270+
// result = ([self _plotHeight] - result);
271+
//
272+
// return result;
273+
274+
275+
CGFloat scale = (value - [self _minValue]) / ([self _maxValue] - [self _minValue]);
276+
CGFloat result = [self _plotHeight] * scale;
277+
result = ([self _plotHeight] - result);
278+
result += kDefaultLabelHeight;
258279
return result;
280+
281+
// CGFloat result = ([self _plotHeight] - value);
282+
//// result -= kDefaultLabelHeight + kDefaultMarginBottom;
283+
// return result;
284+
285+
286+
// CGFloat result = (self.height - value);
287+
// result -= kDefaultLabelHeight + kDefaultMarginBottom;
288+
// return result;
259289
}
260290

261291
- (UIBezierPath *)_bezierPathWith:(CGFloat)value {

0 commit comments

Comments
 (0)