cleaned up code, removed some not necessary code and fixed issue #23
Showing
2 changed files
with
17 additions
and
25 deletions
| @@ -37,8 +37,6 @@ | @@ -37,8 +37,6 @@ | ||
| 37 | 37 | ||
| 38 | @property (nonatomic) CGFloat xLabelWidth; | 38 | @property (nonatomic) CGFloat xLabelWidth; |
| 39 | 39 | ||
| 40 | -@property (nonatomic) CGFloat xLabelHeight; | ||
| 41 | - | ||
| 42 | @property (nonatomic) CGFloat yValueMax; | 40 | @property (nonatomic) CGFloat yValueMax; |
| 43 | 41 | ||
| 44 | @property (nonatomic) CGFloat yValueMin; | 42 | @property (nonatomic) CGFloat yValueMin; |
| @@ -55,7 +55,7 @@ | @@ -55,7 +55,7 @@ | ||
| 55 | -(void)setYLabels:(NSArray *)yLabels | 55 | -(void)setYLabels:(NSArray *)yLabels |
| 56 | { | 56 | { |
| 57 | 57 | ||
| 58 | - float yStep = (_yValueMax-_yValueMin) / _yLabelNum; | 58 | + CGFloat yStep = (_yValueMax-_yValueMin) / _yLabelNum; |
| 59 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; | 59 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; |
| 60 | 60 | ||
| 61 | 61 | ||
| @@ -75,13 +75,13 @@ | @@ -75,13 +75,13 @@ | ||
| 75 | -(void)setXLabels:(NSArray *)xLabels | 75 | -(void)setXLabels:(NSArray *)xLabels |
| 76 | { | 76 | { |
| 77 | _xLabels = xLabels; | 77 | _xLabels = xLabels; |
| 78 | - | 78 | + NSString* labelText; |
| 79 | if(_showLabel){ | 79 | if(_showLabel){ |
| 80 | _xLabelWidth = _chartCavanWidth/[xLabels count]; | 80 | _xLabelWidth = _chartCavanWidth/[xLabels count]; |
| 81 | 81 | ||
| 82 | for(int index = 0; index < xLabels.count; index++) | 82 | for(int index = 0; index < xLabels.count; index++) |
| 83 | { | 83 | { |
| 84 | - NSString* labelText = xLabels[index]; | 84 | + labelText = xLabels[index]; |
| 85 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2*_chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)]; | 85 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2*_chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)]; |
| 86 | [label setTextAlignment:NSTextAlignmentCenter]; | 86 | [label setTextAlignment:NSTextAlignmentCenter]; |
| 87 | label.text = labelText; | 87 | label.text = labelText; |
| @@ -138,22 +138,21 @@ | @@ -138,22 +138,21 @@ | ||
| 138 | for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { | 138 | for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { |
| 139 | PNLineChartData *chartData = self.chartData[lineIndex]; | 139 | PNLineChartData *chartData = self.chartData[lineIndex]; |
| 140 | CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[lineIndex]; | 140 | CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[lineIndex]; |
| 141 | + CGFloat yValue; | ||
| 142 | + CGFloat innerGrade; | ||
| 143 | + CGPoint point; | ||
| 141 | 144 | ||
| 142 | UIGraphicsBeginImageContext(self.frame.size); | 145 | UIGraphicsBeginImageContext(self.frame.size); |
| 143 | UIBezierPath * progressline = [UIBezierPath bezierPath]; | 146 | UIBezierPath * progressline = [UIBezierPath bezierPath]; |
| 144 | [_chartPath addObject:progressline]; | 147 | [_chartPath addObject:progressline]; |
| 145 | 148 | ||
| 146 | - CGFloat xPosition = _chartMargin; | 149 | + |
| 147 | 150 | ||
| 148 | if(!_showLabel){ | 151 | if(!_showLabel){ |
| 149 | - //heuristic don't know why | ||
| 150 | - _yLabelHeight = 2; | ||
| 151 | _chartCavanHeight = self.frame.size.height - 2*_yLabelHeight; | 152 | _chartCavanHeight = self.frame.size.height - 2*_yLabelHeight; |
| 152 | _chartCavanWidth = self.frame.size.width; | 153 | _chartCavanWidth = self.frame.size.width; |
| 153 | - _yLabelHeight = 5.0; | ||
| 154 | _chartMargin = 0.0; | 154 | _chartMargin = 0.0; |
| 155 | _xLabelWidth = (_chartCavanWidth / ([_xLabels count] -1)); | 155 | _xLabelWidth = (_chartCavanWidth / ([_xLabels count] -1)); |
| 156 | - xPosition = 0; | ||
| 157 | } | 156 | } |
| 158 | 157 | ||
| 159 | NSMutableArray * linePointsArray = [[NSMutableArray alloc] init]; | 158 | NSMutableArray * linePointsArray = [[NSMutableArray alloc] init]; |
| @@ -161,17 +160,16 @@ | @@ -161,17 +160,16 @@ | ||
| 161 | [progressline setLineCapStyle:kCGLineCapRound]; | 160 | [progressline setLineCapStyle:kCGLineCapRound]; |
| 162 | [progressline setLineJoinStyle:kCGLineJoinRound]; | 161 | [progressline setLineJoinStyle:kCGLineJoinRound]; |
| 163 | 162 | ||
| 164 | - PNLineChartDataItem *dataItem; | 163 | + |
| 165 | 164 | ||
| 166 | 165 | ||
| 167 | for (NSUInteger i = 0; i < chartData.itemCount; i++) { | 166 | for (NSUInteger i = 0; i < chartData.itemCount; i++) { |
| 167 | + | ||
| 168 | + yValue = chartData.getData(i).y; | ||
| 168 | 169 | ||
| 169 | - dataItem = chartData.getData(i); | 170 | + innerGrade = (yValue - _yValueMin) / ( _yValueMax - _yValueMin); |
| 170 | - float value = dataItem.y; | ||
| 171 | - | ||
| 172 | - CGFloat innerGrade = (value - _yValueMin) / ( _yValueMax - _yValueMin); | ||
| 173 | 171 | ||
| 174 | - CGPoint point = CGPointMake(2*_chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + ( _yLabelHeight /2 )); | 172 | + point = CGPointMake(2*_chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + ( _yLabelHeight /2 )); |
| 175 | 173 | ||
| 176 | if (i != 0) { | 174 | if (i != 0) { |
| 177 | [progressline addLineToPoint:point]; | 175 | [progressline addLineToPoint:point]; |
| @@ -211,6 +209,7 @@ | @@ -211,6 +209,7 @@ | ||
| 211 | NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count]; | 209 | NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count]; |
| 212 | CGFloat yMax = 0.0f; | 210 | CGFloat yMax = 0.0f; |
| 213 | CGFloat yMin = MAXFLOAT; | 211 | CGFloat yMin = MAXFLOAT; |
| 212 | + CGFloat yValue; | ||
| 214 | // remove all shape layers before adding new ones | 213 | // remove all shape layers before adding new ones |
| 215 | for (CALayer *layer in self.chartLineArray) { | 214 | for (CALayer *layer in self.chartLineArray) { |
| 216 | [layer removeFromSuperlayer]; | 215 | [layer removeFromSuperlayer]; |
| @@ -230,11 +229,10 @@ | @@ -230,11 +229,10 @@ | ||
| 230 | [self.chartLineArray addObject:chartLine]; | 229 | [self.chartLineArray addObject:chartLine]; |
| 231 | 230 | ||
| 232 | for (NSUInteger i = 0; i < chartData.itemCount; i++) { | 231 | for (NSUInteger i = 0; i < chartData.itemCount; i++) { |
| 233 | - PNLineChartDataItem *dataItem = chartData.getData(i); | 232 | + yValue = chartData.getData(i).y; |
| 234 | - CGFloat yValue = dataItem.y; | ||
| 235 | [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]]; | 233 | [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]]; |
| 236 | - yMax = fmaxf(yMax, dataItem.y); | 234 | + yMax = fmaxf(yMax, yValue); |
| 237 | - yMin = fminf(yMin, dataItem.y); | 235 | + yMin = fminf(yMin, yValue); |
| 238 | } | 236 | } |
| 239 | } | 237 | } |
| 240 | 238 | ||
| @@ -245,7 +243,6 @@ | @@ -245,7 +243,6 @@ | ||
| 245 | if (yMin < 0){ | 243 | if (yMin < 0){ |
| 246 | yMin = 0.0f; | 244 | yMin = 0.0f; |
| 247 | } | 245 | } |
| 248 | - NSLog(@"%f",yMin); | ||
| 249 | 246 | ||
| 250 | _yValueMin = yMin; | 247 | _yValueMin = yMin; |
| 251 | _yValueMax = yMax; | 248 | _yValueMax = yMax; |
| @@ -275,10 +272,7 @@ | @@ -275,10 +272,7 @@ | ||
| 275 | _yLabelHeight = [[[[PNChartLabel alloc] init] font] pointSize]; | 272 | _yLabelHeight = [[[[PNChartLabel alloc] init] font] pointSize]; |
| 276 | 273 | ||
| 277 | _chartMargin = 30; | 274 | _chartMargin = 30; |
| 278 | - | 275 | + |
| 279 | - _xLabelHeight = _chartMargin; | ||
| 280 | - | ||
| 281 | - | ||
| 282 | _chartCavanWidth = self.frame.size.width - _chartMargin *2; | 276 | _chartCavanWidth = self.frame.size.width - _chartMargin *2; |
| 283 | _chartCavanHeight = self.frame.size.height - _chartMargin * 2; | 277 | _chartCavanHeight = self.frame.size.height - _chartMargin * 2; |
| 284 | } | 278 | } |
-
Please register or login to post a comment