Kevin

Merge pull request #197 from dullgrass/master

Added the new feature support custom yLabels
... ... @@ -39,7 +39,7 @@
- (void)setGrade:(float)grade
{
NSLog(@"New garde %f",grade);
// NSLog(@"New garde %f",grade);
CGFloat startPosY = (1 - grade) * self.frame.size.height;
... ... @@ -202,7 +202,7 @@
[_chartLine addSublayer:self.textLayer];
[self.textLayer setFontSize:textheigt/2];
[self.textLayer setString:[[NSString alloc]initWithFormat:@"%ld",(NSInteger)(grade*100)]];
[self.textLayer setString:[[NSString alloc]initWithFormat:@"%0.f",grade*100]];
[self.textLayer setFrame:CGRectMake(0, textStartPosY, textWidth, textheigt)];
self.textLayer.contentsScale = [UIScreen mainScreen].scale;
... ...
... ... @@ -10,7 +10,6 @@
#import "PNColor.h"
#import "PNChartLabel.h"
@interface PNBarChart () {
NSMutableArray *_xChartLabels;
NSMutableArray *_yChartLabels;
... ... @@ -67,48 +66,55 @@
- (void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
//make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
int yLabelsDifTotal = (int)[NSSet setWithArray:yValues].count;
_yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1;
if (_showLabel) {
[self __addYCoordinateLabelsValues];
}
}
#pragma mark - Private Method
#pragma mark - 添加柱状图的Y轴坐标
- (void)__addYCoordinateLabelsValues{
[self viewCleanupForCollection:_yChartLabels];
NSArray *yAxisValues = _yLabels ? _yLabels : _yValues;
if (_yMaxValue) {
_yValueMax = _yMaxValue;
} else {
[self getYValueMax:yValues];
[self getYValueMax:yAxisValues];
}
if (_yChartLabels) {
[self viewCleanupForCollection:_yChartLabels];
}else{
_yLabels = [NSMutableArray new];
if (_yLabelSum==4) {
_yLabelSum = yAxisValues.count;
(_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++;
}
if (_showLabel) {
//Add y labels
float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
for (int index = 0; index < _yLabelSum; index++) {
NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum ));
float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
for (int i = 0; i < _yLabelSum; i++) {
NSString *labelText;
if (_yLabels) {
float yAsixValue = [_yLabels[_yLabels.count - i - 1] floatValue];
labelText= _yLabelFormatter(yAsixValue);
} else {
labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum ));
}
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0,
yLabelSectionHeight * index + _chartMargin - kYLabelHeight/2.0,
_yChartLabelWidth,
kYLabelHeight)];
CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame];
label.font = _labelFont;
label.textColor = _labelTextColor;
[label setTextAlignment:NSTextAlignmentRight];
label.text = labelText;
[_yChartLabels addObject:label];
[self addSubview:label];
}
[_yChartLabels addObject:label];
}
}
-(void)updateChartData:(NSArray *)data{
self.yValues = data;
[self updateBar];
... ...
... ... @@ -103,7 +103,12 @@
self.barChart.labelMarginTop = 5.0;
[self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
self.barChart.rotateForXAxisText = true ;
self.barChart.yLabelSum=5;
self.barChart.yMaxValue=100;
[self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
// self.barChart.yLabels = @[@0,@20,@40,@60];
[self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
// Adding gradient
self.barChart.barColorGradientStart = [UIColor blueColor];
... ...