Dissociate the xLabelWidth from the lines drawing
* For graphic with a lot of data, we can't display all xLabels. Now it's possible to display a fewLabels with a lot of value. * Add method to specify the width xLabel.
Showing
2 changed files
with
75 additions
and
64 deletions
| @@ -57,7 +57,6 @@ | @@ -57,7 +57,6 @@ | ||
| 57 | 57 | ||
| 58 | @property (nonatomic) BOOL showLabel; | 58 | @property (nonatomic) BOOL showLabel; |
| 59 | 59 | ||
| 60 | - | ||
| 61 | /** | 60 | /** |
| 62 | * show CoordinateAxis ornot, Default is not | 61 | * show CoordinateAxis ornot, Default is not |
| 63 | */ | 62 | */ |
| @@ -73,4 +72,6 @@ | @@ -73,4 +72,6 @@ | ||
| 73 | */ | 72 | */ |
| 74 | @property (nonatomic, strong) NSString *yLabelFormat; | 73 | @property (nonatomic, strong) NSString *yLabelFormat; |
| 75 | 74 | ||
| 75 | +- (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width; | ||
| 76 | + | ||
| 76 | @end | 77 | @end |
| @@ -12,10 +12,9 @@ | @@ -12,10 +12,9 @@ | ||
| 12 | #import "PNLineChartData.h" | 12 | #import "PNLineChartData.h" |
| 13 | #import "PNLineChartDataItem.h" | 13 | #import "PNLineChartDataItem.h" |
| 14 | 14 | ||
| 15 | - | 15 | +// ------------------------------------------------------------------------------------------------ |
| 16 | -//------------------------------------------------------------------------------------------------ | ||
| 17 | // private interface declaration | 16 | // private interface declaration |
| 18 | -//------------------------------------------------------------------------------------------------ | 17 | +// ------------------------------------------------------------------------------------------------ |
| 19 | @interface PNLineChart () | 18 | @interface PNLineChart () |
| 20 | 19 | ||
| 21 | @property (nonatomic) NSMutableArray *chartLineArray; // Array[CAShapeLayer] | 20 | @property (nonatomic) NSMutableArray *chartLineArray; // Array[CAShapeLayer] |
| @@ -28,10 +27,9 @@ | @@ -28,10 +27,9 @@ | ||
| 28 | 27 | ||
| 29 | @end | 28 | @end |
| 30 | 29 | ||
| 31 | - | 30 | +// ------------------------------------------------------------------------------------------------ |
| 32 | -//------------------------------------------------------------------------------------------------ | ||
| 33 | // public interface implementation | 31 | // public interface implementation |
| 34 | -//------------------------------------------------------------------------------------------------ | 32 | +// ------------------------------------------------------------------------------------------------ |
| 35 | @implementation PNLineChart | 33 | @implementation PNLineChart |
| 36 | 34 | ||
| 37 | #pragma mark initialization | 35 | #pragma mark initialization |
| @@ -47,7 +45,6 @@ | @@ -47,7 +45,6 @@ | ||
| 47 | return self; | 45 | return self; |
| 48 | } | 46 | } |
| 49 | 47 | ||
| 50 | - | ||
| 51 | - (id)initWithFrame:(CGRect)frame | 48 | - (id)initWithFrame:(CGRect)frame |
| 52 | { | 49 | { |
| 53 | self = [super initWithFrame:frame]; | 50 | self = [super initWithFrame:frame]; |
| @@ -59,14 +56,13 @@ | @@ -59,14 +56,13 @@ | ||
| 59 | return self; | 56 | return self; |
| 60 | } | 57 | } |
| 61 | 58 | ||
| 62 | - | ||
| 63 | #pragma mark instance methods | 59 | #pragma mark instance methods |
| 64 | 60 | ||
| 65 | - (void)setYLabels:(NSArray *)yLabels | 61 | - (void)setYLabels:(NSArray *)yLabels |
| 66 | { | 62 | { |
| 67 | CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum; | 63 | CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum; |
| 68 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; | 64 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; |
| 69 | - NSString *yLabelFormat = self.yLabelFormat ?: @"%1.f"; | 65 | + NSString *yLabelFormat = self.yLabelFormat ? : @"%1.f"; |
| 70 | 66 | ||
| 71 | if (yStep == 0.0) { | 67 | if (yStep == 0.0) { |
| 72 | PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight, _chartMargin, _yLabelHeight)]; | 68 | PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight, _chartMargin, _yLabelHeight)]; |
| @@ -74,7 +70,7 @@ | @@ -74,7 +70,7 @@ | ||
| 74 | [self setCustomStyleForYLabel:minLabel]; | 70 | [self setCustomStyleForYLabel:minLabel]; |
| 75 | [self addSubview:minLabel]; | 71 | [self addSubview:minLabel]; |
| 76 | 72 | ||
| 77 | - PNChartLabel *midLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight/2, _chartMargin, _yLabelHeight)]; | 73 | + PNChartLabel *midLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight / 2, _chartMargin, _yLabelHeight)]; |
| 78 | midLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax]; | 74 | midLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax]; |
| 79 | [self setCustomStyleForYLabel:midLabel]; | 75 | [self setCustomStyleForYLabel:midLabel]; |
| 80 | [self addSubview:midLabel]; | 76 | [self addSubview:midLabel]; |
| @@ -88,7 +84,8 @@ | @@ -88,7 +84,8 @@ | ||
| 88 | NSInteger index = 0; | 84 | NSInteger index = 0; |
| 89 | NSInteger num = _yLabelNum + 1; | 85 | NSInteger num = _yLabelNum + 1; |
| 90 | 86 | ||
| 91 | - while (num > 0) { | 87 | + while (num > 0) |
| 88 | + { | ||
| 92 | PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)]; | 89 | PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)]; |
| 93 | [label setTextAlignment:NSTextAlignmentRight]; | 90 | [label setTextAlignment:NSTextAlignmentRight]; |
| 94 | label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)]; | 91 | label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)]; |
| @@ -102,62 +99,77 @@ | @@ -102,62 +99,77 @@ | ||
| 102 | 99 | ||
| 103 | - (void)setXLabels:(NSArray *)xLabels | 100 | - (void)setXLabels:(NSArray *)xLabels |
| 104 | { | 101 | { |
| 102 | + CGFloat xLabelWidth; | ||
| 103 | + | ||
| 104 | + if (_showLabel) { | ||
| 105 | + xLabelWidth = _chartCavanWidth / [xLabels count]; | ||
| 106 | + } else { | ||
| 107 | + xLabelWidth = (self.frame.size.width) / [xLabels count]; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + return [self setXLabels:xLabels withWidth:xLabelWidth]; | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +- (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width | ||
| 114 | +{ | ||
| 105 | _xLabels = xLabels; | 115 | _xLabels = xLabels; |
| 116 | + _xLabelWidth = width; | ||
| 117 | + | ||
| 106 | NSString *labelText; | 118 | NSString *labelText; |
| 107 | 119 | ||
| 108 | if (_showLabel) { | 120 | if (_showLabel) { |
| 109 | - _xLabelWidth = _chartCavanWidth / [xLabels count]; | ||
| 110 | - | ||
| 111 | for (int index = 0; index < xLabels.count; index++) { | 121 | for (int index = 0; index < xLabels.count; index++) { |
| 112 | labelText = xLabels[index]; | 122 | labelText = xLabels[index]; |
| 113 | - PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)]; | 123 | + |
| 124 | + NSInteger x = 2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2); | ||
| 125 | + NSInteger y = _chartMargin + _chartCavanHeight; | ||
| 126 | + | ||
| 127 | + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(x, y, _xLabelWidth, _chartMargin)]; | ||
| 114 | [label setTextAlignment:NSTextAlignmentCenter]; | 128 | [label setTextAlignment:NSTextAlignmentCenter]; |
| 115 | label.text = labelText; | 129 | label.text = labelText; |
| 116 | [self setCustomStyleForXLabel:label]; | 130 | [self setCustomStyleForXLabel:label]; |
| 117 | [self addSubview:label]; | 131 | [self addSubview:label]; |
| 118 | } | 132 | } |
| 119 | } | 133 | } |
| 120 | - else { | ||
| 121 | - _xLabelWidth = (self.frame.size.width) / [xLabels count]; | ||
| 122 | - } | ||
| 123 | } | 134 | } |
| 124 | 135 | ||
| 125 | -- (void)setCustomStyleForXLabel:(UILabel *)label { | 136 | +- (void)setCustomStyleForXLabel:(UILabel *)label |
| 137 | +{ | ||
| 126 | if (_xLabelFont) { | 138 | if (_xLabelFont) { |
| 127 | label.font = _xLabelFont; | 139 | label.font = _xLabelFont; |
| 128 | } | 140 | } |
| 141 | + | ||
| 129 | if (_xLabelColor) { | 142 | if (_xLabelColor) { |
| 130 | label.textColor = _xLabelColor; | 143 | label.textColor = _xLabelColor; |
| 131 | } | 144 | } |
| 132 | } | 145 | } |
| 133 | 146 | ||
| 134 | -- (void)setCustomStyleForYLabel:(UILabel *)label { | 147 | +- (void)setCustomStyleForYLabel:(UILabel *)label |
| 148 | +{ | ||
| 135 | if (_yLabelFont) { | 149 | if (_yLabelFont) { |
| 136 | label.font = _yLabelFont; | 150 | label.font = _yLabelFont; |
| 137 | } | 151 | } |
| 152 | + | ||
| 138 | if (_yLabelColor) { | 153 | if (_yLabelColor) { |
| 139 | label.textColor = _yLabelColor; | 154 | label.textColor = _yLabelColor; |
| 140 | } | 155 | } |
| 141 | } | 156 | } |
| 142 | 157 | ||
| 143 | - | ||
| 144 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | 158 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
| 145 | { | 159 | { |
| 146 | [self touchPoint:touches withEvent:event]; | 160 | [self touchPoint:touches withEvent:event]; |
| 147 | [self touchKeyPoint:touches withEvent:event]; | 161 | [self touchKeyPoint:touches withEvent:event]; |
| 148 | } | 162 | } |
| 149 | 163 | ||
| 150 | - | ||
| 151 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | 164 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event |
| 152 | { | 165 | { |
| 153 | [self touchPoint:touches withEvent:event]; | 166 | [self touchPoint:touches withEvent:event]; |
| 154 | [self touchKeyPoint:touches withEvent:event]; | 167 | [self touchKeyPoint:touches withEvent:event]; |
| 155 | } | 168 | } |
| 156 | 169 | ||
| 157 | - | ||
| 158 | - (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event | 170 | - (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event |
| 159 | { | 171 | { |
| 160 | - //Get the point user touched | 172 | + // Get the point user touched |
| 161 | UITouch *touch = [touches anyObject]; | 173 | UITouch *touch = [touches anyObject]; |
| 162 | CGPoint touchPoint = [touch locationInView:self]; | 174 | CGPoint touchPoint = [touch locationInView:self]; |
| 163 | 175 | ||
| @@ -179,6 +191,7 @@ | @@ -179,6 +191,7 @@ | ||
| 179 | 191 | ||
| 180 | if (pointContainsPath) { | 192 | if (pointContainsPath) { |
| 181 | [_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]]; | 193 | [_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]]; |
| 194 | + | ||
| 182 | return; | 195 | return; |
| 183 | } | 196 | } |
| 184 | } | 197 | } |
| @@ -187,10 +200,9 @@ | @@ -187,10 +200,9 @@ | ||
| 187 | } | 200 | } |
| 188 | } | 201 | } |
| 189 | 202 | ||
| 190 | - | ||
| 191 | - (void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event | 203 | - (void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event |
| 192 | { | 204 | { |
| 193 | - //Get the point user touched | 205 | + // Get the point user touched |
| 194 | UITouch *touch = [touches anyObject]; | 206 | UITouch *touch = [touches anyObject]; |
| 195 | CGPoint touchPoint = [touch locationInView:self]; | 207 | CGPoint touchPoint = [touch locationInView:self]; |
| 196 | 208 | ||
| @@ -210,19 +222,19 @@ | @@ -210,19 +222,19 @@ | ||
| 210 | [_delegate userClickedOnLineKeyPoint:touchPoint | 222 | [_delegate userClickedOnLineKeyPoint:touchPoint |
| 211 | lineIndex:p | 223 | lineIndex:p |
| 212 | andPointIndex:(distance == distanceToP2 ? i + 1 : i)]; | 224 | andPointIndex:(distance == distanceToP2 ? i + 1 : i)]; |
| 225 | + | ||
| 213 | return; | 226 | return; |
| 214 | } | 227 | } |
| 215 | } | 228 | } |
| 216 | } | 229 | } |
| 217 | } | 230 | } |
| 218 | 231 | ||
| 219 | - | ||
| 220 | - (void)strokeChart | 232 | - (void)strokeChart |
| 221 | { | 233 | { |
| 222 | _chartPath = [[NSMutableArray alloc] init]; | 234 | _chartPath = [[NSMutableArray alloc] init]; |
| 223 | _pointPath = [[NSMutableArray alloc] init]; | 235 | _pointPath = [[NSMutableArray alloc] init]; |
| 224 | 236 | ||
| 225 | - //Draw each line | 237 | + // Draw each line |
| 226 | for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { | 238 | for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { |
| 227 | PNLineChartData *chartData = self.chartData[lineIndex]; | 239 | PNLineChartData *chartData = self.chartData[lineIndex]; |
| 228 | CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex]; | 240 | CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex]; |
| @@ -267,26 +279,28 @@ | @@ -267,26 +279,28 @@ | ||
| 267 | innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin); | 279 | innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin); |
| 268 | } | 280 | } |
| 269 | 281 | ||
| 270 | - int x = 2 * _chartMargin + (i * _xLabelWidth); | 282 | + CGFloat offSetX = (_chartCavanWidth - _xLabelWidth) / (chartData.itemCount - 1); |
| 283 | + | ||
| 284 | + int x = 2 * _chartMargin + (i * offSetX); | ||
| 271 | int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2); | 285 | int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2); |
| 272 | 286 | ||
| 273 | // cycle style point | 287 | // cycle style point |
| 274 | if (chartData.inflexionPointStyle == PNLineChartPointStyleCycle) { | 288 | if (chartData.inflexionPointStyle == PNLineChartPointStyleCycle) { |
| 275 | 289 | ||
| 276 | - CGRect circleRect = CGRectMake(x-inflexionWidth/2, y-inflexionWidth/2, inflexionWidth,inflexionWidth); | 290 | + CGRect circleRect = CGRectMake(x - inflexionWidth / 2, y - inflexionWidth / 2, inflexionWidth, inflexionWidth); |
| 277 | CGPoint circleCenter = CGPointMake(circleRect.origin.x + (circleRect.size.width / 2), circleRect.origin.y + (circleRect.size.height / 2)); | 291 | CGPoint circleCenter = CGPointMake(circleRect.origin.x + (circleRect.size.width / 2), circleRect.origin.y + (circleRect.size.height / 2)); |
| 278 | 292 | ||
| 279 | - [pointPath moveToPoint:CGPointMake(circleCenter.x + (inflexionWidth/2), circleCenter.y)]; | 293 | + [pointPath moveToPoint:CGPointMake(circleCenter.x + (inflexionWidth / 2), circleCenter.y)]; |
| 280 | - [pointPath addArcWithCenter:circleCenter radius:inflexionWidth/2 startAngle:0 endAngle:2*M_PI clockwise:YES]; | 294 | + [pointPath addArcWithCenter:circleCenter radius:inflexionWidth / 2 startAngle:0 endAngle:2 * M_PI clockwise:YES]; |
| 281 | 295 | ||
| 282 | if ( i != 0 ) { | 296 | if ( i != 0 ) { |
| 283 | 297 | ||
| 284 | // calculate the point for line | 298 | // calculate the point for line |
| 285 | - float distance = sqrt( pow(x-last_x, 2) + pow(y-last_y,2) ); | 299 | + float distance = sqrt(pow(x - last_x, 2) + pow(y - last_y, 2) ); |
| 286 | - float last_x1 = last_x + (inflexionWidth/2) / distance * (x-last_x); | 300 | + float last_x1 = last_x + (inflexionWidth / 2) / distance * (x - last_x); |
| 287 | - float last_y1 = last_y + (inflexionWidth/2) / distance * (y-last_y); | 301 | + float last_y1 = last_y + (inflexionWidth / 2) / distance * (y - last_y); |
| 288 | - float x1 = x - (inflexionWidth/2) / distance * (x-last_x); | 302 | + float x1 = x - (inflexionWidth / 2) / distance * (x - last_x); |
| 289 | - float y1 = y - (inflexionWidth/2) / distance * (y-last_y); | 303 | + float y1 = y - (inflexionWidth / 2) / distance * (y - last_y); |
| 290 | 304 | ||
| 291 | [progressline moveToPoint:CGPointMake(last_x1, last_y1)]; | 305 | [progressline moveToPoint:CGPointMake(last_x1, last_y1)]; |
| 292 | [progressline addLineToPoint:CGPointMake(x1, y1)]; | 306 | [progressline addLineToPoint:CGPointMake(x1, y1)]; |
| @@ -298,23 +312,23 @@ | @@ -298,23 +312,23 @@ | ||
| 298 | // Square style point | 312 | // Square style point |
| 299 | else if (chartData.inflexionPointStyle == PNLineChartPointStyleSquare) { | 313 | else if (chartData.inflexionPointStyle == PNLineChartPointStyleSquare) { |
| 300 | 314 | ||
| 301 | - CGRect squareRect = CGRectMake(x-inflexionWidth/2, y-inflexionWidth/2, inflexionWidth,inflexionWidth); | 315 | + CGRect squareRect = CGRectMake(x - inflexionWidth / 2, y - inflexionWidth / 2, inflexionWidth, inflexionWidth); |
| 302 | CGPoint squareCenter = CGPointMake(squareRect.origin.x + (squareRect.size.width / 2), squareRect.origin.y + (squareRect.size.height / 2)); | 316 | CGPoint squareCenter = CGPointMake(squareRect.origin.x + (squareRect.size.width / 2), squareRect.origin.y + (squareRect.size.height / 2)); |
| 303 | 317 | ||
| 304 | - [pointPath moveToPoint:CGPointMake(squareCenter.x - (inflexionWidth/2), squareCenter.y - (inflexionWidth/2))]; | 318 | + [pointPath moveToPoint:CGPointMake(squareCenter.x - (inflexionWidth / 2), squareCenter.y - (inflexionWidth / 2))]; |
| 305 | - [pointPath addLineToPoint:CGPointMake(squareCenter.x + (inflexionWidth/2), squareCenter.y - (inflexionWidth/2))]; | 319 | + [pointPath addLineToPoint:CGPointMake(squareCenter.x + (inflexionWidth / 2), squareCenter.y - (inflexionWidth / 2))]; |
| 306 | - [pointPath addLineToPoint:CGPointMake(squareCenter.x + (inflexionWidth/2), squareCenter.y + (inflexionWidth/2))]; | 320 | + [pointPath addLineToPoint:CGPointMake(squareCenter.x + (inflexionWidth / 2), squareCenter.y + (inflexionWidth / 2))]; |
| 307 | - [pointPath addLineToPoint:CGPointMake(squareCenter.x - (inflexionWidth/2), squareCenter.y + (inflexionWidth/2))]; | 321 | + [pointPath addLineToPoint:CGPointMake(squareCenter.x - (inflexionWidth / 2), squareCenter.y + (inflexionWidth / 2))]; |
| 308 | [pointPath closePath]; | 322 | [pointPath closePath]; |
| 309 | 323 | ||
| 310 | if ( i != 0 ) { | 324 | if ( i != 0 ) { |
| 311 | 325 | ||
| 312 | // calculate the point for line | 326 | // calculate the point for line |
| 313 | - float distance = sqrt( pow(x-last_x, 2) + pow(y-last_y,2) ); | 327 | + float distance = sqrt(pow(x - last_x, 2) + pow(y - last_y, 2) ); |
| 314 | - float last_x1 = last_x + (inflexionWidth/2); | 328 | + float last_x1 = last_x + (inflexionWidth / 2); |
| 315 | - float last_y1 = last_y + (inflexionWidth/2) / distance * (y-last_y); | 329 | + float last_y1 = last_y + (inflexionWidth / 2) / distance * (y - last_y); |
| 316 | - float x1 = x - (inflexionWidth/2); | 330 | + float x1 = x - (inflexionWidth / 2); |
| 317 | - float y1 = y - (inflexionWidth/2) / distance * (y-last_y); | 331 | + float y1 = y - (inflexionWidth / 2) / distance * (y - last_y); |
| 318 | 332 | ||
| 319 | [progressline moveToPoint:CGPointMake(last_x1, last_y1)]; | 333 | [progressline moveToPoint:CGPointMake(last_x1, last_y1)]; |
| 320 | [progressline addLineToPoint:CGPointMake(x1, y1)]; | 334 | [progressline addLineToPoint:CGPointMake(x1, y1)]; |
| @@ -348,8 +362,7 @@ | @@ -348,8 +362,7 @@ | ||
| 348 | // setup the color of the chart line | 362 | // setup the color of the chart line |
| 349 | if (chartData.color) { | 363 | if (chartData.color) { |
| 350 | chartLine.strokeColor = [chartData.color CGColor]; | 364 | chartLine.strokeColor = [chartData.color CGColor]; |
| 351 | - } | 365 | + } else { |
| 352 | - else { | ||
| 353 | chartLine.strokeColor = [PNGreen CGColor]; | 366 | chartLine.strokeColor = [PNGreen CGColor]; |
| 354 | pointLayer.strokeColor = [PNGreen CGColor]; | 367 | pointLayer.strokeColor = [PNGreen CGColor]; |
| 355 | } | 368 | } |
| @@ -359,7 +372,6 @@ | @@ -359,7 +372,6 @@ | ||
| 359 | chartLine.path = progressline.CGPath; | 372 | chartLine.path = progressline.CGPath; |
| 360 | pointLayer.path = pointPath.CGPath; | 373 | pointLayer.path = pointPath.CGPath; |
| 361 | 374 | ||
| 362 | - | ||
| 363 | [CATransaction begin]; | 375 | [CATransaction begin]; |
| 364 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | 376 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; |
| 365 | pathAnimation.duration = 1.0; | 377 | pathAnimation.duration = 1.0; |
| @@ -376,7 +388,7 @@ | @@ -376,7 +388,7 @@ | ||
| 376 | } | 388 | } |
| 377 | 389 | ||
| 378 | [CATransaction setCompletionBlock:^{ | 390 | [CATransaction setCompletionBlock:^{ |
| 379 | - //pointLayer.strokeEnd = 1.0f; // stroken point when animation end | 391 | + // pointLayer.strokeEnd = 1.0f; // stroken point when animation end |
| 380 | }]; | 392 | }]; |
| 381 | [CATransaction commit]; | 393 | [CATransaction commit]; |
| 382 | 394 | ||
| @@ -468,7 +480,7 @@ | @@ -468,7 +480,7 @@ | ||
| 468 | CGContextSetLineWidth(ctx, self.axisWidth); | 480 | CGContextSetLineWidth(ctx, self.axisWidth); |
| 469 | CGContextSetStrokeColorWithColor(ctx, [self.axisColor CGColor]); | 481 | CGContextSetStrokeColorWithColor(ctx, [self.axisColor CGColor]); |
| 470 | 482 | ||
| 471 | - CGFloat xAxisWidth = CGRectGetWidth(rect) - _chartMargin/2; | 483 | + CGFloat xAxisWidth = CGRectGetWidth(rect) - _chartMargin / 2; |
| 472 | CGFloat yAxisHeight = _chartMargin + _chartCavanHeight; | 484 | CGFloat yAxisHeight = _chartMargin + _chartCavanHeight; |
| 473 | 485 | ||
| 474 | // draw coordinate axis | 486 | // draw coordinate axis |
| @@ -503,7 +515,7 @@ | @@ -503,7 +515,7 @@ | ||
| 503 | // draw y axis separator | 515 | // draw y axis separator |
| 504 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; | 516 | CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; |
| 505 | for (NSUInteger i = 0; i < [self.xLabels count]; i++) { | 517 | for (NSUInteger i = 0; i < [self.xLabels count]; i++) { |
| 506 | - point = CGPointMake(_chartMargin + yAsixOffset, (_chartCavanHeight - i * yStepHeight + _yLabelHeight/2)); | 518 | + point = CGPointMake(_chartMargin + yAsixOffset, (_chartCavanHeight - i * yStepHeight + _yLabelHeight / 2)); |
| 507 | CGContextMoveToPoint(ctx, point.x, point.y); | 519 | CGContextMoveToPoint(ctx, point.x, point.y); |
| 508 | CGContextAddLineToPoint(ctx, point.x + 2, point.y); | 520 | CGContextAddLineToPoint(ctx, point.x + 2, point.y); |
| 509 | CGContextStrokePath(ctx); | 521 | CGContextStrokePath(ctx); |
| @@ -511,6 +523,7 @@ | @@ -511,6 +523,7 @@ | ||
| 511 | } | 523 | } |
| 512 | 524 | ||
| 513 | UIFont *font = [UIFont systemFontOfSize:11]; | 525 | UIFont *font = [UIFont systemFontOfSize:11]; |
| 526 | + | ||
| 514 | // draw y unit | 527 | // draw y unit |
| 515 | if ([self.yUnit length]) { | 528 | if ([self.yUnit length]) { |
| 516 | CGFloat height = [PNLineChart heightOfString:self.yUnit withWidth:30.f font:font]; | 529 | CGFloat height = [PNLineChart heightOfString:self.yUnit withWidth:30.f font:font]; |
| @@ -521,7 +534,7 @@ | @@ -521,7 +534,7 @@ | ||
| 521 | // draw x unit | 534 | // draw x unit |
| 522 | if ([self.xUnit length]) { | 535 | if ([self.xUnit length]) { |
| 523 | CGFloat height = [PNLineChart heightOfString:self.xUnit withWidth:30.f font:font]; | 536 | CGFloat height = [PNLineChart heightOfString:self.xUnit withWidth:30.f font:font]; |
| 524 | - CGRect drawRect = CGRectMake(CGRectGetWidth(rect) - _chartMargin + 5, _chartMargin + _chartCavanHeight - height/2, 25.f, height); | 537 | + CGRect drawRect = CGRectMake(CGRectGetWidth(rect) - _chartMargin + 5, _chartMargin + _chartCavanHeight - height / 2, 25.f, height); |
| 525 | [self drawTextInContext:ctx text:self.xUnit inRect:drawRect font:font]; | 538 | [self drawTextInContext:ctx text:self.xUnit inRect:drawRect font:font]; |
| 526 | } | 539 | } |
| 527 | } | 540 | } |
| @@ -561,16 +574,14 @@ | @@ -561,16 +574,14 @@ | ||
| 561 | { | 574 | { |
| 562 | NSInteger ch; | 575 | NSInteger ch; |
| 563 | CGSize size = CGSizeMake(width, MAXFLOAT); | 576 | CGSize size = CGSizeMake(width, MAXFLOAT); |
| 564 | - if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) | 577 | + |
| 565 | - { | 578 | + if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { |
| 566 | - NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil]; | 579 | + NSDictionary *tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; |
| 567 | - size =[text boundingRectWithSize:size | 580 | + size = [text boundingRectWithSize:size |
| 568 | - options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading | 581 | + options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading |
| 569 | attributes:tdic | 582 | attributes:tdic |
| 570 | context:nil].size; | 583 | context:nil].size; |
| 571 | - } | 584 | + } else { |
| 572 | - else | ||
| 573 | - { | ||
| 574 | #pragma clang diagnostic push | 585 | #pragma clang diagnostic push |
| 575 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | 586 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 576 | size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; | 587 | size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; |
| @@ -589,7 +600,7 @@ | @@ -589,7 +600,7 @@ | ||
| 589 | priceParagraphStyle.alignment = NSTextAlignmentLeft; | 600 | priceParagraphStyle.alignment = NSTextAlignmentLeft; |
| 590 | 601 | ||
| 591 | [text drawInRect:rect | 602 | [text drawInRect:rect |
| 592 | - withAttributes:@{NSParagraphStyleAttributeName:priceParagraphStyle, NSFontAttributeName:font}]; | 603 | + withAttributes:@{ NSParagraphStyleAttributeName:priceParagraphStyle, NSFontAttributeName:font }]; |
| 593 | } else { | 604 | } else { |
| 594 | #pragma clang diagnostic push | 605 | #pragma clang diagnostic push |
| 595 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | 606 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| @@ -601,5 +612,4 @@ | @@ -601,5 +612,4 @@ | ||
| 601 | } | 612 | } |
| 602 | } | 613 | } |
| 603 | 614 | ||
| 604 | - | ||
| 605 | @end | 615 | @end |
-
Please register or login to post a comment