-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
PNLineChart.m
331 lines (248 loc) · 9.88 KB
/
PNLineChart.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
//
// PNLineChart.m
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import "PNLineChart.h"
#import "PNColor.h"
#import "PNChartLabel.h"
#import "PNLineChartData.h"
#import "PNLineChartDataItem.h"
//------------------------------------------------------------------------------------------------
// private interface declaration
//------------------------------------------------------------------------------------------------
@interface PNLineChart ()
@property (nonatomic) NSMutableArray *chartLineArray; // Array[CAShapeLayer]
@property (nonatomic) NSMutableArray *chartPath; //Array of line path, one for each line.
- (void)setDefaultValues;
@end
//------------------------------------------------------------------------------------------------
// public interface implementation
//------------------------------------------------------------------------------------------------
@implementation PNLineChart
#pragma mark initialization
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setDefaultValues];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setDefaultValues];
}
return self;
}
#pragma mark instance methods
- (void)setYLabels:(NSArray *)yLabels
{
CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
NSInteger index = 0;
NSInteger num = _yLabelNum + 1;
while (num > 0) {
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)];
[label setTextAlignment:NSTextAlignmentRight];
label.text = [NSString stringWithFormat:@"%1.f", _yValueMin + (yStep * index)];
[self addSubview:label];
index += 1;
num -= 1;
}
}
- (void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
NSString *labelText;
if (_showLabel) {
_xLabelWidth = _chartCavanWidth / [xLabels count];
for (int index = 0; index < xLabels.count; index++) {
labelText = xLabels[index];
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
}
}
else {
_xLabelWidth = (self.frame.size.width) / [xLabels count];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchPoint:touches withEvent:event];
[self touchKeyPoint:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchPoint:touches withEvent:event];
[self touchKeyPoint:touches withEvent:event];
}
- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get the point user touched
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
for (int p = _pathPoints.count - 1; p >= 0; p--) {
NSArray *linePointsArray = _pathPoints[p];
for (int i = 0; i < linePointsArray.count - 1; i += 1) {
CGPoint p1 = [linePointsArray[i] CGPointValue];
CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
// Closest distance from point to line
float distance = fabsf(((p2.x - p1.x) * (touchPoint.y - p1.y)) - ((p1.x - touchPoint.x) * (p1.y - p2.y)));
distance /= hypot(p2.x - p1.x, p1.y - p2.y);
if (distance <= 5.0) {
// Conform to delegate parameters, figure out what bezier path this CGPoint belongs to.
for (UIBezierPath *path in _chartPath) {
BOOL pointContainsPath = CGPathContainsPoint(path.CGPath, NULL, p1, NO);
if (pointContainsPath) {
[_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]];
return;
}
}
}
}
}
}
- (void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get the point user touched
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
for (int p = _pathPoints.count - 1; p >= 0; p--) {
NSArray *linePointsArray = _pathPoints[p];
for (int i = 0; i < linePointsArray.count - 1; i += 1) {
CGPoint p1 = [linePointsArray[i] CGPointValue];
CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
float distanceToP1 = fabsf(hypot(touchPoint.x - p1.x, touchPoint.y - p1.y));
float distanceToP2 = hypot(touchPoint.x - p2.x, touchPoint.y - p2.y);
float distance = MIN(distanceToP1, distanceToP2);
if (distance <= 10.0) {
[_delegate userClickedOnLineKeyPoint:touchPoint
lineIndex:p
andPointIndex:(distance == distanceToP2 ? i + 1 : i)];
return;
}
}
}
}
- (void)strokeChart
{
_chartPath = [[NSMutableArray alloc] init];
//Draw each line
for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
PNLineChartData *chartData = self.chartData[lineIndex];
CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex];
CGFloat yValue;
CGFloat innerGrade;
CGPoint point;
UIGraphicsBeginImageContext(self.frame.size);
UIBezierPath *progressline = [UIBezierPath bezierPath];
[_chartPath addObject:progressline];
if (!_showLabel) {
_chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight;
_chartCavanWidth = self.frame.size.width;
_chartMargin = 0.0;
_xLabelWidth = (_chartCavanWidth / ([_xLabels count] - 1));
}
NSMutableArray *linePointsArray = [[NSMutableArray alloc] init];
[progressline setLineWidth:3.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
for (NSUInteger i = 0; i < chartData.itemCount; i++) {
yValue = chartData.getData(i).y;
innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin);
point = CGPointMake(2 * _chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2));
if (i != 0) {
[progressline addLineToPoint:point];
}
[progressline moveToPoint:point];
[linePointsArray addObject:[NSValue valueWithCGPoint:point]];
}
[_pathPoints addObject:[linePointsArray copy]];
// setup the color of the chart line
if (chartData.color) {
chartLine.strokeColor = [chartData.color CGColor];
}
else {
chartLine.strokeColor = [PNGreen CGColor];
}
[progressline stroke];
chartLine.path = progressline.CGPath;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @0.0f;
pathAnimation.toValue = @1.0f;
[chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
chartLine.strokeEnd = 1.0;
UIGraphicsEndImageContext();
}
}
- (void)setChartData:(NSArray *)data
{
if (data != _chartData) {
NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count];
CGFloat yMax = 0.0f;
CGFloat yMin = MAXFLOAT;
CGFloat yValue;
// remove all shape layers before adding new ones
for (CALayer *layer in self.chartLineArray) {
[layer removeFromSuperlayer];
}
self.chartLineArray = [NSMutableArray arrayWithCapacity:data.count];
for (PNLineChartData *chartData in data) {
// create as many chart line layers as there are data-lines
CAShapeLayer *chartLine = [CAShapeLayer layer];
chartLine.lineCap = kCALineCapRound;
chartLine.lineJoin = kCALineJoinBevel;
chartLine.fillColor = [[UIColor whiteColor] CGColor];
chartLine.lineWidth = 3.0;
chartLine.strokeEnd = 0.0;
[self.layer addSublayer:chartLine];
[self.chartLineArray addObject:chartLine];
for (NSUInteger i = 0; i < chartData.itemCount; i++) {
yValue = chartData.getData(i).y;
[yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]];
yMax = fmaxf(yMax, yValue);
yMin = fminf(yMin, yValue);
}
}
// Min value for Y label
if (yMax < 5) {
yMax = 5.0f;
}
if (yMin < 0) {
yMin = 0.0f;
}
_yValueMin = yMin;
_yValueMax = yMax;
_chartData = data;
if (_showLabel) {
[self setYLabels:yLabelsArray];
}
[self setNeedsDisplay];
}
}
#pragma mark private methods
- (void)setDefaultValues
{
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
self.chartLineArray = [NSMutableArray new];
_showLabel = YES;
_pathPoints = [[NSMutableArray alloc] init];
self.userInteractionEnabled = YES;
_yLabelNum = 5.0;
_yLabelHeight = [[[[PNChartLabel alloc] init] font] pointSize];
_chartMargin = 30;
_chartCavanWidth = self.frame.size.width - _chartMargin * 2;
_chartCavanHeight = self.frame.size.height - _chartMargin * 2;
}
@end