Merge pull request #138 from dredful/master
Added additional initWithFrame methods to allow for chart options
Showing
2 changed files
with
96 additions
and
19 deletions
| @@ -29,6 +29,30 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { | @@ -29,6 +29,30 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { | ||
| 29 | clockwise:(BOOL)clockwise | 29 | clockwise:(BOOL)clockwise |
| 30 | shadow:(BOOL)hasBackgroundShadow; | 30 | shadow:(BOOL)hasBackgroundShadow; |
| 31 | 31 | ||
| 32 | +- (id)initWithFrame:(CGRect)frame | ||
| 33 | + total:(NSNumber *)total | ||
| 34 | + current:(NSNumber *)current | ||
| 35 | + clockwise:(BOOL)clockwise | ||
| 36 | + shadow:(BOOL)hasBackgroundShadow | ||
| 37 | + shadowColor:(UIColor *)backgroundShadowColor; | ||
| 38 | + | ||
| 39 | +- (id)initWithFrame:(CGRect)frame | ||
| 40 | + total:(NSNumber *)total | ||
| 41 | + current:(NSNumber *)current | ||
| 42 | + clockwise:(BOOL)clockwise | ||
| 43 | + shadow:(BOOL)hasBackgroundShadow | ||
| 44 | + shadowColor:(UIColor *)backgroundShadowColor | ||
| 45 | +displayCountingLabel:(BOOL)displayCountingLabel; | ||
| 46 | + | ||
| 47 | +- (id)initWithFrame:(CGRect)frame | ||
| 48 | + total:(NSNumber *)total | ||
| 49 | + current:(NSNumber *)current | ||
| 50 | + clockwise:(BOOL)clockwise | ||
| 51 | + shadow:(BOOL)hasBackgroundShadow | ||
| 52 | + shadowColor:(UIColor *)backgroundShadowColor | ||
| 53 | +displayCountingLabel:(BOOL)displayCountingLabel | ||
| 54 | + overrideLineWidth:(NSNumber *)overrideLineWidth; | ||
| 55 | + | ||
| 32 | @property (strong, nonatomic) UICountingLabel *countingLabel; | 56 | @property (strong, nonatomic) UICountingLabel *countingLabel; |
| 33 | @property (nonatomic) UIColor *strokeColor; | 57 | @property (nonatomic) UIColor *strokeColor; |
| 34 | @property (nonatomic) UIColor *strokeColorGradientStart; | 58 | @property (nonatomic) UIColor *strokeColorGradientStart; |
| @@ -43,4 +67,6 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { | @@ -43,4 +67,6 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { | ||
| 43 | @property (nonatomic) CAShapeLayer *gradientMask; | 67 | @property (nonatomic) CAShapeLayer *gradientMask; |
| 44 | @property (nonatomic) CAShapeLayer *circleBackground; | 68 | @property (nonatomic) CAShapeLayer *circleBackground; |
| 45 | 69 | ||
| 70 | +@property (nonatomic) BOOL displayCountingLabel; | ||
| 71 | + | ||
| 46 | @end | 72 | @end |
| @@ -13,12 +13,53 @@ | @@ -13,12 +13,53 @@ | ||
| 13 | 13 | ||
| 14 | @implementation PNCircleChart | 14 | @implementation PNCircleChart |
| 15 | 15 | ||
| 16 | +- (id)initWithFrame:(CGRect)frame total:(NSNumber *)total current:(NSNumber *)current clockwise:(BOOL)clockwise shadow:(BOOL)hasBackgroundShadow { | ||
| 17 | + | ||
| 18 | + return [self initWithFrame:frame | ||
| 19 | + total:total | ||
| 20 | + current:current | ||
| 21 | + clockwise:clockwise | ||
| 22 | + shadow:shadow | ||
| 23 | + shadowColor:PNGreen | ||
| 24 | + displayCountingLabel:YES | ||
| 25 | + overrideLineWidth:@8.0f]; | ||
| 26 | + | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +- (id)initWithFrame:(CGRect)frame total:(NSNumber *)total current:(NSNumber *)current clockwise:(BOOL)clockwise shadow:(BOOL)hasBackgroundShadow shadowColor:(UIColor *)backgroundShadowColor { | ||
| 30 | + | ||
| 31 | + return [self initWithFrame:frame | ||
| 32 | + total:total | ||
| 33 | + current:current | ||
| 34 | + clockwise:clockwise | ||
| 35 | + shadow:shadow | ||
| 36 | + shadowColor:backgroundShadowColor | ||
| 37 | + displayCountingLabel:YES | ||
| 38 | + overrideLineWidth:@8.0f]; | ||
| 39 | + | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +- (id)initWithFrame:(CGRect)frame total:(NSNumber *)total current:(NSNumber *)current clockwise:(BOOL)clockwise shadow:(BOOL)hasBackgroundShadow shadowColor:(UIColor *)backgroundShadowColor displayCountingLabel:(BOOL)displayCountingLabel { | ||
| 43 | + | ||
| 44 | + return [self initWithFrame:frame | ||
| 45 | + total:total | ||
| 46 | + current:current | ||
| 47 | + clockwise:clockwise | ||
| 48 | + shadow:shadow | ||
| 49 | + shadowColor:PNGreen | ||
| 50 | + displayCountingLabel:displayCountingLabel | ||
| 51 | + overrideLineWidth:@8.0f]; | ||
| 52 | + | ||
| 53 | +} | ||
| 16 | 54 | ||
| 17 | - (id)initWithFrame:(CGRect)frame | 55 | - (id)initWithFrame:(CGRect)frame |
| 18 | total:(NSNumber *)total | 56 | total:(NSNumber *)total |
| 19 | current:(NSNumber *)current | 57 | current:(NSNumber *)current |
| 20 | clockwise:(BOOL)clockwise | 58 | clockwise:(BOOL)clockwise |
| 21 | shadow:(BOOL)hasBackgroundShadow | 59 | shadow:(BOOL)hasBackgroundShadow |
| 60 | + shadowColor:(UIColor *)backgroundShadowColor | ||
| 61 | +displayCountingLabel:(BOOL)displayCountingLabel | ||
| 62 | + overrideLineWidth:(NSNumber *)overrideLineWidth | ||
| 22 | { | 63 | { |
| 23 | self = [super initWithFrame:frame]; | 64 | self = [super initWithFrame:frame]; |
| 24 | 65 | ||
| @@ -28,13 +69,16 @@ | @@ -28,13 +69,16 @@ | ||
| 28 | _strokeColor = PNFreshGreen; | 69 | _strokeColor = PNFreshGreen; |
| 29 | _duration = 1.0; | 70 | _duration = 1.0; |
| 30 | _chartType = PNChartFormatTypePercent; | 71 | _chartType = PNChartFormatTypePercent; |
| 72 | + | ||
| 73 | + _displayCountingLabel = displayCountingLabel; | ||
| 31 | 74 | ||
| 32 | CGFloat startAngle = clockwise ? -90.0f : 270.0f; | 75 | CGFloat startAngle = clockwise ? -90.0f : 270.0f; |
| 33 | CGFloat endAngle = clockwise ? -90.01f : 270.01f; | 76 | CGFloat endAngle = clockwise ? -90.01f : 270.01f; |
| 34 | 77 | ||
| 35 | - _lineWidth = @8.0f; | 78 | + _lineWidth = overrideLineWidth; |
| 79 | + | ||
| 36 | UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f) | 80 | UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f) |
| 37 | - radius:(self.frame.size.height * 0.5) - [_lineWidth floatValue] | 81 | + radius:(self.frame.size.height * 0.5) - ([_lineWidth floatValue]/2.0f) |
| 38 | startAngle:DEGREES_TO_RADIANS(startAngle) | 82 | startAngle:DEGREES_TO_RADIANS(startAngle) |
| 39 | endAngle:DEGREES_TO_RADIANS(endAngle) | 83 | endAngle:DEGREES_TO_RADIANS(endAngle) |
| 40 | clockwise:clockwise]; | 84 | clockwise:clockwise]; |
| @@ -51,7 +95,7 @@ | @@ -51,7 +95,7 @@ | ||
| 51 | _circleBackground.lineCap = kCALineCapRound; | 95 | _circleBackground.lineCap = kCALineCapRound; |
| 52 | _circleBackground.fillColor = [UIColor clearColor].CGColor; | 96 | _circleBackground.fillColor = [UIColor clearColor].CGColor; |
| 53 | _circleBackground.lineWidth = [_lineWidth floatValue]; | 97 | _circleBackground.lineWidth = [_lineWidth floatValue]; |
| 54 | - _circleBackground.strokeColor = (hasBackgroundShadow ? PNLightYellow.CGColor : [UIColor clearColor].CGColor); | 98 | + _circleBackground.strokeColor = (hasBackgroundShadow ? backgroundShadowColor.CGColor : [UIColor clearColor].CGColor); |
| 55 | _circleBackground.strokeEnd = 1.0; | 99 | _circleBackground.strokeEnd = 1.0; |
| 56 | _circleBackground.zPosition = -1; | 100 | _circleBackground.zPosition = -1; |
| 57 | 101 | ||
| @@ -65,7 +109,9 @@ | @@ -65,7 +109,9 @@ | ||
| 65 | [_countingLabel setBackgroundColor:[UIColor clearColor]]; | 109 | [_countingLabel setBackgroundColor:[UIColor clearColor]]; |
| 66 | [_countingLabel setCenter:CGPointMake(self.center.x, self.center.y)]; | 110 | [_countingLabel setCenter:CGPointMake(self.center.x, self.center.y)]; |
| 67 | _countingLabel.method = UILabelCountingMethodEaseInOut; | 111 | _countingLabel.method = UILabelCountingMethodEaseInOut; |
| 68 | - [self addSubview:_countingLabel];; | 112 | + if (_displayCountingLabel) { |
| 113 | + [self addSubview:_countingLabel]; | ||
| 114 | + } | ||
| 69 | } | 115 | } |
| 70 | 116 | ||
| 71 | return self; | 117 | return self; |
| @@ -76,21 +122,23 @@ | @@ -76,21 +122,23 @@ | ||
| 76 | { | 122 | { |
| 77 | // Add counting label | 123 | // Add counting label |
| 78 | 124 | ||
| 79 | - NSString *format; | 125 | + if (_displayCountingLabel) { |
| 80 | - switch (self.chartType) { | 126 | + NSString *format; |
| 81 | - case PNChartFormatTypePercent: | 127 | + switch (self.chartType) { |
| 82 | - format = @"%d%%"; | 128 | + case PNChartFormatTypePercent: |
| 83 | - break; | 129 | + format = @"%d%%"; |
| 84 | - case PNChartFormatTypeDollar: | 130 | + break; |
| 85 | - format = @"$%d"; | 131 | + case PNChartFormatTypeDollar: |
| 86 | - break; | 132 | + format = @"$%d"; |
| 87 | - case PNChartFormatTypeNone: | 133 | + break; |
| 88 | - default: | 134 | + case PNChartFormatTypeNone: |
| 89 | - format = @"%d"; | 135 | + default: |
| 90 | - break; | 136 | + format = @"%d"; |
| 137 | + break; | ||
| 138 | + } | ||
| 139 | + self.countingLabel.format = format; | ||
| 140 | + [self addSubview:self.countingLabel]; | ||
| 91 | } | 141 | } |
| 92 | - self.countingLabel.format = format; | ||
| 93 | - [self addSubview:self.countingLabel]; | ||
| 94 | 142 | ||
| 95 | 143 | ||
| 96 | // Add circle params | 144 | // Add circle params |
| @@ -172,7 +220,10 @@ | @@ -172,7 +220,10 @@ | ||
| 172 | } | 220 | } |
| 173 | [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | 221 | [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; |
| 174 | 222 | ||
| 175 | - [self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([current floatValue], [_total floatValue]) withDuration:self.duration]; | 223 | + if (_displayCountingLabel) { |
| 224 | + [self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([current floatValue], [_total floatValue]) withDuration:self.duration]; | ||
| 225 | + } | ||
| 226 | + | ||
| 176 | _current = current; | 227 | _current = current; |
| 177 | } | 228 | } |
| 178 | 229 |
-
Please register or login to post a comment