Merge pull request #37 from OlegAnghelov/bar-chart-reload
Removing existent subviews before adding new ones in PNBarChart
Showing
1 changed file
with
37 additions
and
20 deletions
| @@ -11,7 +11,11 @@ | @@ -11,7 +11,11 @@ | ||
| 11 | #import "PNChartLabel.h" | 11 | #import "PNChartLabel.h" |
| 12 | #import "PNBar.h" | 12 | #import "PNBar.h" |
| 13 | 13 | ||
| 14 | -@interface PNBarChart() | 14 | +@interface PNBarChart() { |
| 15 | + NSMutableArray* _bars; | ||
| 16 | + NSMutableArray* _labels; | ||
| 17 | +} | ||
| 18 | + | ||
| 15 | - (UIColor *)barColorAtIndex:(NSUInteger)index; | 19 | - (UIColor *)barColorAtIndex:(NSUInteger)index; |
| 16 | @end | 20 | @end |
| 17 | 21 | ||
| @@ -26,8 +30,10 @@ | @@ -26,8 +30,10 @@ | ||
| 26 | self.clipsToBounds = YES; | 30 | self.clipsToBounds = YES; |
| 27 | _showLabel = YES; | 31 | _showLabel = YES; |
| 28 | _barBackgroundColor = PNLightGrey; | 32 | _barBackgroundColor = PNLightGrey; |
| 33 | + _labels = [NSMutableArray array]; | ||
| 34 | + _bars = [NSMutableArray array]; | ||
| 29 | } | 35 | } |
| 30 | - | 36 | + |
| 31 | return self; | 37 | return self; |
| 32 | } | 38 | } |
| 33 | 39 | ||
| @@ -35,7 +41,7 @@ | @@ -35,7 +41,7 @@ | ||
| 35 | { | 41 | { |
| 36 | _yValues = yValues; | 42 | _yValues = yValues; |
| 37 | [self setYLabels:yValues]; | 43 | [self setYLabels:yValues]; |
| 38 | - | 44 | + |
| 39 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count]; | 45 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count]; |
| 40 | } | 46 | } |
| 41 | 47 | ||
| @@ -47,36 +53,38 @@ | @@ -47,36 +53,38 @@ | ||
| 47 | if (value > max) { | 53 | if (value > max) { |
| 48 | max = value; | 54 | max = value; |
| 49 | } | 55 | } |
| 50 | - | 56 | + |
| 51 | } | 57 | } |
| 52 | - | 58 | + |
| 53 | //Min value for Y label | 59 | //Min value for Y label |
| 54 | if (max < 5) { | 60 | if (max < 5) { |
| 55 | max = 5; | 61 | max = 5; |
| 56 | } | 62 | } |
| 57 | - | 63 | + |
| 58 | _yValueMax = (int)max; | 64 | _yValueMax = (int)max; |
| 59 | - | 65 | + |
| 60 | - | 66 | + |
| 61 | } | 67 | } |
| 62 | 68 | ||
| 63 | -(void)setXLabels:(NSArray *)xLabels | 69 | -(void)setXLabels:(NSArray *)xLabels |
| 64 | { | 70 | { |
| 71 | + [self viewCleanupForCollection:_labels]; | ||
| 65 | _xLabels = xLabels; | 72 | _xLabels = xLabels; |
| 66 | - | 73 | + |
| 67 | if (_showLabel) { | 74 | if (_showLabel) { |
| 68 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count]; | 75 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count]; |
| 69 | - | 76 | + |
| 70 | for(int index = 0; index < xLabels.count; index++) | 77 | for(int index = 0; index < xLabels.count; index++) |
| 71 | { | 78 | { |
| 72 | NSString* labelText = xLabels[index]; | 79 | NSString* labelText = xLabels[index]; |
| 73 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)]; | 80 | PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)]; |
| 74 | [label setTextAlignment:NSTextAlignmentCenter]; | 81 | [label setTextAlignment:NSTextAlignmentCenter]; |
| 75 | label.text = labelText; | 82 | label.text = labelText; |
| 83 | + [_labels addObject:label]; | ||
| 76 | [self addSubview:label]; | 84 | [self addSubview:label]; |
| 77 | } | 85 | } |
| 78 | } | 86 | } |
| 79 | - | 87 | + |
| 80 | } | 88 | } |
| 81 | 89 | ||
| 82 | -(void)setStrokeColor:(UIColor *)strokeColor | 90 | -(void)setStrokeColor:(UIColor *)strokeColor |
| @@ -86,13 +94,13 @@ | @@ -86,13 +94,13 @@ | ||
| 86 | 94 | ||
| 87 | -(void)strokeChart | 95 | -(void)strokeChart |
| 88 | { | 96 | { |
| 89 | - | 97 | + [self viewCleanupForCollection:_bars]; |
| 90 | CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0; | 98 | CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0; |
| 91 | NSInteger index = 0; | 99 | NSInteger index = 0; |
| 92 | - | 100 | + |
| 93 | for (NSString * valueString in _yValues) { | 101 | for (NSString * valueString in _yValues) { |
| 94 | float value = [valueString floatValue]; | 102 | float value = [valueString floatValue]; |
| 95 | - | 103 | + |
| 96 | float grade = (float)value / (float)_yValueMax; | 104 | float grade = (float)value / (float)_yValueMax; |
| 97 | PNBar * bar; | 105 | PNBar * bar; |
| 98 | if (_showLabel) { | 106 | if (_showLabel) { |
| @@ -100,15 +108,24 @@ | @@ -100,15 +108,24 @@ | ||
| 100 | }else{ | 108 | }else{ |
| 101 | bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)]; | 109 | bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)]; |
| 102 | } | 110 | } |
| 103 | - bar.backgroundColor = _barBackgroundColor; | 111 | + bar.backgroundColor = _barBackgroundColor; |
| 104 | - bar.barColor = [self barColorAtIndex:index]; | 112 | + bar.barColor = [self barColorAtIndex:index]; |
| 105 | - bar.grade = grade; | 113 | + bar.grade = grade; |
| 106 | - [self addSubview:bar]; | 114 | + [_bars addObject:bar]; |
| 107 | - | 115 | + [self addSubview:bar]; |
| 116 | + | ||
| 108 | index += 1; | 117 | index += 1; |
| 109 | } | 118 | } |
| 110 | } | 119 | } |
| 111 | 120 | ||
| 121 | +- (void)viewCleanupForCollection:(NSMutableArray*)array | ||
| 122 | +{ | ||
| 123 | + if (array.count) { | ||
| 124 | + [array makeObjectsPerformSelector:@selector(removeFromSuperview)]; | ||
| 125 | + [array removeAllObjects]; | ||
| 126 | + } | ||
| 127 | +} | ||
| 128 | + | ||
| 112 | #pragma mark - Class extension methods | 129 | #pragma mark - Class extension methods |
| 113 | 130 | ||
| 114 | - (UIColor *)barColorAtIndex:(NSUInteger)index | 131 | - (UIColor *)barColorAtIndex:(NSUInteger)index |
| @@ -120,4 +137,4 @@ | @@ -120,4 +137,4 @@ | ||
| 120 | } | 137 | } |
| 121 | } | 138 | } |
| 122 | 139 | ||
| 123 | -@end | 140 | +@end |
-
Please register or login to post a comment