Allow setting Top/Bottom/Left/Right margins independently in PNBarChart
Showing
3 changed files
with
40 additions
and
21 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; |
| @@ -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; |
| @@ -104,7 +107,7 @@ | @@ -104,7 +107,7 @@ | ||
| 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) { |
| @@ -114,7 +117,7 @@ | @@ -114,7 +117,7 @@ | ||
| 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; |
| @@ -153,7 +156,7 @@ | @@ -153,7 +156,7 @@ | ||
| 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; |
| @@ -171,13 +174,13 @@ | @@ -171,13 +174,13 @@ | ||
| 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]; |
| @@ -197,7 +200,7 @@ | @@ -197,7 +200,7 @@ | ||
| 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) { |
| @@ -212,9 +215,9 @@ | @@ -212,9 +215,9 @@ | ||
| 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 | ||
| @@ -226,7 +229,7 @@ | @@ -226,7 +229,7 @@ | ||
| 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 | ||
| @@ -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]; |
| @@ -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]; |
| @@ -360,8 +363,8 @@ | @@ -360,8 +363,8 @@ | ||
| 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]; |
| @@ -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