Showing
1 changed file
with
15 additions
and
13 deletions
| @@ -10,7 +10,7 @@ | @@ -10,7 +10,7 @@ | ||
| 10 | 10 | ||
| 11 | @interface PNPieChart() | 11 | @interface PNPieChart() |
| 12 | 12 | ||
| 13 | -@property (nonatomic, readwrite) NSArray *items; | 13 | +@property (nonatomic) NSArray *items; |
| 14 | @property (nonatomic) NSArray *endPercentages; | 14 | @property (nonatomic) NSArray *endPercentages; |
| 15 | 15 | ||
| 16 | @property (nonatomic) CGFloat outerCircleRadius; | 16 | @property (nonatomic) CGFloat outerCircleRadius; |
| @@ -28,6 +28,7 @@ | @@ -28,6 +28,7 @@ | ||
| 28 | - (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index; | 28 | - (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index; |
| 29 | - (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index; | 29 | - (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index; |
| 30 | - (CGFloat)ratioForItemAtIndex:(NSUInteger)index; | 30 | - (CGFloat)ratioForItemAtIndex:(NSUInteger)index; |
| 31 | + | ||
| 31 | - (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius | 32 | - (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius |
| 32 | borderWidth:(CGFloat)borderWidth | 33 | borderWidth:(CGFloat)borderWidth |
| 33 | fillColor:(UIColor *)fillColor | 34 | fillColor:(UIColor *)fillColor |
| @@ -45,8 +46,8 @@ | @@ -45,8 +46,8 @@ | ||
| 45 | self = [self initWithFrame:frame]; | 46 | self = [self initWithFrame:frame]; |
| 46 | if(self){ | 47 | if(self){ |
| 47 | _items = [NSArray arrayWithArray:items]; | 48 | _items = [NSArray arrayWithArray:items]; |
| 48 | - _outerCircleRadius = CGRectGetWidth(self.bounds)/2; | 49 | + _outerCircleRadius = CGRectGetWidth(self.bounds) / 2; |
| 49 | - _innerCircleRadius = CGRectGetWidth(self.bounds)/6; | 50 | + _innerCircleRadius = CGRectGetWidth(self.bounds) / 6; |
| 50 | 51 | ||
| 51 | _descriptionTextColor = [UIColor whiteColor]; | 52 | _descriptionTextColor = [UIColor whiteColor]; |
| 52 | _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0]; | 53 | _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0]; |
| @@ -66,10 +67,10 @@ | @@ -66,10 +67,10 @@ | ||
| 66 | NSMutableArray *endPercentages = [NSMutableArray new]; | 67 | NSMutableArray *endPercentages = [NSMutableArray new]; |
| 67 | [_items enumerateObjectsUsingBlock:^(PNPieChartDataItem *item, NSUInteger idx, BOOL *stop) { | 68 | [_items enumerateObjectsUsingBlock:^(PNPieChartDataItem *item, NSUInteger idx, BOOL *stop) { |
| 68 | if (total == 0){ | 69 | if (total == 0){ |
| 69 | - [endPercentages addObject:@(1.0/_items.count*(idx+1))]; | 70 | + [endPercentages addObject:@(1.0 / _items.count * (idx + 1))]; |
| 70 | }else{ | 71 | }else{ |
| 71 | currentTotal += item.value; | 72 | currentTotal += item.value; |
| 72 | - [endPercentages addObject:@(currentTotal/total)]; | 73 | + [endPercentages addObject:@(currentTotal / total)]; |
| 73 | } | 74 | } |
| 74 | }]; | 75 | }]; |
| 75 | self.endPercentages = [endPercentages copy]; | 76 | self.endPercentages = [endPercentages copy]; |
| @@ -96,7 +97,7 @@ | @@ -96,7 +97,7 @@ | ||
| 96 | CGFloat startPercnetage = [self startPercentageForItemAtIndex:i]; | 97 | CGFloat startPercnetage = [self startPercentageForItemAtIndex:i]; |
| 97 | CGFloat endPercentage = [self endPercentageForItemAtIndex:i]; | 98 | CGFloat endPercentage = [self endPercentageForItemAtIndex:i]; |
| 98 | 99 | ||
| 99 | - CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2; | 100 | + CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; |
| 100 | CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; | 101 | CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; |
| 101 | CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius | 102 | CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius |
| 102 | borderWidth:borderWidth | 103 | borderWidth:borderWidth |
| @@ -119,7 +120,7 @@ | @@ -119,7 +120,7 @@ | ||
| 119 | - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{ | 120 | - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{ |
| 120 | PNPieChartDataItem *currentDataItem = [self dataItemForIndex:index]; | 121 | PNPieChartDataItem *currentDataItem = [self dataItemForIndex:index]; |
| 121 | CGFloat distance = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; | 122 | CGFloat distance = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; |
| 122 | - CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/2; | 123 | + CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/ 2; |
| 123 | CGFloat rad = centerPercentage * 2 * M_PI; | 124 | CGFloat rad = centerPercentage * 2 * M_PI; |
| 124 | 125 | ||
| 125 | UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)]; | 126 | UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)]; |
| @@ -139,8 +140,7 @@ | @@ -139,8 +140,7 @@ | ||
| 139 | 140 | ||
| 140 | descriptionLabel.font = _descriptionTextFont; | 141 | descriptionLabel.font = _descriptionTextFont; |
| 141 | CGSize labelSize = [descriptionLabel.text sizeWithAttributes:@{NSFontAttributeName:descriptionLabel.font}]; | 142 | CGSize labelSize = [descriptionLabel.text sizeWithAttributes:@{NSFontAttributeName:descriptionLabel.font}]; |
| 142 | - descriptionLabel.frame = CGRectMake( | 143 | + descriptionLabel.frame = CGRectMake(descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y, |
| 143 | - descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y, | ||
| 144 | descriptionLabel.frame.size.width, labelSize.height); | 144 | descriptionLabel.frame.size.width, labelSize.height); |
| 145 | descriptionLabel.numberOfLines = 0; | 145 | descriptionLabel.numberOfLines = 0; |
| 146 | descriptionLabel.textColor = _descriptionTextColor; | 146 | descriptionLabel.textColor = _descriptionTextColor; |
| @@ -198,12 +198,11 @@ | @@ -198,12 +198,11 @@ | ||
| 198 | circle.lineWidth = borderWidth; | 198 | circle.lineWidth = borderWidth; |
| 199 | circle.path = path.CGPath; | 199 | circle.path = path.CGPath; |
| 200 | 200 | ||
| 201 | - | ||
| 202 | return circle; | 201 | return circle; |
| 203 | } | 202 | } |
| 204 | 203 | ||
| 205 | - (void)maskChart{ | 204 | - (void)maskChart{ |
| 206 | - CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2; | 205 | + CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; |
| 207 | CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; | 206 | CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; |
| 208 | CAShapeLayer *maskLayer = [self newCircleLayerWithRadius:radius | 207 | CAShapeLayer *maskLayer = [self newCircleLayerWithRadius:radius |
| 209 | borderWidth:borderWidth | 208 | borderWidth:borderWidth |
| @@ -223,8 +222,11 @@ | @@ -223,8 +222,11 @@ | ||
| 223 | [maskLayer addAnimation:animation forKey:@"circleAnimation"]; | 222 | [maskLayer addAnimation:animation forKey:@"circleAnimation"]; |
| 224 | } | 223 | } |
| 225 | 224 | ||
| 226 | -- (void)createArcAnimationForLayer:(CAShapeLayer *)layer ForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate | 225 | +- (void)createArcAnimationForLayer:(CAShapeLayer *)layer |
| 227 | -{ | 226 | + forKey:(NSString *)key |
| 227 | + fromValue:(NSNumber *)from | ||
| 228 | + toValue:(NSNumber *)to | ||
| 229 | + delegate:(id)delegate{ | ||
| 228 | CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key]; | 230 | CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key]; |
| 229 | arcAnimation.fromValue = @0; | 231 | arcAnimation.fromValue = @0; |
| 230 | arcAnimation.toValue = to; | 232 | arcAnimation.toValue = to; |
-
Please register or login to post a comment