Merge pull request #197 from dullgrass/master
Added the new feature support custom yLabels
Showing
3 changed files
with
37 additions
and
26 deletions
| @@ -39,7 +39,7 @@ | @@ -39,7 +39,7 @@ | ||
| 39 | 39 | ||
| 40 | - (void)setGrade:(float)grade | 40 | - (void)setGrade:(float)grade |
| 41 | { | 41 | { |
| 42 | - NSLog(@"New garde %f",grade); | 42 | +// NSLog(@"New garde %f",grade); |
| 43 | 43 | ||
| 44 | CGFloat startPosY = (1 - grade) * self.frame.size.height; | 44 | CGFloat startPosY = (1 - grade) * self.frame.size.height; |
| 45 | 45 | ||
| @@ -202,7 +202,7 @@ | @@ -202,7 +202,7 @@ | ||
| 202 | [_chartLine addSublayer:self.textLayer]; | 202 | [_chartLine addSublayer:self.textLayer]; |
| 203 | [self.textLayer setFontSize:textheigt/2]; | 203 | [self.textLayer setFontSize:textheigt/2]; |
| 204 | 204 | ||
| 205 | - [self.textLayer setString:[[NSString alloc]initWithFormat:@"%ld",(NSInteger)(grade*100)]]; | 205 | + [self.textLayer setString:[[NSString alloc]initWithFormat:@"%0.f",grade*100]]; |
| 206 | [self.textLayer setFrame:CGRectMake(0, textStartPosY, textWidth, textheigt)]; | 206 | [self.textLayer setFrame:CGRectMake(0, textStartPosY, textWidth, textheigt)]; |
| 207 | self.textLayer.contentsScale = [UIScreen mainScreen].scale; | 207 | self.textLayer.contentsScale = [UIScreen mainScreen].scale; |
| 208 | 208 |
| @@ -10,7 +10,6 @@ | @@ -10,7 +10,6 @@ | ||
| 10 | #import "PNColor.h" | 10 | #import "PNColor.h" |
| 11 | #import "PNChartLabel.h" | 11 | #import "PNChartLabel.h" |
| 12 | 12 | ||
| 13 | - | ||
| 14 | @interface PNBarChart () { | 13 | @interface PNBarChart () { |
| 15 | NSMutableArray *_xChartLabels; | 14 | NSMutableArray *_xChartLabels; |
| 16 | NSMutableArray *_yChartLabels; | 15 | NSMutableArray *_yChartLabels; |
| @@ -67,48 +66,55 @@ | @@ -67,48 +66,55 @@ | ||
| 67 | - (void)setYValues:(NSArray *)yValues | 66 | - (void)setYValues:(NSArray *)yValues |
| 68 | { | 67 | { |
| 69 | _yValues = yValues; | 68 | _yValues = yValues; |
| 70 | - | ||
| 71 | //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis | 69 | //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis |
| 72 | - int yLabelsDifTotal = (int)[NSSet setWithArray:yValues].count; | ||
| 73 | - _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1; | ||
| 74 | 70 | ||
| 71 | + if (_showLabel) { | ||
| 72 | + [self __addYCoordinateLabelsValues]; | ||
| 73 | + } | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +#pragma mark - Private Method | ||
| 77 | +#pragma mark - 添加柱状图的Y轴坐标 | ||
| 78 | +- (void)__addYCoordinateLabelsValues{ | ||
| 79 | + | ||
| 80 | + [self viewCleanupForCollection:_yChartLabels]; | ||
| 81 | + | ||
| 82 | + NSArray *yAxisValues = _yLabels ? _yLabels : _yValues; | ||
| 75 | if (_yMaxValue) { | 83 | if (_yMaxValue) { |
| 76 | _yValueMax = _yMaxValue; | 84 | _yValueMax = _yMaxValue; |
| 77 | } else { | 85 | } else { |
| 78 | - [self getYValueMax:yValues]; | 86 | + [self getYValueMax:yAxisValues]; |
| 79 | } | 87 | } |
| 80 | 88 | ||
| 81 | - if (_yChartLabels) { | 89 | + if (_yLabelSum==4) { |
| 82 | - [self viewCleanupForCollection:_yChartLabels]; | 90 | + _yLabelSum = yAxisValues.count; |
| 83 | - }else{ | 91 | + (_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++; |
| 84 | - _yLabels = [NSMutableArray new]; | ||
| 85 | } | 92 | } |
| 86 | 93 | ||
| 87 | - if (_showLabel) { | 94 | + float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; |
| 88 | - //Add y labels | 95 | + for (int i = 0; i < _yLabelSum; i++) { |
| 89 | - | 96 | + NSString *labelText; |
| 90 | - float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; | 97 | + if (_yLabels) { |
| 91 | - | 98 | + float yAsixValue = [_yLabels[_yLabels.count - i - 1] floatValue]; |
| 92 | - for (int index = 0; index < _yLabelSum; index++) { | 99 | + labelText= _yLabelFormatter(yAsixValue); |
| 93 | - | 100 | + } else { |
| 94 | - NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum )); | 101 | + labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum )); |
| 102 | + } | ||
| 95 | 103 | ||
| 96 | - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0, | 104 | + CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight}; |
| 97 | - yLabelSectionHeight * index + _chartMargin - kYLabelHeight/2.0, | 105 | + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame]; |
| 98 | - _yChartLabelWidth, | ||
| 99 | - kYLabelHeight)]; | ||
| 100 | label.font = _labelFont; | 106 | label.font = _labelFont; |
| 101 | label.textColor = _labelTextColor; | 107 | label.textColor = _labelTextColor; |
| 102 | [label setTextAlignment:NSTextAlignmentRight]; | 108 | [label setTextAlignment:NSTextAlignmentRight]; |
| 103 | label.text = labelText; | 109 | label.text = labelText; |
| 104 | - | ||
| 105 | - [_yChartLabels addObject:label]; | ||
| 106 | [self addSubview:label]; | 110 | [self addSubview:label]; |
| 107 | 111 | ||
| 108 | - } | 112 | + [_yChartLabels addObject:label]; |
| 109 | } | 113 | } |
| 110 | } | 114 | } |
| 111 | 115 | ||
| 116 | + | ||
| 117 | + | ||
| 112 | -(void)updateChartData:(NSArray *)data{ | 118 | -(void)updateChartData:(NSArray *)data{ |
| 113 | self.yValues = data; | 119 | self.yValues = data; |
| 114 | [self updateBar]; | 120 | [self updateBar]; |
| @@ -103,7 +103,12 @@ | @@ -103,7 +103,12 @@ | ||
| 103 | self.barChart.labelMarginTop = 5.0; | 103 | self.barChart.labelMarginTop = 5.0; |
| 104 | [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; | 104 | [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; |
| 105 | self.barChart.rotateForXAxisText = true ; | 105 | self.barChart.rotateForXAxisText = true ; |
| 106 | + | ||
| 107 | + self.barChart.yLabelSum=5; | ||
| 108 | + self.barChart.yMaxValue=100; | ||
| 109 | + | ||
| 106 | [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; | 110 | [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; |
| 111 | +// self.barChart.yLabels = @[@0,@20,@40,@60]; | ||
| 107 | [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; | 112 | [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; |
| 108 | // Adding gradient | 113 | // Adding gradient |
| 109 | self.barChart.barColorGradientStart = [UIColor blueColor]; | 114 | self.barChart.barColorGradientStart = [UIColor blueColor]; |
-
Please register or login to post a comment