dullgrass

no message

@@ -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,8 +71,12 @@ @@ -70,8 +71,12 @@
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;
73 - _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1; 74 +
74 - 75 + // !!!: 调整Y坐标的显示值
  76 + if (_yLabelSum==defaultYSum) {
  77 + _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1;
  78 + }
  79 +
75 if (_yMaxValue) { 80 if (_yMaxValue) {
76 _yValueMax = _yMaxValue; 81 _yValueMax = _yMaxValue;
77 } else { 82 } else {
@@ -86,10 +91,36 @@ @@ -86,10 +91,36 @@
86 91
87 if (_showLabel) { 92 if (_showLabel) {
88 //Add y labels 93 //Add y labels
  94 + float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
  95 +
  96 + // !!!: 用户自行修改Y轴坐标值
  97 + if (_yLabels) {
89 98
90 - float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum; 99 + [self getYValueMax:_yLabels];
91 100
92 - for (int index = 0; index < _yLabelSum; index++) { 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 +
  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 ));
95 126
@@ -106,6 +137,7 @@ @@ -106,6 +137,7 @@
106 [self addSubview:label]; 137 [self addSubview:label];
107 138
108 } 139 }
  140 + }
109 } 141 }
110 } 142 }
111 143