kevinzhow

Merge branch 'independant-margins-bar-chart' of https://github.com/openhood/PNCh…

…art into openhood-independant-margins-bar-chart
@@ -52,7 +52,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); @@ -52,7 +52,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
52 /** Suffix to y label values, none if unset. */ 52 /** Suffix to y label values, none if unset. */
53 @property (nonatomic) NSString *yLabelSuffix; 53 @property (nonatomic) NSString *yLabelSuffix;
54 54
55 -@property (nonatomic) CGFloat chartMargin; 55 +@property (nonatomic) CGFloat chartMarginLeft;
  56 +@property (nonatomic) CGFloat chartMarginRight;
  57 +@property (nonatomic) CGFloat chartMarginTop;
  58 +@property (nonatomic) CGFloat chartMarginBottom;
56 59
57 /** Controls whether labels should be displayed. */ 60 /** Controls whether labels should be displayed. */
58 @property (nonatomic) BOOL showLabel; 61 @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;
@@ -106,7 +109,8 @@ @@ -106,7 +109,8 @@
106 109
107 [self processYMaxValue]; 110 [self processYMaxValue];
108 111
109 - float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; 112 + float sectionHeight = (self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight) / _yLabelSum;
  113 +
110 for (int i = 0; i <= _yLabelSum; i++) { 114 for (int i = 0; i <= _yLabelSum; i++) {
111 NSString *labelText; 115 NSString *labelText;
112 if (_yLabels) { 116 if (_yLabels) {
@@ -116,14 +120,16 @@ @@ -116,14 +120,16 @@
116 labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum )); 120 labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum ));
117 } 121 }
118 122
119 - CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight}; 123 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectZero];
120 - PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame];  
121 label.font = _labelFont; 124 label.font = _labelFont;
122 label.textColor = _labelTextColor; 125 label.textColor = _labelTextColor;
123 [label setTextAlignment:NSTextAlignmentRight]; 126 [label setTextAlignment:NSTextAlignmentRight];
124 label.text = [NSString stringWithFormat:@"%@%@%@", _yLabelPrefix, labelText, _yLabelSuffix]; 127 label.text = [NSString stringWithFormat:@"%@%@%@", _yLabelPrefix, labelText, _yLabelSuffix];
  128 +
125 [self addSubview:label]; 129 [self addSubview:label];
126 130
  131 + label.frame = (CGRect){0, sectionHeight * i + _chartMarginTop - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
  132 +
127 [_yChartLabels addObject:label]; 133 [_yChartLabels addObject:label];
128 } 134 }
129 } 135 }
@@ -155,7 +161,7 @@ @@ -155,7 +161,7 @@
155 _xChartLabels = [NSMutableArray new]; 161 _xChartLabels = [NSMutableArray new];
156 } 162 }
157 163
158 - _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count]; 164 + _xLabelWidth = (self.frame.size.width - _chartMarginLeft - _chartMarginRight) / [xLabels count];
159 165
160 if (_showLabel) { 166 if (_showLabel) {
161 int labelAddCount = 0; 167 int labelAddCount = 0;
@@ -173,13 +179,13 @@ @@ -173,13 +179,13 @@
173 CGFloat labelXPosition; 179 CGFloat labelXPosition;
174 if (_rotateForXAxisText){ 180 if (_rotateForXAxisText){
175 label.transform = CGAffineTransformMakeRotation(M_PI / 4); 181 label.transform = CGAffineTransformMakeRotation(M_PI / 4);
176 - labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /1.5); 182 + labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /1.5);
177 } 183 }
178 else{ 184 else{
179 - labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 ); 185 + labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 );
180 } 186 }
181 label.center = CGPointMake(labelXPosition, 187 label.center = CGPointMake(labelXPosition,
182 - self.frame.size.height - kXLabelHeight - _chartMargin + label.frame.size.height /2.0 + _labelMarginTop); 188 + self.frame.size.height - kXLabelHeight - _chartMarginTop + label.frame.size.height /2.0 + _labelMarginTop);
183 labelAddCount = 0; 189 labelAddCount = 0;
184 190
185 [_xChartLabels addObject:label]; 191 [_xChartLabels addObject:label];
@@ -199,7 +205,7 @@ @@ -199,7 +205,7 @@
199 { 205 {
200 206
201 //Add bars 207 //Add bars
202 - CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - kXLabelHeight; 208 + CGFloat chartCavanHeight = self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight;
203 NSInteger index = 0; 209 NSInteger index = 0;
204 210
205 for (NSNumber *valueString in _yValues) { 211 for (NSNumber *valueString in _yValues) {
@@ -214,9 +220,9 @@ @@ -214,9 +220,9 @@
214 220
215 if (_barWidth) { 221 if (_barWidth) {
216 barWidth = _barWidth; 222 barWidth = _barWidth;
217 - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 - _barWidth /2.0; 223 + barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 - _barWidth /2.0;
218 }else{ 224 }else{
219 - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth * 0.25; 225 + barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth * 0.25;
220 if (_showLabel) { 226 if (_showLabel) {
221 barWidth = _xLabelWidth * 0.5; 227 barWidth = _xLabelWidth * 0.5;
222 228
@@ -228,7 +234,7 @@ @@ -228,7 +234,7 @@
228 } 234 }
229 235
230 bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position 236 bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position
231 - self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMargin , //Bar Y position 237 + self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMarginTop , //Bar Y position
232 barWidth, // Bar witdh 238 barWidth, // Bar witdh
233 self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height 239 self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height
234 240
@@ -303,8 +309,8 @@ @@ -303,8 +309,8 @@
303 309
304 UIBezierPath *progressline = [UIBezierPath bezierPath]; 310 UIBezierPath *progressline = [UIBezierPath bezierPath];
305 311
306 - [progressline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; 312 + [progressline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginTop)];
307 - [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; 313 + [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginRight, self.frame.size.height - kXLabelHeight - _chartMarginTop)];
308 314
309 [progressline setLineWidth:1.0]; 315 [progressline setLineWidth:1.0];
310 [progressline setLineCapStyle:kCGLineCapSquare]; 316 [progressline setLineCapStyle:kCGLineCapSquare];
@@ -332,8 +338,8 @@ @@ -332,8 +338,8 @@
332 338
333 UIBezierPath *progressLeftline = [UIBezierPath bezierPath]; 339 UIBezierPath *progressLeftline = [UIBezierPath bezierPath];
334 340
335 - [progressLeftline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)]; 341 + [progressLeftline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginBottom + _chartMarginTop)];
336 - [progressLeftline addLineToPoint:CGPointMake(_chartMargin, _chartMargin)]; 342 + [progressLeftline addLineToPoint:CGPointMake(_chartMarginLeft, _chartMarginTop)];
337 343
338 [progressLeftline setLineWidth:1.0]; 344 [progressLeftline setLineWidth:1.0];
339 [progressLeftline setLineCapStyle:kCGLineCapSquare]; 345 [progressLeftline setLineCapStyle:kCGLineCapSquare];
@@ -362,8 +368,8 @@ @@ -362,8 +368,8 @@
362 368
363 UIBezierPath *progressline = [UIBezierPath bezierPath]; 369 UIBezierPath *progressline = [UIBezierPath bezierPath];
364 370
365 - [progressline moveToPoint:CGPointMake(_chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)]; 371 + [progressline moveToPoint:CGPointMake(_chartMarginLeft, (self.frame.size.height - kXLabelHeight )/2.0)];
366 - [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)]; 372 + [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginLeft - _chartMarginRight, (self.frame.size.height - kXLabelHeight )/2.0)];
367 373
368 [progressline setLineWidth:1.0]; 374 [progressline setLineWidth:1.0];
369 [progressline setLineCapStyle:kCGLineCapSquare]; 375 [progressline setLineCapStyle:kCGLineCapSquare];
@@ -91,20 +91,34 @@ @@ -91,20 +91,34 @@
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 +
  110 + self.barChart.yChartLabelWidth = 20.0;
  111 + self.barChart.chartMarginLeft = 30.0;
  112 + self.barChart.chartMarginRight = 10.0;
  113 + self.barChart.chartMarginTop = 5.0;
  114 + self.barChart.chartMarginBottom = 10.0;
  115 +
  116 +
104 self.barChart.labelMarginTop = 5.0; 117 self.barChart.labelMarginTop = 5.0;
105 self.barChart.showChartBorder = YES; 118 self.barChart.showChartBorder = YES;
106 [self.barChart setXLabels:@[@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"5"]]; 119 [self.barChart setXLabels:@[@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"5"]];
107 // self.barChart.yLabels = @[@-10,@0,@10]; 120 // self.barChart.yLabels = @[@-10,@0,@10];
  121 +// [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]]; 122 [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]]; 123 [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNGreen,PNRed,PNGreen]];
110 self.barChart.isGradientShow = NO; 124 self.barChart.isGradientShow = NO;