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);
/** Suffix to y label values, none if unset. */
@property (nonatomic) NSString *yLabelSuffix;
@property (nonatomic) CGFloat chartMargin;
@property (nonatomic) CGFloat chartMarginLeft;
@property (nonatomic) CGFloat chartMarginRight;
@property (nonatomic) CGFloat chartMarginTop;
@property (nonatomic) CGFloat chartMarginBottom;
/** Controls whether labels should be displayed. */
@property (nonatomic) BOOL showLabel;
... ...
... ... @@ -56,7 +56,10 @@
_xLabelSkip = 1;
_yLabelSum = 4;
_labelMarginTop = 0;
_chartMargin = 25.0;
_chartMarginLeft = 25.0;
_chartMarginRight = 25.0;
_chartMarginTop = 25.0;
_chartMarginBottom = 25.0;
_barRadius = 2.0;
_showChartBorder = NO;
_showLevelLine = NO;
... ... @@ -106,7 +109,8 @@
[self processYMaxValue];
float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
float sectionHeight = (self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight) / _yLabelSum;
for (int i = 0; i <= _yLabelSum; i++) {
NSString *labelText;
if (_yLabels) {
... ... @@ -116,14 +120,16 @@
labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum ));
}
CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame];
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectZero];
label.font = _labelFont;
label.textColor = _labelTextColor;
[label setTextAlignment:NSTextAlignmentRight];
label.text = [NSString stringWithFormat:@"%@%@%@", _yLabelPrefix, labelText, _yLabelSuffix];
[self addSubview:label];
label.frame = (CGRect){0, sectionHeight * i + _chartMarginTop - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
[_yChartLabels addObject:label];
}
}
... ... @@ -155,7 +161,7 @@
_xChartLabels = [NSMutableArray new];
}
_xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count];
_xLabelWidth = (self.frame.size.width - _chartMarginLeft - _chartMarginRight) / [xLabels count];
if (_showLabel) {
int labelAddCount = 0;
... ... @@ -173,13 +179,13 @@
CGFloat labelXPosition;
if (_rotateForXAxisText){
label.transform = CGAffineTransformMakeRotation(M_PI / 4);
labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /1.5);
labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /1.5);
}
else{
labelXPosition = (index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 );
labelXPosition = (index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 );
}
label.center = CGPointMake(labelXPosition,
self.frame.size.height - kXLabelHeight - _chartMargin + label.frame.size.height /2.0 + _labelMarginTop);
self.frame.size.height - kXLabelHeight - _chartMarginTop + label.frame.size.height /2.0 + _labelMarginTop);
labelAddCount = 0;
[_xChartLabels addObject:label];
... ... @@ -199,7 +205,7 @@
{
//Add bars
CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - kXLabelHeight;
CGFloat chartCavanHeight = self.frame.size.height - _chartMarginTop - _chartMarginBottom - kXLabelHeight;
NSInteger index = 0;
for (NSNumber *valueString in _yValues) {
... ... @@ -214,9 +220,9 @@
if (_barWidth) {
barWidth = _barWidth;
barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 - _barWidth /2.0;
barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth /2.0 - _barWidth /2.0;
}else{
barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth * 0.25;
barXPosition = index * _xLabelWidth + _chartMarginLeft + _xLabelWidth * 0.25;
if (_showLabel) {
barWidth = _xLabelWidth * 0.5;
... ... @@ -228,7 +234,7 @@
}
bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position
self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMargin , //Bar Y position
self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMarginTop , //Bar Y position
barWidth, // Bar witdh
self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height
... ... @@ -303,8 +309,8 @@
UIBezierPath *progressline = [UIBezierPath bezierPath];
[progressline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)];
[progressline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginTop)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginRight, self.frame.size.height - kXLabelHeight - _chartMarginTop)];
[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];
... ... @@ -332,8 +338,8 @@
UIBezierPath *progressLeftline = [UIBezierPath bezierPath];
[progressLeftline moveToPoint:CGPointMake(_chartMargin, self.frame.size.height - kXLabelHeight - _chartMargin)];
[progressLeftline addLineToPoint:CGPointMake(_chartMargin, _chartMargin)];
[progressLeftline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginBottom + _chartMarginTop)];
[progressLeftline addLineToPoint:CGPointMake(_chartMarginLeft, _chartMarginTop)];
[progressLeftline setLineWidth:1.0];
[progressLeftline setLineCapStyle:kCGLineCapSquare];
... ... @@ -362,8 +368,8 @@
UIBezierPath *progressline = [UIBezierPath bezierPath];
[progressline moveToPoint:CGPointMake(_chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline moveToPoint:CGPointMake(_chartMarginLeft, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginLeft - _chartMarginRight, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];
... ...
... ... @@ -91,20 +91,34 @@
}
else if ([self.title isEqualToString:@"Bar Chart"])
{
static NSNumberFormatter *barChartFormatter;
if (!barChartFormatter){
barChartFormatter = [[NSNumberFormatter alloc] init];
barChartFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
barChartFormatter.allowsFloats = NO;
barChartFormatter.maximumFractionDigits = 0;
}
self.titleLabel.text = @"Bar Chart";
self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
// self.barChart.showLabel = NO;
self.barChart.backgroundColor = [UIColor clearColor];
self.barChart.yLabelFormatter = ^(CGFloat yValue){
CGFloat yValueParsed = yValue;
NSString * labelText = [NSString stringWithFormat:@"%0.f",yValueParsed];
return labelText;
return [barChartFormatter stringFromNumber:[NSNumber numberWithFloat:yValue]];
};
self.barChart.yChartLabelWidth = 20.0;
self.barChart.chartMarginLeft = 30.0;
self.barChart.chartMarginRight = 10.0;
self.barChart.chartMarginTop = 5.0;
self.barChart.chartMarginBottom = 10.0;
self.barChart.labelMarginTop = 5.0;
self.barChart.showChartBorder = YES;
[self.barChart setXLabels:@[@"2",@"3",@"4",@"5",@"2",@"3",@"4",@"5"]];
// self.barChart.yLabels = @[@-10,@0,@10];
// [self.barChart setYValues:@[@10000.0,@30000.0,@10000.0,@100000.0,@500000.0,@1000000.0,@1150000.0,@2150000.0]];
[self.barChart setYValues:@[@10.82,@1.88,@6.96,@33.93,@10.82,@1.88,@6.96,@33.93]];
[self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNGreen,PNRed,PNGreen]];
self.barChart.isGradientShow = NO;
... ...