Showing
1 changed file
with
33 additions
and
1 deletions
| @@ -10,6 +10,7 @@ | @@ -10,6 +10,7 @@ | ||
| 10 | #import "PNColor.h" | 10 | #import "PNColor.h" |
| 11 | #import "PNChartLabel.h" | 11 | #import "PNChartLabel.h" |
| 12 | 12 | ||
| 13 | +#define defaultYSum 0 //默认的Y显示值的个数, | ||
| 13 | 14 | ||
| 14 | @interface PNBarChart () { | 15 | @interface PNBarChart () { |
| 15 | NSMutableArray *_xChartLabels; | 16 | NSMutableArray *_xChartLabels; |
| @@ -70,7 +71,11 @@ | @@ -70,7 +71,11 @@ | ||
| 70 | 71 | ||
| 71 | //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis | 72 | //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 | int yLabelsDifTotal = (int)[NSSet setWithArray:yValues].count; |
| 74 | + | ||
| 75 | + // !!!: 调整Y坐标的显示值 | ||
| 76 | + if (_yLabelSum==defaultYSum) { | ||
| 73 | _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1; | 77 | _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1; |
| 78 | + } | ||
| 74 | 79 | ||
| 75 | if (_yMaxValue) { | 80 | if (_yMaxValue) { |
| 76 | _yValueMax = _yMaxValue; | 81 | _yValueMax = _yMaxValue; |
| @@ -86,9 +91,35 @@ | @@ -86,9 +91,35 @@ | ||
| 86 | 91 | ||
| 87 | if (_showLabel) { | 92 | if (_showLabel) { |
| 88 | //Add y labels | 93 | //Add y labels |
| 89 | - | ||
| 90 | float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; | 94 | float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; |
| 91 | 95 | ||
| 96 | + // !!!: 用户自行修改Y轴坐标值 | ||
| 97 | + if (_yLabels) { | ||
| 98 | + | ||
| 99 | + [self getYValueMax:_yLabels]; | ||
| 100 | + | ||
| 101 | + for (int i=0; i<_yLabels.count; i++) { | ||
| 102 | + float yAsixValues=[[_yLabels objectAtIndex:_yLabels.count-i-1] floatValue]; | ||
| 103 | + NSString *labelText= _yLabelFormatter(yAsixValues); | ||
| 104 | + | ||
| 105 | + | ||
| 106 | + | ||
| 107 | + PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0, | ||
| 108 | + yLabelSectionHeight * i + _chartMargin - kYLabelHeight/2.0, | ||
| 109 | + _yChartLabelWidth, | ||
| 110 | + kYLabelHeight)]; | ||
| 111 | + label.font = _labelFont; | ||
| 112 | + label.textColor = _labelTextColor; | ||
| 113 | + [label setTextAlignment:NSTextAlignmentRight]; | ||
| 114 | + label.text = labelText; | ||
| 115 | + | ||
| 116 | + [_yChartLabels addObject:label]; | ||
| 117 | + [self addSubview:label]; | ||
| 118 | + | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + }else{ | ||
| 122 | + | ||
| 92 | for (int index = 0; index < _yLabelSum; index++) { | 123 | for (int index = 0; index < _yLabelSum; index++) { |
| 93 | 124 | ||
| 94 | NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum )); | 125 | NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum )); |
| @@ -107,6 +138,7 @@ | @@ -107,6 +138,7 @@ | ||
| 107 | 138 | ||
| 108 | } | 139 | } |
| 109 | } | 140 | } |
| 141 | + } | ||
| 110 | } | 142 | } |
| 111 | 143 | ||
| 112 | -(void)updateChartData:(NSArray *)data{ | 144 | -(void)updateChartData:(NSArray *)data{ |
-
Please register or login to post a comment