Merge branch 'master' of github.com:kevinzhow/PNChart
Showing
1 changed file
with
19 additions
and
2 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,6 +30,8 @@ | @@ -26,6 +30,8 @@ | ||
| 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; |
| @@ -62,6 +68,7 @@ | @@ -62,6 +68,7 @@ | ||
| 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) { |
| @@ -73,6 +80,7 @@ | @@ -73,6 +80,7 @@ | ||
| 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 | } |
| @@ -86,7 +94,7 @@ | @@ -86,7 +94,7 @@ | ||
| 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 | ||
| @@ -103,12 +111,21 @@ | @@ -103,12 +111,21 @@ | ||
| 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; |
| 114 | + [_bars addObject:bar]; | ||
| 106 | [self addSubview:bar]; | 115 | [self addSubview:bar]; |
| 107 | 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 |
-
Please register or login to post a comment