kevinzhow

New calculateChartPath method

@@ -59,4 +59,10 @@ @@ -59,4 +59,10 @@
59 59
60 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width; 60 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width;
61 61
  62 +/**
  63 + * Update Chart Value
  64 + */
  65 +
  66 +- (void)updateChartData:(NSArray *)data;
  67 +
62 @end 68 @end
@@ -150,6 +150,8 @@ @@ -150,6 +150,8 @@
150 } 150 }
151 } 151 }
152 152
  153 +#pragma mark - Touch at point
  154 +
153 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 155 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
154 { 156 {
155 [self touchPoint:touches withEvent:event]; 157 [self touchPoint:touches withEvent:event];
@@ -224,21 +226,66 @@ @@ -224,21 +226,66 @@
224 } 226 }
225 } 227 }
226 228
  229 +#pragma mark - Draw Chart
  230 +
227 - (void)strokeChart 231 - (void)strokeChart
228 { 232 {
229 _chartPath = [[NSMutableArray alloc] init]; 233 _chartPath = [[NSMutableArray alloc] init];
230 _pointPath = [[NSMutableArray alloc] init]; 234 _pointPath = [[NSMutableArray alloc] init];
231 235
  236 + [self calculateChartPath:_chartPath andPointsPath:_pointPath andPathKeyPoints:_pathPoints];
232 // Draw each line 237 // Draw each line
233 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { 238 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
234 PNLineChartData *chartData = self.chartData[lineIndex]; 239 PNLineChartData *chartData = self.chartData[lineIndex];
235 CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex]; 240 CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex];
236 CAShapeLayer *pointLayer = (CAShapeLayer *)self.chartPointArray[lineIndex]; 241 CAShapeLayer *pointLayer = (CAShapeLayer *)self.chartPointArray[lineIndex];
  242 + UIGraphicsBeginImageContext(self.frame.size);
  243 + // setup the color of the chart line
  244 + if (chartData.color) {
  245 + chartLine.strokeColor = [chartData.color CGColor];
  246 + } else {
  247 + chartLine.strokeColor = [PNGreen CGColor];
  248 + pointLayer.strokeColor = [PNGreen CGColor];
  249 + }
  250 +
  251 + UIBezierPath *progressline = [_chartPath objectAtIndex:lineIndex];
  252 + UIBezierPath *pointPath = [_pointPath objectAtIndex:lineIndex];
  253 +
  254 + chartLine.path = progressline.CGPath;
  255 + pointLayer.path = pointPath.CGPath;
  256 +
  257 + [CATransaction begin];
  258 + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  259 + pathAnimation.duration = 1.0;
  260 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  261 + pathAnimation.fromValue = @0.0f;
  262 + pathAnimation.toValue = @1.0f;
  263 +
  264 + [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  265 + chartLine.strokeEnd = 1.0;
  266 +
  267 + // if you want cancel the point animation, conment this code, the point will show immediately
  268 + if (chartData.inflexionPointStyle != PNLineChartPointStyleNone) {
  269 + [pointLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  270 + }
  271 +
  272 + [CATransaction commit];
  273 +
  274 + UIGraphicsEndImageContext();
  275 + }
  276 +}
  277 +
  278 +
  279 +- (void)calculateChartPath:(NSMutableArray *)chartPath andPointsPath:(NSMutableArray *)pointsPath andPathKeyPoints:(NSMutableArray *)pathPoints
  280 +{
  281 +
  282 + // Draw each line
  283 + for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
  284 + PNLineChartData *chartData = self.chartData[lineIndex];
237 285
238 CGFloat yValue; 286 CGFloat yValue;
239 CGFloat innerGrade; 287 CGFloat innerGrade;
240 288
241 - UIGraphicsBeginImageContext(self.frame.size);  
242 289
243 UIBezierPath *progressline = [UIBezierPath bezierPath]; 290 UIBezierPath *progressline = [UIBezierPath bezierPath];
244 [progressline setLineWidth:chartData.lineWidth]; 291 [progressline setLineWidth:chartData.lineWidth];
@@ -248,8 +295,9 @@ @@ -248,8 +295,9 @@
248 UIBezierPath *pointPath = [UIBezierPath bezierPath]; 295 UIBezierPath *pointPath = [UIBezierPath bezierPath];
249 [pointPath setLineWidth:chartData.lineWidth]; 296 [pointPath setLineWidth:chartData.lineWidth];
250 297
251 - [_chartPath addObject:progressline]; 298 +
252 - [_pointPath addObject:pointPath]; 299 + [chartPath insertObject:progressline atIndex:lineIndex];
  300 + [pointsPath insertObject:pointPath atIndex:lineIndex];
253 301
254 if (!_showLabel) { 302 if (!_showLabel) {
255 _chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight; 303 _chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight;
@@ -373,42 +421,13 @@ @@ -373,42 +421,13 @@
373 [linePointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]]; 421 [linePointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
374 } 422 }
375 423
376 - [_pathPoints addObject:[linePointsArray copy]]; 424 + [pathPoints addObject:[linePointsArray copy]];
377 -  
378 - // setup the color of the chart line  
379 - if (chartData.color) {  
380 - chartLine.strokeColor = [chartData.color CGColor];  
381 - } else {  
382 - chartLine.strokeColor = [PNGreen CGColor];  
383 - pointLayer.strokeColor = [PNGreen CGColor];  
384 - }  
385 -  
386 - [progressline stroke];  
387 -  
388 - chartLine.path = progressline.CGPath;  
389 - pointLayer.path = pointPath.CGPath;  
390 -  
391 - [CATransaction begin];  
392 - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];  
393 - pathAnimation.duration = 1.0;  
394 - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
395 - pathAnimation.fromValue = @0.0f;  
396 - pathAnimation.toValue = @1.0f;  
397 -  
398 - [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];  
399 - chartLine.strokeEnd = 1.0;  
400 -  
401 - // if you want cancel the point animation, conment this code, the point will show immediately  
402 - if (chartData.inflexionPointStyle != PNLineChartPointStyleNone) {  
403 - [pointLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];  
404 - }  
405 -  
406 - [CATransaction commit];  
407 425
408 - UIGraphicsEndImageContext();  
409 } 426 }
410 } 427 }
411 428
  429 +#pragma mark - Set Chart Data
  430 +
412 - (void)setChartData:(NSArray *)data 431 - (void)setChartData:(NSArray *)data
413 { 432 {
414 if (data != _chartData) { 433 if (data != _chartData) {
@@ -479,6 +498,13 @@ @@ -479,6 +498,13 @@
479 } 498 }
480 } 499 }
481 500
  501 +#pragma mark - Update Chart Data
  502 +
  503 +- (void)updateChartData:(NSArray *)data
  504 +{
  505 +
  506 +}
  507 +
482 #define IOS7_OR_LATER [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 508 #define IOS7_OR_LATER [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0
483 509
484 - (void)drawRect:(CGRect)rect 510 - (void)drawRect:(CGRect)rect