Allow setting Top/Bottom/Left/Right margins independently in PNBarChart
Showing
3 changed files
with
82 additions
and
63 deletions
| @@ -46,7 +46,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | @@ -46,7 +46,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | ||
| 46 | /** Formats the ylabel text. */ | 46 | /** Formats the ylabel text. */ |
| 47 | @property (copy) PNYLabelFormatter yLabelFormatter; | 47 | @property (copy) PNYLabelFormatter yLabelFormatter; |
| 48 | 48 | ||
| 49 | -@property (nonatomic) CGFloat chartMargin; | 49 | +@property (nonatomic) CGFloat chartMarginLeft; |
| 50 | +@property (nonatomic) CGFloat chartMarginRight; | ||
| 51 | +@property (nonatomic) CGFloat chartMarginTop; | ||
| 52 | +@property (nonatomic) CGFloat chartMarginBottom; | ||
| 50 | 53 | ||
| 51 | /** Controls whether labels should be displayed. */ | 54 | /** Controls whether labels should be displayed. */ |
| 52 | @property (nonatomic) BOOL showLabel; | 55 | @property (nonatomic) BOOL showLabel; |
| @@ -24,7 +24,7 @@ | @@ -24,7 +24,7 @@ | ||
| 24 | - (id)initWithCoder:(NSCoder *)aDecoder | 24 | - (id)initWithCoder:(NSCoder *)aDecoder |
| 25 | { | 25 | { |
| 26 | self = [super initWithCoder:aDecoder]; | 26 | self = [super initWithCoder:aDecoder]; |
| 27 | - | 27 | + |
| 28 | if (self) { | 28 | if (self) { |
| 29 | [self setupDefaultValues]; | 29 | [self setupDefaultValues]; |
| 30 | } | 30 | } |
| @@ -56,7 +56,10 @@ | @@ -56,7 +56,10 @@ | ||
| 56 | _xLabelSkip = 1; | 56 | _xLabelSkip = 1; |
| 57 | _yLabelSum = 4; | 57 | _yLabelSum = 4; |
| 58 | _labelMarginTop = 0; | 58 | _labelMarginTop = 0; |
| 59 | - _chartMargin = 25.0; | 59 | + _chartMarginLeft = 25.0; |
| 60 | + _chartMarginRight = 25.0; | ||
| 61 | + _chartMarginTop = 25.0; | ||
| 62 | + _chartMarginBottom = 25.0; | ||
| 60 | _barRadius = 2.0; | 63 | _barRadius = 2.0; |
| 61 | _showChartBorder = NO; | 64 | _showChartBorder = NO; |
| 62 | _showLevelLine = NO; | 65 | _showLevelLine = NO; |
| @@ -73,7 +76,7 @@ | @@ -73,7 +76,7 @@ | ||
| 73 | { | 76 | { |
| 74 | _yValues = yValues; | 77 | _yValues = yValues; |
| 75 | //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis | 78 | //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis |
| 76 | - | 79 | + |
| 77 | if (_showLabel) { | 80 | if (_showLabel) { |
| 78 | [self __addYCoordinateLabelsValues]; | 81 | [self __addYCoordinateLabelsValues]; |
| 79 | } else { | 82 | } else { |
| @@ -89,7 +92,7 @@ | @@ -89,7 +92,7 @@ | ||
| 89 | } else { | 92 | } else { |
| 90 | [self getYValueMax:yAxisValues]; | 93 | [self getYValueMax:yAxisValues]; |
| 91 | } | 94 | } |
| 92 | - | 95 | + |
| 93 | if (_yLabelSum==4) { | 96 | if (_yLabelSum==4) { |
| 94 | _yLabelSum = yAxisValues.count; | 97 | _yLabelSum = yAxisValues.count; |
| 95 | (_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++; | 98 | (_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++; |
| @@ -99,12 +102,12 @@ | @@ -99,12 +102,12 @@ | ||
| 99 | #pragma mark - Private Method | 102 | #pragma mark - Private Method |
| 100 | #pragma mark - 添加柱状图的Y轴坐标 | 103 | #pragma mark - 添加柱状图的Y轴坐标 |
| 101 | - (void)__addYCoordinateLabelsValues{ | 104 | - (void)__addYCoordinateLabelsValues{ |
| 102 | - | 105 | + |
| 103 | [self viewCleanupForCollection:_yChartLabels]; | 106 | [self viewCleanupForCollection:_yChartLabels]; |
| 104 | - | 107 | + |
| 105 | [self processYMaxValue]; | 108 | [self processYMaxValue]; |
| 106 | - | 109 | + |
| 107 | - float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; | 110 | + float sectionHeight = (self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight) / _yLabelSum; |
| 108 | for (int i = 0; i <= _yLabelSum; i++) { | 111 | for (int i = 0; i <= _yLabelSum; i++) { |
| 109 | NSString *labelText; | 112 | NSString *labelText; |
| 110 | if (_yLabels) { | 113 | if (_yLabels) { |
| @@ -113,15 +116,15 @@ | @@ -113,15 +116,15 @@ | ||
| 113 | } else { | 116 | } else { |
| 114 | labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum )); | 117 | labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum )); |
| 115 | } | 118 | } |
| 116 | - | 119 | + |
| 117 | - CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight}; | 120 | + CGRect frame = (CGRect){0, sectionHeight * i + _chartMarginTop - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight}; |
| 118 | PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame]; | 121 | PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame]; |
| 119 | label.font = _labelFont; | 122 | label.font = _labelFont; |
| 120 | label.textColor = _labelTextColor; | 123 | label.textColor = _labelTextColor; |
| 121 | [label setTextAlignment:NSTextAlignmentRight]; | 124 | [label setTextAlignment:NSTextAlignmentRight]; |
| 122 | label.text = labelText; | 125 | label.text = labelText; |
| 123 | [self addSubview:label]; | 126 | [self addSubview:label]; |
| 124 | - | 127 | + |
| 125 | [_yChartLabels addObject:label]; | 128 | [_yChartLabels addObject:label]; |
| 126 | } | 129 | } |
| 127 | } | 130 | } |
| @@ -146,20 +149,20 @@ | @@ -146,20 +149,20 @@ | ||
| 146 | - (void)setXLabels:(NSArray *)xLabels | 149 | - (void)setXLabels:(NSArray *)xLabels |
| 147 | { | 150 | { |
| 148 | _xLabels = xLabels; | 151 | _xLabels = xLabels; |
| 149 | - | 152 | + |
| 150 | if (_xChartLabels) { | 153 | if (_xChartLabels) { |
| 151 | [self viewCleanupForCollection:_xChartLabels]; | 154 | [self viewCleanupForCollection:_xChartLabels]; |
| 152 | }else{ | 155 | }else{ |
| 153 | _xChartLabels = [NSMutableArray new]; | 156 | _xChartLabels = [NSMutableArray new]; |
| 154 | } | 157 | } |
| 155 | - | 158 | + |
| 156 | - _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count]; | 159 | + _xLabelWidth = (self.frame.size.width - _chartMarginLeft - _chartMarginRight) / [xLabels count]; |
| 157 | - | 160 | + |
| 158 | - if (_showLabel) { | 161 | + if (_showLabel) { |
| 159 | int labelAddCount = 0; | 162 | int labelAddCount = 0; |
| 160 | for (int index = 0; index < _xLabels.count; index++) { | 163 | for (int index = 0; index < _xLabels.count; index++) { |
| 161 | labelAddCount += 1; | 164 | labelAddCount += 1; |
| 162 | - | 165 | + |
| 163 | if (labelAddCount == _xLabelSkip) { | 166 | if (labelAddCount == _xLabelSkip) { |
| 164 | NSString *labelText = [_xLabels[index] description]; | 167 | NSString *labelText = [_xLabels[index] description]; |
| 165 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0, 0, _xLabelWidth, kXLabelHeight)]; | 168 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0, 0, _xLabelWidth, kXLabelHeight)]; |
| @@ -171,15 +174,15 @@ | @@ -171,15 +174,15 @@ | ||
| 171 | CGFloat labelXPosition; | 174 | CGFloat labelXPosition; |
| 172 | if (_rotateForXAxisText){ | 175 | if (_rotateForXAxisText){ |
| 173 | label.transform = CGAffineTransformMakeRotation(M_PI / 4); | 176 | label.transform = CGAffineTransformMakeRotation(M_PI / 4); |
| 174 | - labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /1.5); | 177 | + labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /1.5); |
| 175 | } | 178 | } |
| 176 | else{ | 179 | else{ |
| 177 | - labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 ); | 180 | + labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 ); |
| 178 | } | 181 | } |
| 179 | label.center = CGPointMake(labelXPosition, | 182 | label.center = CGPointMake(labelXPosition, |
| 180 | - self.frame.size.height - kXLabelHeight - _chartMargin + label.frame.size.height /2.0 + _labelMarginTop); | 183 | + self.frame.size.height - kXLabelHeight - _chartMarginTop + label.frame.size.height /2.0 + _labelMarginTop); |
| 181 | labelAddCount = 0; | 184 | labelAddCount = 0; |
| 182 | - | 185 | + |
| 183 | [_xChartLabels addObject:label]; | 186 | [_xChartLabels addObject:label]; |
| 184 | [self addSubview:label]; | 187 | [self addSubview:label]; |
| 185 | } | 188 | } |
| @@ -195,44 +198,44 @@ | @@ -195,44 +198,44 @@ | ||
| 195 | 198 | ||
| 196 | - (void)updateBar | 199 | - (void)updateBar |
| 197 | { | 200 | { |
| 198 | - | 201 | + |
| 199 | //Add bars | 202 | //Add bars |
| 200 | - CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - kXLabelHeight; | 203 | + CGFloat chartCavanHeight = self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight; |
| 201 | NSInteger index = 0; | 204 | NSInteger index = 0; |
| 202 | - | 205 | + |
| 203 | for (NSNumber *valueString in _yValues) { | 206 | for (NSNumber *valueString in _yValues) { |
| 204 | - | 207 | + |
| 205 | PNBar *bar; | 208 | PNBar *bar; |
| 206 | - | 209 | + |
| 207 | if (_bars.count == _yValues.count) { | 210 | if (_bars.count == _yValues.count) { |
| 208 | bar = [_bars objectAtIndex:index]; | 211 | bar = [_bars objectAtIndex:index]; |
| 209 | }else{ | 212 | }else{ |
| 210 | CGFloat barWidth; | 213 | CGFloat barWidth; |
| 211 | CGFloat barXPosition; | 214 | CGFloat barXPosition; |
| 212 | - | 215 | + |
| 213 | if (_barWidth) { | 216 | if (_barWidth) { |
| 214 | barWidth = _barWidth; | 217 | barWidth = _barWidth; |
| 215 | - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 - _barWidth /2.0; | 218 | + barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 - _barWidth /2.0; |
| 216 | }else{ | 219 | }else{ |
| 217 | - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth * 0.25; | 220 | + barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth * 0.25; |
| 218 | if (_showLabel) { | 221 | if (_showLabel) { |
| 219 | barWidth = _xLabelWidth * 0.5; | 222 | barWidth = _xLabelWidth * 0.5; |
| 220 | - | 223 | + |
| 221 | } | 224 | } |
| 222 | else { | 225 | else { |
| 223 | barWidth = _xLabelWidth * 0.6; | 226 | barWidth = _xLabelWidth * 0.6; |
| 224 | - | 227 | + |
| 225 | } | 228 | } |
| 226 | } | 229 | } |
| 227 | - | 230 | + |
| 228 | bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position | 231 | bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position |
| 229 | - self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMargin , //Bar Y position | 232 | + self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMarginTop , //Bar Y position |
| 230 | barWidth, // Bar witdh | 233 | barWidth, // Bar witdh |
| 231 | self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height | 234 | self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height |
| 232 | - | 235 | + |
| 233 | //Change Bar Radius | 236 | //Change Bar Radius |
| 234 | bar.barRadius = _barRadius; | 237 | bar.barRadius = _barRadius; |
| 235 | - | 238 | + |
| 236 | //Change Bar Background color | 239 | //Change Bar Background color |
| 237 | bar.backgroundColor = _barBackgroundColor; | 240 | bar.backgroundColor = _barBackgroundColor; |
| 238 | //Bar StrokColor First | 241 | //Bar StrokColor First |
| @@ -241,23 +244,23 @@ | @@ -241,23 +244,23 @@ | ||
| 241 | }else{ | 244 | }else{ |
| 242 | bar.barColor = [self barColorAtIndex:index]; | 245 | bar.barColor = [self barColorAtIndex:index]; |
| 243 | } | 246 | } |
| 244 | - | 247 | + |
| 245 | // Add gradient | 248 | // Add gradient |
| 246 | if (self.isGradientShow) { | 249 | if (self.isGradientShow) { |
| 247 | bar.barColorGradientStart = bar.barColor; | 250 | bar.barColorGradientStart = bar.barColor; |
| 248 | } | 251 | } |
| 249 | - | 252 | + |
| 250 | //For Click Index | 253 | //For Click Index |
| 251 | bar.tag = index; | 254 | bar.tag = index; |
| 252 | - | 255 | + |
| 253 | [_bars addObject:bar]; | 256 | [_bars addObject:bar]; |
| 254 | [self addSubview:bar]; | 257 | [self addSubview:bar]; |
| 255 | } | 258 | } |
| 256 | - | 259 | + |
| 257 | //Height Of Bar | 260 | //Height Of Bar |
| 258 | float value = [valueString floatValue]; | 261 | float value = [valueString floatValue]; |
| 259 | float grade =fabsf((float)value / (float)_yValueMax); | 262 | float grade =fabsf((float)value / (float)_yValueMax); |
| 260 | - | 263 | + |
| 261 | if (isnan(grade)) { | 264 | if (isnan(grade)) { |
| 262 | grade = 0; | 265 | grade = 0; |
| 263 | } | 266 | } |
| @@ -266,14 +269,14 @@ | @@ -266,14 +269,14 @@ | ||
| 266 | bar.isShowNumber = self.isShowNumbers; | 269 | bar.isShowNumber = self.isShowNumbers; |
| 267 | CGRect originalFrame = bar.frame; | 270 | CGRect originalFrame = bar.frame; |
| 268 | NSString *currentNumber = [NSString stringWithFormat:@"%f",value]; | 271 | NSString *currentNumber = [NSString stringWithFormat:@"%f",value]; |
| 269 | - | 272 | + |
| 270 | if ([[currentNumber substringToIndex:1] isEqualToString:@"-"] && self.showLevelLine) { | 273 | if ([[currentNumber substringToIndex:1] isEqualToString:@"-"] && self.showLevelLine) { |
| 271 | CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI); | 274 | CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI); |
| 272 | [bar setTransform:transform]; | 275 | [bar setTransform:transform]; |
| 273 | originalFrame.origin.y = bar.frame.origin.y + bar.frame.size.height; | 276 | originalFrame.origin.y = bar.frame.origin.y + bar.frame.size.height; |
| 274 | bar.frame = originalFrame; | 277 | bar.frame = originalFrame; |
| 275 | bar.isNegative = YES; | 278 | bar.isNegative = YES; |
| 276 | - | 279 | + |
| 277 | } | 280 | } |
| 278 | index += 1; | 281 | index += 1; |
| 279 | } | 282 | } |
| @@ -287,9 +290,9 @@ | @@ -287,9 +290,9 @@ | ||
| 287 | 290 | ||
| 288 | 291 | ||
| 289 | //Update Bar | 292 | //Update Bar |
| 290 | - | 293 | + |
| 291 | [self updateBar]; | 294 | [self updateBar]; |
| 292 | - | 295 | + |
| 293 | //Add chart border lines | 296 | //Add chart border lines |
| 294 | 297 | ||
| 295 | if (_showChartBorder) { | 298 | if (_showChartBorder) { |
| @@ -301,8 +304,8 @@ | @@ -301,8 +304,8 @@ | ||
| 301 | 304 | ||
| 302 | UIBezierPath *progressline = [UIBezierPath bezierPath]; | 305 | UIBezierPath *progressline = [UIBezierPath bezierPath]; |
| 303 | 306 | ||
| 304 | - [progressline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; | 307 | + [progressline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginTop)]; |
| 305 | - [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; | 308 | + [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginRight, self.frame.size.height - kXLabelHeight - _chartMarginTop)]; |
| 306 | 309 | ||
| 307 | [progressline setLineWidth:1.0]; | 310 | [progressline setLineWidth:1.0]; |
| 308 | [progressline setLineCapStyle:kCGLineCapSquare]; | 311 | [progressline setLineCapStyle:kCGLineCapSquare]; |
| @@ -319,7 +322,7 @@ | @@ -319,7 +322,7 @@ | ||
| 319 | _chartBottomLine.strokeEnd = 1.0; | 322 | _chartBottomLine.strokeEnd = 1.0; |
| 320 | 323 | ||
| 321 | [self.layer addSublayer:_chartBottomLine]; | 324 | [self.layer addSublayer:_chartBottomLine]; |
| 322 | - | 325 | + |
| 323 | //Add left Chart Line | 326 | //Add left Chart Line |
| 324 | 327 | ||
| 325 | _chartLeftLine = [CAShapeLayer layer]; | 328 | _chartLeftLine = [CAShapeLayer layer]; |
| @@ -330,8 +333,8 @@ | @@ -330,8 +333,8 @@ | ||
| 330 | 333 | ||
| 331 | UIBezierPath *progressLeftline = [UIBezierPath bezierPath]; | 334 | UIBezierPath *progressLeftline = [UIBezierPath bezierPath]; |
| 332 | 335 | ||
| 333 | - [progressLeftline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; | 336 | + [progressLeftline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginBottom + _chartMarginTop)]; |
| 334 | - [progressLeftline addLineToPoint:CGPointMake(_chartMargin, _chartMargin)]; | 337 | + [progressLeftline addLineToPoint:CGPointMake(_chartMarginLeft, _chartMarginTop)]; |
| 335 | 338 | ||
| 336 | [progressLeftline setLineWidth:1.0]; | 339 | [progressLeftline setLineWidth:1.0]; |
| 337 | [progressLeftline setLineCapStyle:kCGLineCapSquare]; | 340 | [progressLeftline setLineCapStyle:kCGLineCapSquare]; |
| @@ -349,7 +352,7 @@ | @@ -349,7 +352,7 @@ | ||
| 349 | 352 | ||
| 350 | [self.layer addSublayer:_chartLeftLine]; | 353 | [self.layer addSublayer:_chartLeftLine]; |
| 351 | } | 354 | } |
| 352 | - | 355 | + |
| 353 | // Add Level Separator Line | 356 | // Add Level Separator Line |
| 354 | if (_showLevelLine) { | 357 | if (_showLevelLine) { |
| 355 | _chartLevelLine = [CAShapeLayer layer]; | 358 | _chartLevelLine = [CAShapeLayer layer]; |
| @@ -357,27 +360,27 @@ | @@ -357,27 +360,27 @@ | ||
| 357 | _chartLevelLine.fillColor = [[UIColor whiteColor] CGColor]; | 360 | _chartLevelLine.fillColor = [[UIColor whiteColor] CGColor]; |
| 358 | _chartLevelLine.lineWidth = 1.0; | 361 | _chartLevelLine.lineWidth = 1.0; |
| 359 | _chartLevelLine.strokeEnd = 0.0; | 362 | _chartLevelLine.strokeEnd = 0.0; |
| 360 | - | 363 | + |
| 361 | UIBezierPath *progressline = [UIBezierPath bezierPath]; | 364 | UIBezierPath *progressline = [UIBezierPath bezierPath]; |
| 362 | - | 365 | + |
| 363 | - [progressline moveToPoint:CGPointMake(_chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)]; | 366 | + [progressline moveToPoint:CGPointMake(_chartMarginLeft, (self.frame.size.height - kXLabelHeight )/2.0)]; |
| 364 | - [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)]; | 367 | + [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginLeft - _chartMarginRight, (self.frame.size.height - kXLabelHeight )/2.0)]; |
| 365 | - | 368 | + |
| 366 | [progressline setLineWidth:1.0]; | 369 | [progressline setLineWidth:1.0]; |
| 367 | [progressline setLineCapStyle:kCGLineCapSquare]; | 370 | [progressline setLineCapStyle:kCGLineCapSquare]; |
| 368 | _chartLevelLine.path = progressline.CGPath; | 371 | _chartLevelLine.path = progressline.CGPath; |
| 369 | - | 372 | + |
| 370 | _chartLevelLine.strokeColor = PNLightGrey.CGColor; | 373 | _chartLevelLine.strokeColor = PNLightGrey.CGColor; |
| 371 | - | 374 | + |
| 372 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | 375 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; |
| 373 | pathAnimation.duration = 0.5; | 376 | pathAnimation.duration = 0.5; |
| 374 | pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | 377 | pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; |
| 375 | pathAnimation.fromValue = @0.0f; | 378 | pathAnimation.fromValue = @0.0f; |
| 376 | pathAnimation.toValue = @1.0f; | 379 | pathAnimation.toValue = @1.0f; |
| 377 | [_chartLevelLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | 380 | [_chartLevelLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; |
| 378 | - | 381 | + |
| 379 | _chartLevelLine.strokeEnd = 1.0; | 382 | _chartLevelLine.strokeEnd = 1.0; |
| 380 | - | 383 | + |
| 381 | [self.layer addSublayer:_chartLevelLine]; | 384 | [self.layer addSublayer:_chartLevelLine]; |
| 382 | } else { | 385 | } else { |
| 383 | if (_chartLevelLine) { | 386 | if (_chartLevelLine) { |
| @@ -91,20 +91,33 @@ | @@ -91,20 +91,33 @@ | ||
| 91 | } | 91 | } |
| 92 | else if ([self.title isEqualToString:@"Bar Chart"]) | 92 | else if ([self.title isEqualToString:@"Bar Chart"]) |
| 93 | { | 93 | { |
| 94 | + static NSNumberFormatter *barChartFormatter; | ||
| 95 | + if (!barChartFormatter){ | ||
| 96 | + barChartFormatter = [[NSNumberFormatter alloc] init]; | ||
| 97 | + barChartFormatter.numberStyle = NSNumberFormatterCurrencyStyle; | ||
| 98 | + barChartFormatter.allowsFloats = NO; | ||
| 99 | + barChartFormatter.maximumFractionDigits = 0; | ||
| 100 | + } | ||
| 94 | self.titleLabel.text = @"Bar Chart"; | 101 | self.titleLabel.text = @"Bar Chart"; |
| 95 | 102 | ||
| 96 | self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; | 103 | self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; |
| 97 | // self.barChart.showLabel = NO; | 104 | // self.barChart.showLabel = NO; |
| 98 | self.barChart.backgroundColor = [UIColor clearColor]; | 105 | self.barChart.backgroundColor = [UIColor clearColor]; |
| 99 | self.barChart.yLabelFormatter = ^(CGFloat yValue){ | 106 | self.barChart.yLabelFormatter = ^(CGFloat yValue){ |
| 100 | - CGFloat yValueParsed = yValue; | 107 | + return [barChartFormatter stringFromNumber:[NSNumber numberWithFloat:yValue]]; |
| 101 | - NSString * labelText = [NSString stringWithFormat:@"%0.f",yValueParsed]; | ||
| 102 | - return labelText; | ||
| 103 | }; | 108 | }; |
| 109 | + self.barChart.yChartLabelWidth = 60.0; | ||
| 110 | + self.barChart.chartMarginLeft = 70.0; | ||
| 111 | + self.barChart.chartMarginRight = 10.0; | ||
| 112 | + self.barChart.chartMarginTop = 5.0; | ||
| 113 | + self.barChart.chartMarginBottom = 10.0; | ||
| 114 | + | ||
| 115 | + | ||
| 104 | self.barChart.labelMarginTop = 5.0; | 116 | self.barChart.labelMarginTop = 5.0; |
| 105 | self.barChart.showChartBorder = YES; | 117 | self.barChart.showChartBorder = YES; |
| 106 | [self.barChart setXLabels:@[@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"5"]]; | 118 | [self.barChart setXLabels:@[@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"5"]]; |
| 107 | // self.barChart.yLabels = @[@-10,@0,@10]; | 119 | // self.barChart.yLabels = @[@-10,@0,@10]; |
| 120 | +// [self.barChart setYValues:@[@10000.0,@30000.0,@10000.0,@100000.0,@500000.0,@1000000.0,@1150000.0,@2150000.0]]; | ||
| 108 | [self.barChart setYValues:@[@10.82,@1.88,@6.96,@33.93,@10.82,@1.88,@6.96,@33.93]]; | 121 | [self.barChart setYValues:@[@10.82,@1.88,@6.96,@33.93,@10.82,@1.88,@6.96,@33.93]]; |
| 109 | [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNGreen,PNRed,PNGreen]]; | 122 | [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNGreen,PNRed,PNGreen]]; |
| 110 | self.barChart.isGradientShow = NO; | 123 | self.barChart.isGradientShow = NO; |
-
Please register or login to post a comment