-
Notifications
You must be signed in to change notification settings - Fork 1
/
S7GraphView.m
376 lines (272 loc) · 10.1 KB
/
S7GraphView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// S7GraphView.m
// S7Touch
//
// Created by Aleks Nesterow on 9/27/09.
//
// Thanks to http://snobit.habrahabr.ru/ for releasing sources for his
// Cocoa component named GraphView.
//
// Copyright © 2009, 7touchGroup, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the 7touchGroup, Inc. nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY 7touchGroup, Inc. "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL 7touchGroup, Inc. BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "S7GraphView.h"
@interface S7GraphView (PrivateMethods)
- (void)initializeComponent;
@end
@implementation S7GraphView
+ (UIColor *)colorByIndex:(NSInteger)index {
UIColor *color;
switch (index) {
case 0: color = RGB(5, 141, 191);
break;
case 1: color = RGB(80, 180, 50);
break;
case 2: color = RGB(255, 102, 0);
break;
case 3: color = RGB(255, 158, 1);
break;
case 4: color = RGB(252, 210, 2);
break;
case 5: color = RGB(248, 255, 1);
break;
case 6: color = RGB(176, 222, 9);
break;
case 7: color = RGB(106, 249, 196);
break;
case 8: color = RGB(178, 222, 255);
break;
case 9: color = RGB(4, 210, 21);
break;
default: color = RGB(204, 204, 204);
break;
}
return color;
}
@synthesize dataSource = _dataSource, xValuesFormatter = _xValuesFormatter, yValuesFormatter = _yValuesFormatter;
@synthesize drawAxisX = _drawAxisX, drawAxisY = _drawAxisY, drawGridX = _drawGridX, drawGridY = _drawGridY;
@synthesize xValuesColor = _xValuesColor, yValuesColor = _yValuesColor, gridXColor = _gridXColor, gridYColor = _gridYColor;
@synthesize drawInfo = _drawInfo, info = _info, infoColor = _infoColor;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initializeComponent];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
[self initializeComponent];
}
return self;
}
- (void)dealloc {
[_xValuesFormatter release];
[_yValuesFormatter release];
[_xValuesColor release];
[_yValuesColor release];
[_gridXColor release];
[_gridYColor release];
[_info release];
[_infoColor release];
[super dealloc];
}
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
CGContextFillRect(c, rect);
NSUInteger numberOfPlots = [self.dataSource graphViewNumberOfPlots:self];
if (!numberOfPlots) {
return;
}
CGFloat offsetX = _drawAxisY ? 60.0f : 10.0f;
CGFloat offsetY = (_drawAxisX || _drawInfo) ? 30.0f : 10.0f;
CGFloat minY = 0.0;
CGFloat maxY = 0.0;
UIFont *font = [UIFont systemFontOfSize:11.0f];
for (NSUInteger plotIndex = 0; plotIndex < numberOfPlots; plotIndex++) {
NSArray *values = [self.dataSource graphView:self yValuesForPlot:plotIndex];
for (NSUInteger valueIndex = 0; valueIndex < values.count; valueIndex++) {
if ([[values objectAtIndex:valueIndex] floatValue] > maxY) {
maxY = [[values objectAtIndex:valueIndex] floatValue];
}
}
}
if (maxY < 100) {
maxY = ceil(maxY / 10) * 10;
}
if (maxY > 100 && maxY < 1000) {
maxY = ceil(maxY / 100) * 100;
}
if (maxY > 1000 && maxY < 10000) {
maxY = ceil(maxY / 1000) * 1000;
}
if (maxY > 10000 && maxY < 100000) {
maxY = ceil(maxY / 10000) * 10000;
}
CGFloat step = (maxY - minY) / 5;
CGFloat stepY = (self.frame.size.height - (offsetY * 2)) / maxY;
for (NSUInteger i = 0; i < 6; i++) {
NSUInteger y = (i * step) * stepY;
NSUInteger value = i * step;
if (_drawGridY) {
CGFloat lineDash[2];
lineDash[0] = 6.0f;
lineDash[1] = 6.0f;
CGContextSetLineDash(c, 0.0f, lineDash, 2);
CGContextSetLineWidth(c, 0.1f);
CGPoint startPoint = CGPointMake(offsetX, self.frame.size.height - y - offsetY);
CGPoint endPoint = CGPointMake(self.frame.size.width - offsetX, self.frame.size.height - y - offsetY);
CGContextMoveToPoint(c, startPoint.x, startPoint.y);
CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
CGContextClosePath(c);
CGContextSetStrokeColorWithColor(c, self.gridYColor.CGColor);
CGContextStrokePath(c);
}
if (i > 0 && _drawAxisY) {
NSNumber *valueToFormat = [NSNumber numberWithInt:value];
NSString *valueString;
if (_yValuesFormatter) {
valueString = [_yValuesFormatter stringForObjectValue:valueToFormat];
} else {
valueString = [valueToFormat stringValue];
}
[self.yValuesColor set];
CGRect valueStringRect = CGRectMake(0.0f, self.frame.size.height - y - offsetY, 50.0f, 20.0f);
[valueString drawInRect:valueStringRect withFont:font
lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentRight];
}
}
NSUInteger maxStep;
NSArray *xValues = [self.dataSource graphViewXValues:self];
NSUInteger xValuesCount = xValues.count;
if (xValuesCount > 5) {
NSUInteger stepCount = 5;
NSUInteger count = xValuesCount - 1;
for (NSUInteger i = 4; i < 8; i++) {
if (count % i == 0) {
stepCount = i;
}
}
step = xValuesCount / stepCount;
maxStep = stepCount + 1;
} else {
step = 1;
maxStep = xValuesCount;
}
CGFloat stepX = (self.frame.size.width - (offsetX * 2)) / (xValuesCount - 1);
for (NSUInteger i = 0; i < maxStep; i++) {
NSUInteger x = (i * step) * stepX;
if (x > self.frame.size.width - (offsetX * 2)) {
x = self.frame.size.width - (offsetX * 2);
}
NSUInteger index = i * step;
if (index >= xValuesCount) {
index = xValuesCount - 1;
}
if (_drawGridX) {
CGFloat lineDash[2];
lineDash[0] = 6.0f;
lineDash[1] = 6.0f;
CGContextSetLineDash(c, 0.0f, lineDash, 2);
CGContextSetLineWidth(c, 0.1f);
CGPoint startPoint = CGPointMake(x + offsetX, offsetY);
CGPoint endPoint = CGPointMake(x + offsetX, self.frame.size.height - offsetY);
CGContextMoveToPoint(c, startPoint.x, startPoint.y);
CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
CGContextClosePath(c);
CGContextSetStrokeColorWithColor(c, self.gridXColor.CGColor);
CGContextStrokePath(c);
}
if (_drawAxisX) {
id valueToFormat = [xValues objectAtIndex:index];
NSString *valueString;
if (_xValuesFormatter) {
valueString = [_xValuesFormatter stringForObjectValue:valueToFormat];
} else {
valueString = [NSString stringWithFormat:@"%@", valueToFormat];
}
[self.xValuesColor set];
[valueString drawInRect:CGRectMake(x, self.frame.size.height - 20.0f, 120.0f, 20.0f) withFont:font
lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentCenter];
}
}
stepX = (self.frame.size.width - (offsetX * 2)) / (xValuesCount - 1);
CGContextSetLineDash(c, 0, NULL, 0);
for (NSUInteger plotIndex = 0; plotIndex < numberOfPlots; plotIndex++) {
NSArray *values = [self.dataSource graphView:self yValuesForPlot:plotIndex];
BOOL shouldFill = NO;
if ([self.dataSource respondsToSelector:@selector(graphView:shouldFillPlot:)]) {
shouldFill = [self.dataSource graphView:self shouldFillPlot:plotIndex];
}
CGColorRef plotColor = [S7GraphView colorByIndex:plotIndex].CGColor;
for (NSUInteger valueIndex = 0; valueIndex < values.count - 1; valueIndex++) {
NSUInteger x = valueIndex * stepX;
NSUInteger y = [[values objectAtIndex:valueIndex] intValue] * stepY;
CGContextSetLineWidth(c, 1.5f);
CGPoint startPoint = CGPointMake(x + offsetX, self.frame.size.height - y - offsetY);
x = (valueIndex + 1) * stepX;
y = [[values objectAtIndex:valueIndex + 1] intValue] * stepY;
CGPoint endPoint = CGPointMake(x + offsetX, self.frame.size.height - y - offsetY);
CGContextMoveToPoint(c, startPoint.x, startPoint.y);
CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
CGContextClosePath(c);
CGContextSetStrokeColorWithColor(c, plotColor);
CGContextStrokePath(c);
if (shouldFill) {
CGContextMoveToPoint(c, startPoint.x, self.frame.size.height - offsetY);
CGContextAddLineToPoint(c, startPoint.x, startPoint.y);
CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
CGContextAddLineToPoint(c, endPoint.x, self.frame.size.height - offsetY);
CGContextClosePath(c);
CGContextSetFillColorWithColor(c, plotColor);
CGContextFillPath(c);
}
}
}
if (_drawInfo) {
font = [UIFont boldSystemFontOfSize:13.0f];
[self.infoColor set];
[_info drawInRect:CGRectMake(0.0f, 5.0f, self.frame.size.width, 20.0f) withFont:font
lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentCenter];
}
}
- (void)reloadData {
[self setNeedsDisplay];
}
#pragma mark PrivateMethods
- (void)initializeComponent {
_drawAxisX = YES;
_drawAxisY = YES;
_drawGridX = YES;
_drawGridY = YES;
_xValuesColor = [[UIColor blackColor] retain];
_yValuesColor = [[UIColor blackColor] retain];
_gridXColor = [[UIColor blackColor] retain];
_gridYColor = [[UIColor blackColor] retain];
_drawInfo = NO;
_infoColor = [[UIColor blackColor] retain];
}
@end