Emma Makinson
Committed by Edu Caselles

Makes animation optional on Line Chart

@@ -74,6 +74,9 @@ @@ -74,6 +74,9 @@
74 */ 74 */
75 @property (nonatomic, copy) NSString* (^yLabelBlockFormatter)(CGFloat); 75 @property (nonatomic, copy) NSString* (^yLabelBlockFormatter)(CGFloat);
76 76
  77 +/** Display the line chart with or without animation. Default is YES. **/
  78 +@property (nonatomic) BOOL displayAnimated;
  79 +
77 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width; 80 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width;
78 81
79 /** 82 /**
@@ -22,6 +22,8 @@ @@ -22,6 +22,8 @@
22 @property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line 22 @property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line
23 @property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line 23 @property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line
24 24
  25 +@property (nonatomic) CABasicAnimation *pathAnimation; // will be set to nil if _displayAnimation is NO
  26 +
25 // display grade 27 // display grade
26 @property (nonatomic) NSMutableArray *gradeStringPaths; 28 @property (nonatomic) NSMutableArray *gradeStringPaths;
27 29
@@ -29,6 +31,8 @@ @@ -29,6 +31,8 @@
29 31
30 @implementation PNLineChart 32 @implementation PNLineChart
31 33
  34 +@synthesize pathAnimation = _pathAnimation;
  35 +
32 #pragma mark initialization 36 #pragma mark initialization
33 37
34 - (id)initWithCoder:(NSCoder *)coder 38 - (id)initWithCoder:(NSCoder *)coder
@@ -340,18 +344,13 @@ @@ -340,18 +344,13 @@
340 pointLayer.path = pointPath.CGPath; 344 pointLayer.path = pointPath.CGPath;
341 345
342 [CATransaction begin]; 346 [CATransaction begin];
343 - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];  
344 - pathAnimation.duration = 1.0;  
345 - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
346 - pathAnimation.fromValue = @0.0f;  
347 - pathAnimation.toValue = @1.0f;  
348 347
349 - [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 348 + [chartLine addAnimation:self.pathAnimation forKey:@"strokeEndAnimation"];
350 chartLine.strokeEnd = 1.0; 349 chartLine.strokeEnd = 1.0;
351 350
352 // if you want cancel the point animation, conment this code, the point will show immediately 351 // if you want cancel the point animation, conment this code, the point will show immediately
353 if (chartData.inflexionPointStyle != PNLineChartPointStyleNone) { 352 if (chartData.inflexionPointStyle != PNLineChartPointStyleNone) {
354 - [pointLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 353 + [pointLayer addAnimation:self.pathAnimation forKey:@"strokeEndAnimation"];
355 } 354 }
356 355
357 [CATransaction commit]; 356 [CATransaction commit];
@@ -756,6 +755,7 @@ @@ -756,6 +755,7 @@
756 self.chartLineArray = [NSMutableArray new]; 755 self.chartLineArray = [NSMutableArray new];
757 _showLabel = YES; 756 _showLabel = YES;
758 _showGenYLabels = YES; 757 _showGenYLabels = YES;
  758 + _displayAnimated = YES;
759 _pathPoints = [[NSMutableArray alloc] init]; 759 _pathPoints = [[NSMutableArray alloc] init];
760 _endPointsOfPath = [[NSMutableArray alloc] init]; 760 _endPointsOfPath = [[NSMutableArray alloc] init];
761 self.userInteractionEnabled = YES; 761 self.userInteractionEnabled = YES;
@@ -1052,4 +1052,16 @@ @@ -1052,4 +1052,16 @@
1052 return fadeAnimation; 1052 return fadeAnimation;
1053 } 1053 }
1054 1054
  1055 +-(CABasicAnimation *)pathAnimation
  1056 +{
  1057 + if (_displayAnimated && !_pathAnimation) {
  1058 + _pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  1059 + _pathAnimation.duration = 1.0;
  1060 + _pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  1061 + _pathAnimation.fromValue = @0.0f;
  1062 + _pathAnimation.toValue = @1.0f;
  1063 + }
  1064 + return _pathAnimation;
  1065 +}
  1066 +
1055 @end 1067 @end