Kevin

Merge pull request #197 from dullgrass/master

Added the new feature support custom yLabels
@@ -39,8 +39,8 @@ @@ -39,8 +39,8 @@
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
46 UIBezierPath *progressline = [UIBezierPath bezierPath]; 46 UIBezierPath *progressline = [UIBezierPath bezierPath];
@@ -201,8 +201,8 @@ @@ -201,8 +201,8 @@
201 201
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;
  69 + //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
  70 +
  71 + if (_showLabel) {
  72 + [self __addYCoordinateLabelsValues];
  73 + }
  74 +}
70 75
71 - //make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis 76 +#pragma mark - Private Method
72 - int yLabelsDifTotal = (int)[NSSet setWithArray:yValues].count; 77 +#pragma mark - 添加柱状图的Y轴坐标
73 - _yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1; 78 +- (void)__addYCoordinateLabelsValues{
74 - 79 +
75 - if (_yMaxValue) { 80 + [self viewCleanupForCollection:_yChartLabels];
76 - _yValueMax = _yMaxValue; 81 +
  82 + NSArray *yAxisValues = _yLabels ? _yLabels : _yValues;
  83 + if (_yMaxValue) {
  84 + _yValueMax = _yMaxValue;
  85 + } else {
  86 + [self getYValueMax:yAxisValues];
  87 + }
  88 +
  89 + if (_yLabelSum==4) {
  90 + _yLabelSum = yAxisValues.count;
  91 + (_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++;
  92 + }
  93 +
  94 + float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
  95 + for (int i = 0; i < _yLabelSum; i++) {
  96 + NSString *labelText;
  97 + if (_yLabels) {
  98 + float yAsixValue = [_yLabels[_yLabels.count - i - 1] floatValue];
  99 + labelText= _yLabelFormatter(yAsixValue);
77 } else { 100 } else {
78 - [self getYValueMax:yValues]; 101 + labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum ));
79 } 102 }
80 103
81 - if (_yChartLabels) { 104 + CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
82 - [self viewCleanupForCollection:_yChartLabels]; 105 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame];
83 - }else{ 106 + label.font = _labelFont;
84 - _yLabels = [NSMutableArray new]; 107 + label.textColor = _labelTextColor;
85 - } 108 + [label setTextAlignment:NSTextAlignmentRight];
  109 + label.text = labelText;
  110 + [self addSubview:label];
86 111
87 - if (_showLabel) { 112 + [_yChartLabels addObject:label];
88 - //Add y labels 113 + }
89 -  
90 - float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;  
91 -  
92 - for (int index = 0; index < _yLabelSum; index++) {  
93 -  
94 - NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum ));  
95 -  
96 - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0,  
97 - yLabelSectionHeight * index + _chartMargin - kYLabelHeight/2.0,  
98 - _yChartLabelWidth,  
99 - kYLabelHeight)];  
100 - label.font = _labelFont;  
101 - label.textColor = _labelTextColor;  
102 - [label setTextAlignment:NSTextAlignmentRight];  
103 - label.text = labelText;  
104 -  
105 - [_yChartLabels addObject:label];  
106 - [self addSubview:label];  
107 -  
108 - }  
109 - }  
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];
@@ -238,7 +244,7 @@ @@ -238,7 +244,7 @@
238 float value = [valueString floatValue]; 244 float value = [valueString floatValue];
239 245
240 float grade = (float)value / (float)_yValueMax; 246 float grade = (float)value / (float)_yValueMax;
241 - 247 +
242 if (isnan(grade)) { 248 if (isnan(grade)) {
243 grade = 0; 249 grade = 0;
244 } 250 }
@@ -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];