kevinzhow

Update Y labels

@@ -29,6 +29,8 @@ @@ -29,6 +29,8 @@
29 29
30 @property (nonatomic) NSMutableArray *pathPoints; 30 @property (nonatomic) NSMutableArray *pathPoints;
31 @property (nonatomic) NSMutableArray *xChartLabels; 31 @property (nonatomic) NSMutableArray *xChartLabels;
  32 +@property (nonatomic) NSMutableArray *yChartLabels;
  33 +
32 @property (nonatomic) CGFloat xLabelWidth; 34 @property (nonatomic) CGFloat xLabelWidth;
33 @property (nonatomic) UIFont *xLabelFont; 35 @property (nonatomic) UIFont *xLabelFont;
34 @property (nonatomic) UIColor *xLabelColor; 36 @property (nonatomic) UIColor *xLabelColor;
@@ -58,21 +58,32 @@ @@ -58,21 +58,32 @@
58 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; 58 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
59 NSString *yLabelFormat = self.yLabelFormat ? : @"%1.f"; 59 NSString *yLabelFormat = self.yLabelFormat ? : @"%1.f";
60 60
  61 + if (_yChartLabels) {
  62 + for (PNChartLabel * label in _yChartLabels) {
  63 + [label removeFromSuperview];
  64 + }
  65 + }else{
  66 + _yChartLabels = [NSMutableArray new];
  67 + }
  68 +
61 if (yStep == 0.0) { 69 if (yStep == 0.0) {
62 PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)]; 70 PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
63 minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0]; 71 minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0];
64 [self setCustomStyleForYLabel:minLabel]; 72 [self setCustomStyleForYLabel:minLabel];
65 [self addSubview:minLabel]; 73 [self addSubview:minLabel];
  74 + [_yChartLabels addObject:minLabel];
66 75
67 PNChartLabel *midLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)(_chartCavanHeight / 2), (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)]; 76 PNChartLabel *midLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)(_chartCavanHeight / 2), (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
68 midLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax]; 77 midLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax];
69 [self setCustomStyleForYLabel:midLabel]; 78 [self setCustomStyleForYLabel:midLabel];
70 [self addSubview:midLabel]; 79 [self addSubview:midLabel];
  80 + [_yChartLabels addObject:midLabel];
71 81
72 PNChartLabel *maxLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)]; 82 PNChartLabel *maxLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
73 maxLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax * 2]; 83 maxLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax * 2];
74 [self setCustomStyleForYLabel:maxLabel]; 84 [self setCustomStyleForYLabel:maxLabel];
75 [self addSubview:maxLabel]; 85 [self addSubview:maxLabel];
  86 + [_yChartLabels addObject:maxLabel];
76 87
77 } else { 88 } else {
78 NSInteger index = 0; 89 NSInteger index = 0;
@@ -85,6 +96,7 @@ @@ -85,6 +96,7 @@
85 label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)]; 96 label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)];
86 [self setCustomStyleForYLabel:label]; 97 [self setCustomStyleForYLabel:label];
87 [self addSubview:label]; 98 [self addSubview:label];
  99 + [_yChartLabels addObject:label];
88 index += 1; 100 index += 1;
89 num -= 1; 101 num -= 1;
90 } 102 }
@@ -439,10 +451,6 @@ @@ -439,10 +451,6 @@
439 - (void)setChartData:(NSArray *)data 451 - (void)setChartData:(NSArray *)data
440 { 452 {
441 if (data != _chartData) { 453 if (data != _chartData) {
442 - NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count];  
443 - CGFloat yMax = 0.0f;  
444 - CGFloat yMin = MAXFLOAT;  
445 - CGFloat yValue;  
446 454
447 // remove all shape layers before adding new ones 455 // remove all shape layers before adding new ones
448 for (CALayer *layer in self.chartLineArray) { 456 for (CALayer *layer in self.chartLineArray) {
@@ -475,43 +483,60 @@ @@ -475,43 +483,60 @@
475 pointLayer.lineWidth = chartData.lineWidth; 483 pointLayer.lineWidth = chartData.lineWidth;
476 [self.layer addSublayer:pointLayer]; 484 [self.layer addSublayer:pointLayer];
477 [self.chartPointArray addObject:pointLayer]; 485 [self.chartPointArray addObject:pointLayer];
478 -  
479 - for (NSUInteger i = 0; i < chartData.itemCount; i++) {  
480 - yValue = chartData.getData(i).y;  
481 - [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]];  
482 - yMax = fmaxf(yMax, yValue);  
483 - yMin = fminf(yMin, yValue);  
484 - }  
485 - }  
486 -  
487 - // Min value for Y label  
488 - if (yMax < 5) {  
489 - yMax = 5.0f;  
490 - }  
491 -  
492 - if (yMin < 0) {  
493 - yMin = 0.0f;  
494 } 486 }
495 487
496 - _yValueMin = yMin;  
497 - _yValueMax = yMax;  
498 -  
499 _chartData = data; 488 _chartData = data;
500 - 489 +
501 - if (_showLabel) { 490 + [self prepareYLabelsWithData:data];
502 - [self setYLabels:yLabelsArray];  
503 - }  
504 491
505 [self setNeedsDisplay]; 492 [self setNeedsDisplay];
506 } 493 }
507 } 494 }
508 495
  496 +-(void)prepareYLabelsWithData:(NSArray *)data
  497 +{
  498 + CGFloat yMax = 0.0f;
  499 + CGFloat yMin = MAXFLOAT;
  500 + NSMutableArray *yLabelsArray = [NSMutableArray new];
  501 +
  502 + for (PNLineChartData *chartData in data) {
  503 + // create as many chart line layers as there are data-lines
  504 +
  505 + for (NSUInteger i = 0; i < chartData.itemCount; i++) {
  506 + CGFloat yValue = chartData.getData(i).y;
  507 + [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]];
  508 + yMax = fmaxf(yMax, yValue);
  509 + yMin = fminf(yMin, yValue);
  510 + }
  511 + }
  512 +
  513 +
  514 + // Min value for Y label
  515 + if (yMax < 5) {
  516 + yMax = 5.0f;
  517 + }
  518 +
  519 + if (yMin < 0) {
  520 + yMin = 0.0f;
  521 + }
  522 +
  523 + _yValueMin = yMin;
  524 + _yValueMax = yMax + yMax / 10.0;
  525 +
  526 + if (_showLabel) {
  527 + [self setYLabels:yLabelsArray];
  528 + }
  529 +
  530 +}
  531 +
509 #pragma mark - Update Chart Data 532 #pragma mark - Update Chart Data
510 533
511 - (void)updateChartData:(NSArray *)data 534 - (void)updateChartData:(NSArray *)data
512 { 535 {
513 _chartData = data; 536 _chartData = data;
514 537
  538 + [self prepareYLabelsWithData:data];
  539 +
515 [self calculateChartPath:_chartPath andPointsPath:_pointPath andPathKeyPoints:_pathPoints]; 540 [self calculateChartPath:_chartPath andPointsPath:_pointPath andPathKeyPoints:_pathPoints];
516 541
517 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { 542 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {