forked from Boris-Em/BEMSimpleLineGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEMLine.m
414 lines (341 loc) · 17 KB
/
BEMLine.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//
// BEMLine.m
// SimpleLineGraph
//
// Created by Bobo on 12/27/13. Updated by Sam Spencer on 1/11/14.
// Copyright (c) 2013 Boris Emorine. All rights reserved.
// Copyright (c) 2014 Sam Spencer.
//
#import "BEMLine.h"
#import "BEMSimpleLineGraphView.h"
#if CGFLOAT_IS_DOUBLE
#define CGFloatValue doubleValue
#else
#define CGFloatValue floatValue
#endif
@interface BEMLine()
@property (nonatomic, strong) NSMutableArray *points;
@end
@implementation BEMLine
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
_enableLeftReferenceFrameLine = YES;
_enableBottomReferenceFrameLine = YES;
_interpolateNullValues = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect {
//----------------------------//
//---- Draw Refrence Lines ---//
//----------------------------//
UIBezierPath *verticalReferenceLinesPath = [UIBezierPath bezierPath];
UIBezierPath *horizontalReferenceLinesPath = [UIBezierPath bezierPath];
UIBezierPath *referenceFramePath = [UIBezierPath bezierPath];
verticalReferenceLinesPath.lineCapStyle = kCGLineCapButt;
verticalReferenceLinesPath.lineWidth = 0.7;
horizontalReferenceLinesPath.lineCapStyle = kCGLineCapButt;
horizontalReferenceLinesPath.lineWidth = 0.7;
referenceFramePath.lineCapStyle = kCGLineCapButt;
referenceFramePath.lineWidth = 0.7;
if (self.enableRefrenceFrame == YES) {
if (self.enableBottomReferenceFrameLine) {
// Bottom Line
[referenceFramePath moveToPoint:CGPointMake(0, self.frame.size.height)];
[referenceFramePath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
}
if (self.enableLeftReferenceFrameLine) {
// Left Line
[referenceFramePath moveToPoint:CGPointMake(0+self.referenceLineWidth/4, self.frame.size.height)];
[referenceFramePath addLineToPoint:CGPointMake(0+self.referenceLineWidth/4, 0)];
}
if (self.enableTopReferenceFrameLine) {
// Top Line
[referenceFramePath moveToPoint:CGPointMake(0+self.referenceLineWidth/4, 0)];
[referenceFramePath addLineToPoint:CGPointMake(self.frame.size.width, 0)];
}
if (self.enableRightReferenceFrameLine) {
// Right Line
[referenceFramePath moveToPoint:CGPointMake(self.frame.size.width - self.referenceLineWidth/4, self.frame.size.height)];
[referenceFramePath addLineToPoint:CGPointMake(self.frame.size.width - self.referenceLineWidth/4, 0)];
}
}
if (self.enableRefrenceLines == YES) {
if (self.arrayOfVerticalRefrenceLinePoints.count > 0) {
for (NSNumber *xNumber in self.arrayOfVerticalRefrenceLinePoints) {
CGFloat xValue;
if (self.verticalReferenceHorizontalFringeNegation != 0.0) {
if ([self.arrayOfVerticalRefrenceLinePoints indexOfObject:xNumber] == 0) { // far left reference line
xValue = [xNumber floatValue] + self.verticalReferenceHorizontalFringeNegation;
} else if ([self.arrayOfVerticalRefrenceLinePoints indexOfObject:xNumber] == [self.arrayOfVerticalRefrenceLinePoints count]-1) { // far right reference line
xValue = [xNumber floatValue] - self.verticalReferenceHorizontalFringeNegation;
} else xValue = [xNumber floatValue];
} else xValue = [xNumber floatValue];
CGPoint initialPoint = CGPointMake(xValue, self.frame.size.height);
CGPoint finalPoint = CGPointMake(xValue, 0);
[verticalReferenceLinesPath moveToPoint:initialPoint];
[verticalReferenceLinesPath addLineToPoint:finalPoint];
}
}
if (self.arrayOfHorizontalRefrenceLinePoints.count > 0) {
for (NSNumber *yNumber in self.arrayOfHorizontalRefrenceLinePoints) {
CGPoint initialPoint = CGPointMake(0, [yNumber floatValue]);
CGPoint finalPoint = CGPointMake(self.frame.size.width, [yNumber floatValue]);
[horizontalReferenceLinesPath moveToPoint:initialPoint];
[horizontalReferenceLinesPath addLineToPoint:finalPoint];
}
}
}
//----------------------------//
//----- Draw Average Line ----//
//----------------------------//
UIBezierPath *averageLinePath = [UIBezierPath bezierPath];
if (self.averageLine.enableAverageLine == YES) {
averageLinePath.lineCapStyle = kCGLineCapButt;
averageLinePath.lineWidth = self.averageLine.width;
CGPoint initialPoint = CGPointMake(0, self.averageLineYCoordinate);
CGPoint finalPoint = CGPointMake(self.frame.size.width, self.averageLineYCoordinate);
[averageLinePath moveToPoint:initialPoint];
[averageLinePath addLineToPoint:finalPoint];
}
//----------------------------//
//------ Draw Graph Line -----//
//----------------------------//
// LINE
UIBezierPath *line = [UIBezierPath bezierPath];
UIBezierPath *fillTop;
UIBezierPath *fillBottom;
CGFloat xIndexScale = self.frame.size.width/([self.arrayOfPoints count] - 1);
self.points = [NSMutableArray arrayWithCapacity:self.arrayOfPoints.count];
for (int i = 0; i < self.arrayOfPoints.count; i++) {
CGPoint value = CGPointMake(xIndexScale * i, [self.arrayOfPoints[i] CGFloatValue]);
if (value.y != BEMNullGraphValue || !self.interpolateNullValues) {
[self.points addObject:[NSValue valueWithCGPoint:value]];
}
}
BOOL bezierStatus = self.bezierCurveIsEnabled;
if (self.arrayOfPoints.count <= 2 && self.bezierCurveIsEnabled == YES) bezierStatus = NO;
if (!self.disableMainLine && bezierStatus) {
line = [BEMLine quadCurvedPathWithPoints:self.points];
fillBottom = [BEMLine quadCurvedPathWithPoints:self.bottomPointsArray];
fillTop = [BEMLine quadCurvedPathWithPoints:self.topPointsArray];
} else if (!self.disableMainLine && !bezierStatus) {
line = [BEMLine linesToPoints:self.points];
fillBottom = [BEMLine linesToPoints:self.bottomPointsArray];
fillTop = [BEMLine linesToPoints:self.topPointsArray];
} else {
fillBottom = [BEMLine linesToPoints:self.bottomPointsArray];
fillTop = [BEMLine linesToPoints:self.topPointsArray];
}
//----------------------------//
//----- Draw Fill Colors -----//
//----------------------------//
[self.topColor set];
[fillTop fillWithBlendMode:kCGBlendModeNormal alpha:self.topAlpha];
[self.bottomColor set];
[fillBottom fillWithBlendMode:kCGBlendModeNormal alpha:self.bottomAlpha];
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (self.topGradient != nil) {
CGContextSaveGState(ctx);
CGContextAddPath(ctx, [fillTop CGPath]);
CGContextClip(ctx);
CGContextDrawLinearGradient(ctx, self.topGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillTop.bounds)), 0);
CGContextRestoreGState(ctx);
}
if (self.bottomGradient != nil) {
CGContextSaveGState(ctx);
CGContextAddPath(ctx, [fillBottom CGPath]);
CGContextClip(ctx);
CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);
CGContextRestoreGState(ctx);
}
//----------------------------//
//------ Animate Drawing -----//
//----------------------------//
if (self.enableRefrenceLines == YES) {
CAShapeLayer *verticalReferenceLinesPathLayer = [CAShapeLayer layer];
verticalReferenceLinesPathLayer.frame = self.bounds;
verticalReferenceLinesPathLayer.path = verticalReferenceLinesPath.CGPath;
verticalReferenceLinesPathLayer.opacity = self.lineAlpha == 0 ? 0.1 : self.lineAlpha/2;
verticalReferenceLinesPathLayer.fillColor = nil;
verticalReferenceLinesPathLayer.lineWidth = self.referenceLineWidth/2;
if (self.lineDashPatternForReferenceYAxisLines) {
verticalReferenceLinesPathLayer.lineDashPattern = self.lineDashPatternForReferenceYAxisLines;
}
if (self.refrenceLineColor) {
verticalReferenceLinesPathLayer.strokeColor = self.refrenceLineColor.CGColor;
} else {
verticalReferenceLinesPathLayer.strokeColor = self.color.CGColor;
}
if (self.animationTime > 0)
[self animateForLayer:verticalReferenceLinesPathLayer withAnimationType:self.animationType isAnimatingReferenceLine:YES];
[self.layer addSublayer:verticalReferenceLinesPathLayer];
CAShapeLayer *horizontalReferenceLinesPathLayer = [CAShapeLayer layer];
horizontalReferenceLinesPathLayer.frame = self.bounds;
horizontalReferenceLinesPathLayer.path = horizontalReferenceLinesPath.CGPath;
horizontalReferenceLinesPathLayer.opacity = self.lineAlpha == 0 ? 0.1 : self.lineAlpha/2;
horizontalReferenceLinesPathLayer.fillColor = nil;
horizontalReferenceLinesPathLayer.lineWidth = self.referenceLineWidth/2;
if(self.lineDashPatternForReferenceXAxisLines) {
horizontalReferenceLinesPathLayer.lineDashPattern = self.lineDashPatternForReferenceXAxisLines;
}
if (self.refrenceLineColor) {
horizontalReferenceLinesPathLayer.strokeColor = self.refrenceLineColor.CGColor;
} else {
horizontalReferenceLinesPathLayer.strokeColor = self.color.CGColor;
}
if (self.animationTime > 0)
[self animateForLayer:horizontalReferenceLinesPathLayer withAnimationType:self.animationType isAnimatingReferenceLine:YES];
[self.layer addSublayer:horizontalReferenceLinesPathLayer];
}
CAShapeLayer *referenceLinesPathLayer = [CAShapeLayer layer];
referenceLinesPathLayer.frame = self.bounds;
referenceLinesPathLayer.path = referenceFramePath.CGPath;
referenceLinesPathLayer.opacity = self.lineAlpha == 0 ? 0.1 : self.lineAlpha/2;
referenceLinesPathLayer.fillColor = nil;
referenceLinesPathLayer.lineWidth = self.referenceLineWidth/2;
if (self.refrenceLineColor) referenceLinesPathLayer.strokeColor = self.refrenceLineColor.CGColor;
else referenceLinesPathLayer.strokeColor = self.color.CGColor;
if (self.animationTime > 0)
[self animateForLayer:referenceLinesPathLayer withAnimationType:self.animationType isAnimatingReferenceLine:YES];
[self.layer addSublayer:referenceLinesPathLayer];
if (self.disableMainLine == NO) {
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = line.CGPath;
pathLayer.strokeColor = self.color.CGColor;
pathLayer.fillColor = nil;
pathLayer.opacity = self.lineAlpha;
pathLayer.lineWidth = self.lineWidth;
pathLayer.lineJoin = kCALineJoinBevel;
pathLayer.lineCap = kCALineCapRound;
if (self.animationTime > 0) [self animateForLayer:pathLayer withAnimationType:self.animationType isAnimatingReferenceLine:NO];
if (self.lineGradient) [self.layer addSublayer:[self backgroundGradientLayerForLayer:pathLayer]];
else [self.layer addSublayer:pathLayer];
}
if (self.averageLine.enableAverageLine == YES) {
CAShapeLayer *averageLinePathLayer = [CAShapeLayer layer];
averageLinePathLayer.frame = self.bounds;
averageLinePathLayer.path = averageLinePath.CGPath;
averageLinePathLayer.opacity = self.averageLine.alpha;
averageLinePathLayer.fillColor = nil;
averageLinePathLayer.lineWidth = self.averageLine.width;
if (self.averageLine.dashPattern) averageLinePathLayer.lineDashPattern = self.averageLine.dashPattern;
if (self.averageLine.color) averageLinePathLayer.strokeColor = self.averageLine.color.CGColor;
else averageLinePathLayer.strokeColor = self.color.CGColor;
if (self.animationTime > 0)
[self animateForLayer:averageLinePathLayer withAnimationType:self.animationType isAnimatingReferenceLine:NO];
[self.layer addSublayer:averageLinePathLayer];
}
}
- (NSArray *)topPointsArray {
CGPoint topPointZero = CGPointMake(0,0);
CGPoint topPointFull = CGPointMake(self.frame.size.width, 0);
NSMutableArray *topPoints = [NSMutableArray arrayWithArray:self.points];
[topPoints insertObject:[NSValue valueWithCGPoint:topPointZero] atIndex:0];
[topPoints addObject:[NSValue valueWithCGPoint:topPointFull]];
return topPoints;
}
- (NSArray *)bottomPointsArray {
CGPoint bottomPointZero = CGPointMake(0, self.frame.size.height);
CGPoint bottomPointFull = CGPointMake(self.frame.size.width, self.frame.size.height);
NSMutableArray *bottomPoints = [NSMutableArray arrayWithArray:self.points];
[bottomPoints insertObject:[NSValue valueWithCGPoint:bottomPointZero] atIndex:0];
[bottomPoints addObject:[NSValue valueWithCGPoint:bottomPointFull]];
return bottomPoints;
}
+ (UIBezierPath *)linesToPoints:(NSArray *)points {
UIBezierPath *path = [UIBezierPath bezierPath];
NSValue *value = points[0];
CGPoint p1 = [value CGPointValue];
[path moveToPoint:p1];
for (NSUInteger i = 1; i < points.count; i++) {
value = points[i];
CGPoint p2 = [value CGPointValue];
[path addLineToPoint:p2];
}
return path;
}
+ (UIBezierPath *)quadCurvedPathWithPoints:(NSArray *)points {
UIBezierPath *path = [UIBezierPath bezierPath];
NSValue *value = points[0];
CGPoint p1 = [value CGPointValue];
[path moveToPoint:p1];
if (points.count == 2) {
value = points[1];
CGPoint p2 = [value CGPointValue];
[path addLineToPoint:p2];
return path;
}
for (NSUInteger i = 1; i < points.count; i++) {
value = points[i];
CGPoint p2 = [value CGPointValue];
CGPoint midPoint = midPointForPoints(p1, p2);
[path addQuadCurveToPoint:midPoint controlPoint:controlPointForPoints(midPoint, p1)];
[path addQuadCurveToPoint:p2 controlPoint:controlPointForPoints(midPoint, p2)];
p1 = p2;
}
return path;
}
static CGPoint midPointForPoints(CGPoint p1, CGPoint p2) {
return CGPointMake((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
static CGPoint controlPointForPoints(CGPoint p1, CGPoint p2) {
CGPoint controlPoint = midPointForPoints(p1, p2);
CGFloat diffY = fabs(p2.y - controlPoint.y);
if (p1.y < p2.y)
controlPoint.y += diffY;
else if (p1.y > p2.y)
controlPoint.y -= diffY;
return controlPoint;
}
- (void)animateForLayer:(CAShapeLayer *)shapeLayer withAnimationType:(BEMLineAnimation)animationType isAnimatingReferenceLine:(BOOL)shouldHalfOpacity {
if (animationType == BEMLineAnimationNone) return;
else if (animationType == BEMLineAnimationFade) {
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
pathAnimation.duration = self.animationTime;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
if (shouldHalfOpacity == YES) pathAnimation.toValue = [NSNumber numberWithFloat:self.lineAlpha == 0 ? 0.1 : self.lineAlpha/2];
else pathAnimation.toValue = [NSNumber numberWithFloat:self.lineAlpha];
[shapeLayer addAnimation:pathAnimation forKey:@"opacity"];
return;
} else if (animationType == BEMLineAnimationExpand) {
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"lineWidth"];
pathAnimation.duration = self.animationTime;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:shapeLayer.lineWidth];
[shapeLayer addAnimation:pathAnimation forKey:@"lineWidth"];
return;
} else {
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = self.animationTime;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[shapeLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
return;
}
}
- (CALayer *)backgroundGradientLayerForLayer:(CAShapeLayer *)shapeLayer {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef imageCtx = UIGraphicsGetCurrentContext();
CGPoint start, end;
if (self.lineGradientDirection == BEMLineGradientDirectionHorizontal) {
start = CGPointMake(0, CGRectGetMidY(shapeLayer.bounds));
end = CGPointMake(CGRectGetMaxX(shapeLayer.bounds), CGRectGetMidY(shapeLayer.bounds));
} else {
start = CGPointMake(CGRectGetMidX(shapeLayer.bounds), 0);
end = CGPointMake(CGRectGetMidX(shapeLayer.bounds), CGRectGetMaxY(shapeLayer.bounds));
}
CGContextDrawLinearGradient(imageCtx, self.lineGradient, start, end, 0);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CALayer *gradientLayer = [CALayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.contents = (id)image.CGImage;
gradientLayer.mask = shapeLayer;
return gradientLayer;
}
@end