Showing
5 changed files
with
74 additions
and
31 deletions
| @@ -18,5 +18,6 @@ | @@ -18,5 +18,6 @@ | ||
| 18 | @property (nonatomic) UIColor *barColor; | 18 | @property (nonatomic) UIColor *barColor; |
| 19 | @property (nonatomic) UIColor *barColorGradientStart; | 19 | @property (nonatomic) UIColor *barColorGradientStart; |
| 20 | @property (nonatomic) CGFloat barRadius; | 20 | @property (nonatomic) CGFloat barRadius; |
| 21 | +@property (nonatomic) CAShapeLayer *gradientMask; | ||
| 21 | 22 | ||
| 22 | @end | 23 | @end |
| @@ -38,7 +38,8 @@ | @@ -38,7 +38,8 @@ | ||
| 38 | 38 | ||
| 39 | - (void)setGrade:(float)grade | 39 | - (void)setGrade:(float)grade |
| 40 | { | 40 | { |
| 41 | - _grade = grade; | 41 | + NSLog(@"New garde %f",grade); |
| 42 | + | ||
| 42 | UIBezierPath *progressline = [UIBezierPath bezierPath]; | 43 | UIBezierPath *progressline = [UIBezierPath bezierPath]; |
| 43 | 44 | ||
| 44 | [progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)]; | 45 | [progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)]; |
| @@ -46,7 +47,7 @@ | @@ -46,7 +47,7 @@ | ||
| 46 | 47 | ||
| 47 | [progressline setLineWidth:1.0]; | 48 | [progressline setLineWidth:1.0]; |
| 48 | [progressline setLineCapStyle:kCGLineCapSquare]; | 49 | [progressline setLineCapStyle:kCGLineCapSquare]; |
| 49 | - _chartLine.path = progressline.CGPath; | 50 | + |
| 50 | 51 | ||
| 51 | if (_barColor) { | 52 | if (_barColor) { |
| 52 | _chartLine.strokeColor = [_barColor CGColor]; | 53 | _chartLine.strokeColor = [_barColor CGColor]; |
| @@ -55,6 +56,26 @@ | @@ -55,6 +56,26 @@ | ||
| 55 | _chartLine.strokeColor = [PNGreen CGColor]; | 56 | _chartLine.strokeColor = [PNGreen CGColor]; |
| 56 | } | 57 | } |
| 57 | 58 | ||
| 59 | + if (_grade) { | ||
| 60 | + | ||
| 61 | + CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; | ||
| 62 | + pathAnimation.fromValue = (id)_chartLine.path; | ||
| 63 | + pathAnimation.toValue = (id)[progressline CGPath]; | ||
| 64 | + pathAnimation.duration = 0.5f; | ||
| 65 | + pathAnimation.autoreverses = NO; | ||
| 66 | + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 67 | + [_chartLine addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 68 | + | ||
| 69 | + _chartLine.path = progressline.CGPath; | ||
| 70 | + | ||
| 71 | + if (_barColorGradientStart) { | ||
| 72 | + | ||
| 73 | + // Add gradient | ||
| 74 | + [self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 75 | + self.gradientMask.path = progressline.CGPath; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + }else{ | ||
| 58 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | 79 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; |
| 59 | pathAnimation.duration = 1.0; | 80 | pathAnimation.duration = 1.0; |
| 60 | pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | 81 | pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; |
| @@ -64,16 +85,17 @@ | @@ -64,16 +85,17 @@ | ||
| 64 | 85 | ||
| 65 | _chartLine.strokeEnd = 1.0; | 86 | _chartLine.strokeEnd = 1.0; |
| 66 | 87 | ||
| 88 | + _chartLine.path = progressline.CGPath; | ||
| 67 | // Check if user wants to add a gradient from the start color to the bar color | 89 | // Check if user wants to add a gradient from the start color to the bar color |
| 68 | if (_barColorGradientStart) { | 90 | if (_barColorGradientStart) { |
| 69 | 91 | ||
| 70 | // Add gradient | 92 | // Add gradient |
| 71 | - CAShapeLayer *gradientMask = [CAShapeLayer layer]; | 93 | + self.gradientMask = [CAShapeLayer layer]; |
| 72 | - gradientMask.fillColor = [[UIColor clearColor] CGColor]; | 94 | + self.gradientMask.fillColor = [[UIColor clearColor] CGColor]; |
| 73 | - gradientMask.strokeColor = [[UIColor blackColor] CGColor]; | 95 | + self.gradientMask.strokeColor = [[UIColor blackColor] CGColor]; |
| 74 | - gradientMask.lineWidth = self.frame.size.width; | 96 | + self.gradientMask.lineWidth = self.frame.size.width; |
| 75 | - gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); | 97 | + self.gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); |
| 76 | - gradientMask.path = progressline.CGPath; | 98 | + self.gradientMask.path = progressline.CGPath; |
| 77 | 99 | ||
| 78 | 100 | ||
| 79 | CAGradientLayer *gradientLayer = [CAGradientLayer layer]; | 101 | CAGradientLayer *gradientLayer = [CAGradientLayer layer]; |
| @@ -87,14 +109,17 @@ | @@ -87,14 +109,17 @@ | ||
| 87 | ]; | 109 | ]; |
| 88 | gradientLayer.colors = colors; | 110 | gradientLayer.colors = colors; |
| 89 | 111 | ||
| 90 | - [gradientLayer setMask:gradientMask]; | 112 | + [gradientLayer setMask:self.gradientMask]; |
| 91 | 113 | ||
| 92 | [_chartLine addSublayer:gradientLayer]; | 114 | [_chartLine addSublayer:gradientLayer]; |
| 93 | 115 | ||
| 94 | - gradientMask.strokeEnd = 1.0; | 116 | + self.gradientMask.strokeEnd = 1.0; |
| 95 | - [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | 117 | + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; |
| 118 | + } | ||
| 96 | } | 119 | } |
| 97 | 120 | ||
| 121 | + _grade = grade; | ||
| 122 | + | ||
| 98 | } | 123 | } |
| 99 | 124 | ||
| 100 | 125 |
| @@ -36,6 +36,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | @@ -36,6 +36,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | ||
| 36 | @property (nonatomic) NSArray *strokeColors; | 36 | @property (nonatomic) NSArray *strokeColors; |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | +/** Update Values. */ | ||
| 40 | +- (void)updateChartData:(NSArray *)data; | ||
| 41 | + | ||
| 39 | /** Changes chart margin. */ | 42 | /** Changes chart margin. */ |
| 40 | @property (nonatomic) CGFloat yChartLabelWidth; | 43 | @property (nonatomic) CGFloat yChartLabelWidth; |
| 41 | 44 |
| @@ -91,6 +91,11 @@ | @@ -91,6 +91,11 @@ | ||
| 91 | } | 91 | } |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | +-(void)updateChartData:(NSArray *)data{ | ||
| 95 | + self.yValues = data; | ||
| 96 | + [self updateBar]; | ||
| 97 | +} | ||
| 98 | + | ||
| 94 | - (void)getYValueMax:(NSArray *)yLabels | 99 | - (void)getYValueMax:(NSArray *)yLabels |
| 95 | { | 100 | { |
| 96 | int max = [[yLabels valueForKeyPath:@"@max.intValue"] intValue]; | 101 | int max = [[yLabels valueForKeyPath:@"@max.intValue"] intValue]; |
| @@ -151,28 +156,20 @@ | @@ -151,28 +156,20 @@ | ||
| 151 | _strokeColor = strokeColor; | 156 | _strokeColor = strokeColor; |
| 152 | } | 157 | } |
| 153 | 158 | ||
| 154 | - | 159 | +- (void)updateBar |
| 155 | -- (void)strokeChart | ||
| 156 | { | 160 | { |
| 157 | - //Add Labels | ||
| 158 | - | ||
| 159 | - [self viewCleanupForCollection:_bars]; | ||
| 160 | - | ||
| 161 | 161 | ||
| 162 | //Add bars | 162 | //Add bars |
| 163 | CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - xLabelHeight; | 163 | CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - xLabelHeight; |
| 164 | NSInteger index = 0; | 164 | NSInteger index = 0; |
| 165 | 165 | ||
| 166 | for (NSNumber *valueString in _yValues) { | 166 | for (NSNumber *valueString in _yValues) { |
| 167 | - float value = [valueString floatValue]; | ||
| 168 | - | ||
| 169 | - float grade = (float)value / (float)_yValueMax; | ||
| 170 | - | ||
| 171 | - if (isnan(grade)) { | ||
| 172 | - grade = 0; | ||
| 173 | - } | ||
| 174 | 167 | ||
| 175 | PNBar *bar; | 168 | PNBar *bar; |
| 169 | + | ||
| 170 | + if (_bars.count == _yValues.count) { | ||
| 171 | + bar = [_bars objectAtIndex:index]; | ||
| 172 | + }else{ | ||
| 176 | CGFloat barWidth; | 173 | CGFloat barWidth; |
| 177 | CGFloat barXPosition; | 174 | CGFloat barXPosition; |
| 178 | 175 | ||
| @@ -208,23 +205,40 @@ | @@ -208,23 +205,40 @@ | ||
| 208 | }else{ | 205 | }else{ |
| 209 | bar.barColor = [self barColorAtIndex:index]; | 206 | bar.barColor = [self barColorAtIndex:index]; |
| 210 | } | 207 | } |
| 211 | - | ||
| 212 | - //Height Of Bar | ||
| 213 | - bar.grade = grade; | ||
| 214 | - | ||
| 215 | // Add gradient | 208 | // Add gradient |
| 216 | bar.barColorGradientStart = _barColorGradientStart; | 209 | bar.barColorGradientStart = _barColorGradientStart; |
| 217 | 210 | ||
| 218 | - | ||
| 219 | //For Click Index | 211 | //For Click Index |
| 220 | bar.tag = index; | 212 | bar.tag = index; |
| 221 | 213 | ||
| 222 | - | ||
| 223 | [_bars addObject:bar]; | 214 | [_bars addObject:bar]; |
| 224 | [self addSubview:bar]; | 215 | [self addSubview:bar]; |
| 216 | + } | ||
| 217 | + | ||
| 218 | + //Height Of Bar | ||
| 219 | + float value = [valueString floatValue]; | ||
| 220 | + | ||
| 221 | + float grade = (float)value / (float)_yValueMax; | ||
| 222 | + | ||
| 223 | + if (isnan(grade)) { | ||
| 224 | + grade = 0; | ||
| 225 | + } | ||
| 226 | + bar.grade = grade; | ||
| 225 | 227 | ||
| 226 | index += 1; | 228 | index += 1; |
| 227 | } | 229 | } |
| 230 | +} | ||
| 231 | + | ||
| 232 | +- (void)strokeChart | ||
| 233 | +{ | ||
| 234 | + //Add Labels | ||
| 235 | + | ||
| 236 | + [self viewCleanupForCollection:_bars]; | ||
| 237 | + | ||
| 238 | + | ||
| 239 | + //Update Bar | ||
| 240 | + | ||
| 241 | + [self updateBar]; | ||
| 228 | 242 | ||
| 229 | //Add chart border lines | 243 | //Add chart border lines |
| 230 | 244 |
| @@ -136,7 +136,7 @@ | @@ -136,7 +136,7 @@ | ||
| 136 | else if ([self.title isEqualToString:@"Bar Chart"]) | 136 | else if ([self.title isEqualToString:@"Bar Chart"]) |
| 137 | { | 137 | { |
| 138 | [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]]; | 138 | [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]]; |
| 139 | - [self.barChart setYValues:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]]; | 139 | + [self.barChart updateChartData:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]]; |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | } | 142 | } |
-
Please register or login to post a comment