kevinzhow

Add hide label mode, Bar chart add rollback

@@ -17,4 +17,6 @@ @@ -17,4 +17,6 @@
17 17
18 @property (nonatomic, strong) UIColor * barColor; 18 @property (nonatomic, strong) UIColor * barColor;
19 19
  20 +-(void)rollBack;
  21 +
20 @end 22 @end
@@ -57,6 +57,13 @@ @@ -57,6 +57,13 @@
57 _chartLine.strokeEnd = 1.0; 57 _chartLine.strokeEnd = 1.0;
58 } 58 }
59 59
  60 +-(void)rollBack{
  61 + [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  62 + _chartLine.strokeColor = [UIColor clearColor].CGColor;
  63 + } completion:nil];
  64 +
  65 +
  66 +}
60 // Only override drawRect: if you perform custom drawing. 67 // Only override drawRect: if you perform custom drawing.
61 // An empty implementation adversely affects performance during animation. 68 // An empty implementation adversely affects performance during animation.
62 - (void)drawRect:(CGRect)rect 69 - (void)drawRect:(CGRect)rect
@@ -64,7 +71,7 @@ @@ -64,7 +71,7 @@
64 //Draw BG 71 //Draw BG
65 CGContextRef context = UIGraphicsGetCurrentContext(); 72 CGContextRef context = UIGraphicsGetCurrentContext();
66 73
67 - CGContextSetFillColorWithColor(context, [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0].CGColor); 74 + CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
68 CGContextFillRect(context, rect); 75 CGContextFillRect(context, rect);
69 76
70 } 77 }
@@ -31,9 +31,10 @@ @@ -31,9 +31,10 @@
31 31
32 @property (nonatomic) int yValueMax; 32 @property (nonatomic) int yValueMax;
33 33
34 -  
35 -  
36 @property (nonatomic, strong) UIColor * strokeColor; 34 @property (nonatomic, strong) UIColor * strokeColor;
37 35
  36 +@property (nonatomic, strong) UIColor * barBackgroundColor;
  37 +
  38 +@property (nonatomic) BOOL showLabel;
38 39
39 @end 40 @end
@@ -20,7 +20,8 @@ @@ -20,7 +20,8 @@
20 // Initialization code 20 // Initialization code
21 self.backgroundColor = [UIColor whiteColor]; 21 self.backgroundColor = [UIColor whiteColor];
22 self.clipsToBounds = YES; 22 self.clipsToBounds = YES;
23 - 23 + _showLabel = YES;
  24 + _barBackgroundColor = PNLightGrey;
24 } 25 }
25 26
26 return self; 27 return self;
@@ -30,6 +31,8 @@ @@ -30,6 +31,8 @@
30 { 31 {
31 _yValues = yValues; 32 _yValues = yValues;
32 [self setYLabels:yValues]; 33 [self setYLabels:yValues];
  34 +
  35 + _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count];
33 } 36 }
34 37
35 -(void)setYLabels:(NSArray *)yLabels 38 -(void)setYLabels:(NSArray *)yLabels
@@ -50,16 +53,15 @@ @@ -50,16 +53,15 @@
50 53
51 _yValueMax = (int)max; 54 _yValueMax = (int)max;
52 55
53 - NSLog(@"Y Max is %d", _yValueMax );  
54 -  
55 56
56 } 57 }
57 58
58 -(void)setXLabels:(NSArray *)xLabels 59 -(void)setXLabels:(NSArray *)xLabels
59 { 60 {
60 _xLabels = xLabels; 61 _xLabels = xLabels;
61 - _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];  
62 62
  63 + if (_showLabel) {
  64 + _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];
63 for (NSString * labelText in xLabels) { 65 for (NSString * labelText in xLabels) {
64 NSInteger index = [xLabels indexOfObject:labelText]; 66 NSInteger index = [xLabels indexOfObject:labelText];
65 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)]; 67 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
@@ -67,6 +69,7 @@ @@ -67,6 +69,7 @@
67 label.text = labelText; 69 label.text = labelText;
68 [self addSubview:label]; 70 [self addSubview:label];
69 } 71 }
  72 + }
70 73
71 } 74 }
72 75
@@ -85,8 +88,13 @@ @@ -85,8 +88,13 @@
85 float value = [valueString floatValue]; 88 float value = [valueString floatValue];
86 89
87 float grade = (float)value / (float)_yValueMax; 90 float grade = (float)value / (float)_yValueMax;
88 - 91 + PNBar * bar = [[PNBar alloc] init];
89 - PNBar * bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)]; 92 + if (_showLabel) {
  93 + bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)];
  94 + }else{
  95 + bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)];
  96 + }
  97 + bar.backgroundColor = _barBackgroundColor;
90 bar.barColor = _strokeColor; 98 bar.barColor = _strokeColor;
91 bar.grade = grade; 99 bar.grade = grade;
92 [self addSubview:bar]; 100 [self addSubview:bar];
@@ -69,6 +69,13 @@ typedef enum { @@ -69,6 +69,13 @@ typedef enum {
69 @property (nonatomic, strong) UIColor * strokeColor; 69 @property (nonatomic, strong) UIColor * strokeColor;
70 70
71 /** 71 /**
  72 + * PNChart bar chart background color. The default is PNLightGrey.
  73 + *
  74 + */
  75 +
  76 +@property (nonatomic, strong) UIColor * barBackgroundColor;
  77 +
  78 +/**
72 * PNChart circle chart total number. 79 * PNChart circle chart total number.
73 * 80 *
74 */ 81 */
@@ -81,5 +88,12 @@ typedef enum { @@ -81,5 +88,12 @@ typedef enum {
81 */ 88 */
82 @property (nonatomic, strong) NSNumber * current; 89 @property (nonatomic, strong) NSNumber * current;
83 90
  91 +/**
  92 + * PNChart if chart show labels.
  93 + *
  94 + */
  95 +@property (nonatomic) BOOL showLabel;
  96 +
  97 +
84 98
85 @end 99 @end
@@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
18 self.backgroundColor = [UIColor whiteColor]; 18 self.backgroundColor = [UIColor whiteColor];
19 self.clipsToBounds = NO; 19 self.clipsToBounds = NO;
20 self.type = PNLineType; 20 self.type = PNLineType;
  21 + _showLabel = YES;
21 self.strokeColor = PNFreshGreen; 22 self.strokeColor = PNFreshGreen;
22 } 23 }
23 24
@@ -28,6 +29,7 @@ @@ -28,6 +29,7 @@
28 if (self.type == PNLineType) { 29 if (self.type == PNLineType) {
29 _lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 30 _lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
30 _lineChart.backgroundColor = [UIColor clearColor]; 31 _lineChart.backgroundColor = [UIColor clearColor];
  32 + _lineChart.showLabel = _showLabel;
31 [self addSubview:_lineChart]; 33 [self addSubview:_lineChart];
32 [_lineChart setYValues:_yValues]; 34 [_lineChart setYValues:_yValues];
33 [_lineChart setXLabels:_xLabels]; 35 [_lineChart setXLabels:_xLabels];
@@ -38,6 +40,10 @@ @@ -38,6 +40,10 @@
38 { 40 {
39 _barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 41 _barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
40 _barChart.backgroundColor = [UIColor clearColor]; 42 _barChart.backgroundColor = [UIColor clearColor];
  43 + if (_barBackgroundColor) {
  44 + _barChart.barBackgroundColor = _barBackgroundColor;
  45 + }
  46 + _barChart.showLabel = _showLabel;
41 [self addSubview:_barChart]; 47 [self addSubview:_barChart];
42 [_barChart setYValues:_yValues]; 48 [_barChart setYValues:_yValues];
43 [_barChart setXLabels:_xLabels]; 49 [_barChart setXLabels:_xLabels];
@@ -59,7 +65,25 @@ @@ -59,7 +65,25 @@
59 65
60 -(void)strokeChart 66 -(void)strokeChart
61 { 67 {
  68 + if (_lineChart) {
  69 +
  70 + [_lineChart strokeChart];
  71 + [_lineChart setStrokeColor:_strokeColor];
  72 +
  73 + }else if (_barChart)
  74 + {
  75 +
  76 + [_barChart strokeChart];
  77 + [_barChart setStrokeColor:_strokeColor];
  78 +
  79 + }else if (_circleChart)
  80 + {
  81 + [_circleChart strokeChart];
  82 + [_circleChart setStrokeColor:_strokeColor];
  83 + }else{
62 [self setUpChart]; 84 [self setUpChart];
  85 + }
  86 +
63 87
64 } 88 }
65 89
@@ -36,5 +36,6 @@ @@ -36,5 +36,6 @@
36 36
37 @property (nonatomic, strong) UIColor * strokeColor; 37 @property (nonatomic, strong) UIColor * strokeColor;
38 38
  39 +@property (nonatomic) BOOL showLabel;
39 40
40 @end 41 @end
@@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
25 _chartLine.fillColor = [[UIColor whiteColor] CGColor]; 25 _chartLine.fillColor = [[UIColor whiteColor] CGColor];
26 _chartLine.lineWidth = 3.0; 26 _chartLine.lineWidth = 3.0;
27 _chartLine.strokeEnd = 0.0; 27 _chartLine.strokeEnd = 0.0;
  28 + _showLabel = YES;
28 [self.layer addSublayer:_chartLine]; 29 [self.layer addSublayer:_chartLine];
29 } 30 }
30 31
@@ -34,18 +35,14 @@ @@ -34,18 +35,14 @@
34 -(void)setYValues:(NSArray *)yValues 35 -(void)setYValues:(NSArray *)yValues
35 { 36 {
36 _yValues = yValues; 37 _yValues = yValues;
37 - [self setYLabels:yValues]; 38 + _xLabelWidth = (self.frame.size.width)/[_yValues count];
38 -}  
39 39
40 --(void)setYLabels:(NSArray *)yLabels 40 + float max = 0;
41 -{ 41 + for (NSString * valueString in yValues) {
42 - NSInteger max = 0; 42 + float value = [valueString floatValue];
43 - for (NSString * valueString in yLabels) {  
44 - NSInteger value = [valueString integerValue];  
45 if (value > max) { 43 if (value > max) {
46 max = value; 44 max = value;
47 } 45 }
48 -  
49 } 46 }
50 47
51 //Min value for Y label 48 //Min value for Y label
@@ -53,9 +50,19 @@ @@ -53,9 +50,19 @@
53 max = 5; 50 max = 5;
54 } 51 }
55 52
56 - _yValueMax = (int)max; 53 + _yValueMax = (float)max;
  54 +
  55 + if (_showLabel) {
  56 + [self setYLabels:yValues];
  57 + }
  58 +
  59 +}
57 60
58 - float level = max /[yLabels count]; 61 +-(void)setYLabels:(NSArray *)yLabels
  62 +{
  63 +
  64 +
  65 + float level = _yValueMax /[yLabels count];
59 66
60 NSInteger index = 0; 67 NSInteger index = 0;
61 NSInteger num = [yLabels count] + 1; 68 NSInteger num = [yLabels count] + 1;
@@ -75,8 +82,9 @@ @@ -75,8 +82,9 @@
75 -(void)setXLabels:(NSArray *)xLabels 82 -(void)setXLabels:(NSArray *)xLabels
76 { 83 {
77 _xLabels = xLabels; 84 _xLabels = xLabels;
78 - _xLabelWidth = (self.frame.size.width - chartMargin - 30.0)/[xLabels count];  
79 85
  86 + if(_showLabel){
  87 + _xLabelWidth = (self.frame.size.width - chartMargin - 30.0)/[xLabels count];
80 for (NSString * labelText in xLabels) { 88 for (NSString * labelText in xLabels) {
81 NSInteger index = [xLabels indexOfObject:labelText]; 89 NSInteger index = [xLabels indexOfObject:labelText];
82 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + 30.0, self.frame.size.height - 30.0, _xLabelWidth, 20.0)]; 90 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + 30.0, self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
@@ -84,6 +92,8 @@ @@ -84,6 +92,8 @@
84 label.text = labelText; 92 label.text = labelText;
85 [self addSubview:label]; 93 [self addSubview:label];
86 } 94 }
  95 + }
  96 +
87 97
88 } 98 }
89 99
@@ -101,25 +111,29 @@ @@ -101,25 +111,29 @@
101 111
102 CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue]; 112 CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue];
103 113
104 - CGFloat xPosition = _xLabelWidth ; 114 + CGFloat xPosition = 0;
105 - 115 + CGFloat chartCavanHeight = self.frame.size.height - 40.0;
106 - CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0; 116 + if(_showLabel){
  117 + chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
  118 + xPosition = _xLabelWidth;
  119 + }
107 120
108 float grade = (float)firstValue / (float)_yValueMax; 121 float grade = (float)firstValue / (float)_yValueMax;
  122 +
  123 +
109 [progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + 20.0)]; 124 [progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + 20.0)];
110 [progressline setLineWidth:3.0]; 125 [progressline setLineWidth:3.0];
111 [progressline setLineCapStyle:kCGLineCapRound]; 126 [progressline setLineCapStyle:kCGLineCapRound];
112 [progressline setLineJoinStyle:kCGLineJoinRound]; 127 [progressline setLineJoinStyle:kCGLineJoinRound];
113 NSInteger index = 0; 128 NSInteger index = 0;
114 for (NSString * valueString in _yValues) { 129 for (NSString * valueString in _yValues) {
115 - NSInteger value = [valueString integerValue]; 130 + float value = [valueString floatValue];
116 131
117 float grade = (float)value / (float)_yValueMax; 132 float grade = (float)value / (float)_yValueMax;
118 if (index != 0) { 133 if (index != 0) {
119 134
120 - [progressline addLineToPoint:CGPointMake(index * xPosition + 30.0+ _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0)]; 135 + [progressline addLineToPoint:CGPointMake(index * _xLabelWidth + 30.0+ _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0)];
121 - 136 + [progressline moveToPoint:CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0 )];
122 - [progressline moveToPoint:CGPointMake(index * xPosition + 30.0 + _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0 )];  
123 137
124 [progressline stroke]; 138 [progressline stroke];
125 } 139 }