Showing
2 changed files
with
13 additions
and
12 deletions
| @@ -12,6 +12,7 @@ | @@ -12,6 +12,7 @@ | ||
| 12 | #define xLabelMargin 15 | 12 | #define xLabelMargin 15 |
| 13 | #define yLabelMargin 15 | 13 | #define yLabelMargin 15 |
| 14 | #define yLabelHeight 11 | 14 | #define yLabelHeight 11 |
| 15 | +#define xLabelHeight 20 | ||
| 15 | 16 | ||
| 16 | @interface PNBarChart : UIView | 17 | @interface PNBarChart : UIView |
| 17 | 18 |
| @@ -33,7 +33,7 @@ | @@ -33,7 +33,7 @@ | ||
| 33 | _labels = [NSMutableArray array]; | 33 | _labels = [NSMutableArray array]; |
| 34 | _bars = [NSMutableArray array]; | 34 | _bars = [NSMutableArray array]; |
| 35 | } | 35 | } |
| 36 | - | 36 | + |
| 37 | return self; | 37 | return self; |
| 38 | } | 38 | } |
| 39 | 39 | ||
| @@ -41,7 +41,7 @@ | @@ -41,7 +41,7 @@ | ||
| 41 | { | 41 | { |
| 42 | _yValues = yValues; | 42 | _yValues = yValues; |
| 43 | [self setYLabels:yValues]; | 43 | [self setYLabels:yValues]; |
| 44 | - | 44 | + |
| 45 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count]; | 45 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count]; |
| 46 | } | 46 | } |
| 47 | 47 | ||
| @@ -53,21 +53,21 @@ | @@ -53,21 +53,21 @@ | ||
| 53 | if (value > max) { | 53 | if (value > max) { |
| 54 | max = value; | 54 | max = value; |
| 55 | } | 55 | } |
| 56 | - | 56 | + |
| 57 | } | 57 | } |
| 58 | - | 58 | + |
| 59 | //Min value for Y label | 59 | //Min value for Y label |
| 60 | if (max < 5) { | 60 | if (max < 5) { |
| 61 | max = 5; | 61 | max = 5; |
| 62 | } | 62 | } |
| 63 | - | 63 | + |
| 64 | _yValueMax = (int)max; | 64 | _yValueMax = (int)max; |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | -(void)setXLabels:(NSArray *)xLabels | 67 | -(void)setXLabels:(NSArray *)xLabels |
| 68 | { | 68 | { |
| 69 | _xLabels = xLabels; | 69 | _xLabels = xLabels; |
| 70 | - | 70 | + |
| 71 | if (_showLabel) { | 71 | if (_showLabel) { |
| 72 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count]; | 72 | _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count]; |
| 73 | } | 73 | } |
| @@ -75,7 +75,7 @@ | @@ -75,7 +75,7 @@ | ||
| 75 | 75 | ||
| 76 | -(void)setStrokeColor:(UIColor *)strokeColor | 76 | -(void)setStrokeColor:(UIColor *)strokeColor |
| 77 | { | 77 | { |
| 78 | - _strokeColor = strokeColor; | 78 | + _strokeColor = strokeColor; |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | -(void)strokeChart | 81 | -(void)strokeChart |
| @@ -85,7 +85,7 @@ | @@ -85,7 +85,7 @@ | ||
| 85 | for(int index = 0; index < _xLabels.count; index++) | 85 | for(int index = 0; index < _xLabels.count; index++) |
| 86 | { | 86 | { |
| 87 | NSString* labelText = _xLabels[index]; | 87 | NSString* labelText = _xLabels[index]; |
| 88 | - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)]; | 88 | + PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)]; |
| 89 | [label setTextAlignment:NSTextAlignmentCenter]; | 89 | [label setTextAlignment:NSTextAlignmentCenter]; |
| 90 | label.text = labelText; | 90 | label.text = labelText; |
| 91 | [_labels addObject:label]; | 91 | [_labels addObject:label]; |
| @@ -95,16 +95,16 @@ | @@ -95,16 +95,16 @@ | ||
| 95 | 95 | ||
| 96 | [self viewCleanupForCollection:_bars]; | 96 | [self viewCleanupForCollection:_bars]; |
| 97 | 97 | ||
| 98 | - CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0; | 98 | + CGFloat chartCavanHeight = self.frame.size.height - chartMargin*2 - xLabelHeight*2; |
| 99 | NSInteger index = 0; | 99 | NSInteger index = 0; |
| 100 | 100 | ||
| 101 | for (NSString * valueString in _yValues) { | 101 | for (NSString * valueString in _yValues) { |
| 102 | float value = [valueString floatValue]; | 102 | float value = [valueString floatValue]; |
| 103 | - | 103 | + |
| 104 | float grade = (float)value / (float)_yValueMax; | 104 | float grade = (float)value / (float)_yValueMax; |
| 105 | PNBar * bar; | 105 | PNBar * bar; |
| 106 | if (_showLabel) { | 106 | if (_showLabel) { |
| 107 | - bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)]; | 107 | + bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - xLabelHeight - chartMargin, _xLabelWidth * 0.5, chartCavanHeight)]; |
| 108 | 108 | ||
| 109 | } | 109 | } |
| 110 | else{ | 110 | else{ |
| @@ -115,7 +115,7 @@ | @@ -115,7 +115,7 @@ | ||
| 115 | bar.grade = grade; | 115 | bar.grade = grade; |
| 116 | [_bars addObject:bar]; | 116 | [_bars addObject:bar]; |
| 117 | [self addSubview:bar]; | 117 | [self addSubview:bar]; |
| 118 | - | 118 | + |
| 119 | index += 1; | 119 | index += 1; |
| 120 | } | 120 | } |
| 121 | } | 121 | } |
-
Please register or login to post a comment