kevinzhow

Merge branch 'feature/dynamic_update_with_animate' into develop

@@ -18,5 +18,6 @@ @@ -18,5 +18,6 @@
18 @property (nonatomic) UIColor *barColor; 18 @property (nonatomic) UIColor *barColor;
19 @property (nonatomic) UIColor *barColorGradientStart; 19 @property (nonatomic) UIColor *barColorGradientStart;
20 @property (nonatomic) CGFloat barRadius; 20 @property (nonatomic) CGFloat barRadius;
  21 +@property (nonatomic) CAShapeLayer *gradientMask;
21 22
22 @end 23 @end
@@ -38,7 +38,8 @@ @@ -38,7 +38,8 @@
38 38
39 - (void)setGrade:(float)grade 39 - (void)setGrade:(float)grade
40 { 40 {
41 - _grade = grade; 41 + NSLog(@"New garde %f",grade);
  42 +
42 UIBezierPath *progressline = [UIBezierPath bezierPath]; 43 UIBezierPath *progressline = [UIBezierPath bezierPath];
43 44
44 [progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)]; 45 [progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)];
@@ -46,7 +47,7 @@ @@ -46,7 +47,7 @@
46 47
47 [progressline setLineWidth:1.0]; 48 [progressline setLineWidth:1.0];
48 [progressline setLineCapStyle:kCGLineCapSquare]; 49 [progressline setLineCapStyle:kCGLineCapSquare];
49 - _chartLine.path = progressline.CGPath; 50 +
50 51
51 if (_barColor) { 52 if (_barColor) {
52 _chartLine.strokeColor = [_barColor CGColor]; 53 _chartLine.strokeColor = [_barColor CGColor];
@@ -55,6 +56,26 @@ @@ -55,6 +56,26 @@
55 _chartLine.strokeColor = [PNGreen CGColor]; 56 _chartLine.strokeColor = [PNGreen CGColor];
56 } 57 }
57 58
  59 + if (_grade) {
  60 +
  61 + CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
  62 + pathAnimation.fromValue = (id)_chartLine.path;
  63 + pathAnimation.toValue = (id)[progressline CGPath];
  64 + pathAnimation.duration = 0.5f;
  65 + pathAnimation.autoreverses = NO;
  66 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  67 + [_chartLine addAnimation:pathAnimation forKey:@"animationKey"];
  68 +
  69 + _chartLine.path = progressline.CGPath;
  70 +
  71 + if (_barColorGradientStart) {
  72 +
  73 + // Add gradient
  74 + [self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"];
  75 + self.gradientMask.path = progressline.CGPath;
  76 + }
  77 +
  78 + }else{
58 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 79 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
59 pathAnimation.duration = 1.0; 80 pathAnimation.duration = 1.0;
60 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 81 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
@@ -64,16 +85,17 @@ @@ -64,16 +85,17 @@
64 85
65 _chartLine.strokeEnd = 1.0; 86 _chartLine.strokeEnd = 1.0;
66 87
  88 + _chartLine.path = progressline.CGPath;
67 // Check if user wants to add a gradient from the start color to the bar color 89 // Check if user wants to add a gradient from the start color to the bar color
68 if (_barColorGradientStart) { 90 if (_barColorGradientStart) {
69 91
70 // Add gradient 92 // Add gradient
71 - CAShapeLayer *gradientMask = [CAShapeLayer layer]; 93 + self.gradientMask = [CAShapeLayer layer];
72 - gradientMask.fillColor = [[UIColor clearColor] CGColor]; 94 + self.gradientMask.fillColor = [[UIColor clearColor] CGColor];
73 - gradientMask.strokeColor = [[UIColor blackColor] CGColor]; 95 + self.gradientMask.strokeColor = [[UIColor blackColor] CGColor];
74 - gradientMask.lineWidth = self.frame.size.width; 96 + self.gradientMask.lineWidth = self.frame.size.width;
75 - gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 97 + self.gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
76 - gradientMask.path = progressline.CGPath; 98 + self.gradientMask.path = progressline.CGPath;
77 99
78 100
79 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 101 CAGradientLayer *gradientLayer = [CAGradientLayer layer];
@@ -87,14 +109,17 @@ @@ -87,14 +109,17 @@
87 ]; 109 ];
88 gradientLayer.colors = colors; 110 gradientLayer.colors = colors;
89 111
90 - [gradientLayer setMask:gradientMask]; 112 + [gradientLayer setMask:self.gradientMask];
91 113
92 [_chartLine addSublayer:gradientLayer]; 114 [_chartLine addSublayer:gradientLayer];
93 115
94 - gradientMask.strokeEnd = 1.0; 116 + self.gradientMask.strokeEnd = 1.0;
95 - [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 117 + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  118 + }
96 } 119 }
97 120
  121 + _grade = grade;
  122 +
98 } 123 }
99 124
100 125
@@ -36,6 +36,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); @@ -36,6 +36,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
36 @property (nonatomic) NSArray *strokeColors; 36 @property (nonatomic) NSArray *strokeColors;
37 37
38 38
  39 +/** Update Values. */
  40 +- (void)updateChartData:(NSArray *)data;
  41 +
39 /** Changes chart margin. */ 42 /** Changes chart margin. */
40 @property (nonatomic) CGFloat yChartLabelWidth; 43 @property (nonatomic) CGFloat yChartLabelWidth;
41 44
@@ -12,7 +12,8 @@ @@ -12,7 +12,8 @@
12 12
13 13
14 @interface PNBarChart () { 14 @interface PNBarChart () {
15 - NSMutableArray *_labels; 15 + NSMutableArray *_xChartLabels;
  16 + NSMutableArray *_yChartLabels;
16 } 17 }
17 18
18 - (UIColor *)barColorAtIndex:(NSUInteger)index; 19 - (UIColor *)barColorAtIndex:(NSUInteger)index;
@@ -32,7 +33,8 @@ @@ -32,7 +33,8 @@
32 _barBackgroundColor = PNLightGrey; 33 _barBackgroundColor = PNLightGrey;
33 _labelTextColor = [UIColor grayColor]; 34 _labelTextColor = [UIColor grayColor];
34 _labelFont = [UIFont systemFontOfSize:11.0f]; 35 _labelFont = [UIFont systemFontOfSize:11.0f];
35 - _labels = [NSMutableArray array]; 36 + _xChartLabels = [NSMutableArray array];
  37 + _yChartLabels = [NSMutableArray array];
36 _bars = [NSMutableArray array]; 38 _bars = [NSMutableArray array];
37 _xLabelSkip = 1; 39 _xLabelSkip = 1;
38 _yLabelSum = 4; 40 _yLabelSum = 4;
@@ -58,8 +60,40 @@ @@ -58,8 +60,40 @@
58 [self getYValueMax:yValues]; 60 [self getYValueMax:yValues];
59 } 61 }
60 62
  63 + if (_yChartLabels) {
  64 + [self viewCleanupForCollection:_yChartLabels];
  65 + }else{
  66 + _yLabels = [NSMutableArray new];
  67 + }
  68 +
  69 + if (_showLabel) {
  70 + //Add y labels
  71 +
  72 + float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - xLabelHeight) / _yLabelSum;
61 73
62 - _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [_yValues count]; 74 + for (int index = 0; index < _yLabelSum; index++) {
  75 +
  76 + NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum ));
  77 +
  78 + PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0,
  79 + yLabelSectionHeight * index + _chartMargin - yLabelHeight/2.0,
  80 + _yChartLabelWidth,
  81 + yLabelHeight)];
  82 + label.font = _labelFont;
  83 + label.textColor = _labelTextColor;
  84 + [label setTextAlignment:NSTextAlignmentRight];
  85 + label.text = labelText;
  86 +
  87 + [_yChartLabels addObject:label];
  88 + [self addSubview:label];
  89 +
  90 + }
  91 + }
  92 +}
  93 +
  94 +-(void)updateChartData:(NSArray *)data{
  95 + self.yValues = data;
  96 + [self updateBar];
63 } 97 }
64 98
65 - (void)getYValueMax:(NSArray *)yLabels 99 - (void)getYValueMax:(NSArray *)yLabels
@@ -77,24 +111,14 @@ @@ -77,24 +111,14 @@
77 { 111 {
78 _xLabels = xLabels; 112 _xLabels = xLabels;
79 113
80 - if (_showLabel) { 114 + if (_xChartLabels) {
81 - _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count]; 115 + [self viewCleanupForCollection:_xChartLabels];
  116 + }else{
  117 + _xChartLabels = [NSMutableArray new];
82 } 118 }
83 -}  
84 -  
85 119
86 -- (void)setStrokeColor:(UIColor *)strokeColor  
87 -{  
88 - _strokeColor = strokeColor;  
89 -}  
90 -  
91 -  
92 -- (void)strokeChart  
93 -{  
94 - [self viewCleanupForCollection:_labels];  
95 - //Add Labels  
96 if (_showLabel) { 120 if (_showLabel) {
97 - //Add x labels 121 + _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count];
98 int labelAddCount = 0; 122 int labelAddCount = 0;
99 for (int index = 0; index < _xLabels.count; index++) { 123 for (int index = 0; index < _xLabels.count; index++) {
100 labelAddCount += 1; 124 labelAddCount += 1;
@@ -119,52 +143,33 @@ @@ -119,52 +143,33 @@
119 self.frame.size.height - xLabelHeight - _chartMargin + label.frame.size.height /2.0 + _labelMarginTop); 143 self.frame.size.height - xLabelHeight - _chartMargin + label.frame.size.height /2.0 + _labelMarginTop);
120 labelAddCount = 0; 144 labelAddCount = 0;
121 145
122 - [_labels addObject:label]; 146 + [_xChartLabels addObject:label];
123 [self addSubview:label]; 147 [self addSubview:label];
124 } 148 }
125 } 149 }
126 -  
127 - //Add y labels  
128 -  
129 - float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - xLabelHeight) / _yLabelSum;  
130 -  
131 - for (int index = 0; index < _yLabelSum; index++) {  
132 -  
133 - NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum ));  
134 -  
135 - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0,  
136 - yLabelSectionHeight * index + _chartMargin - yLabelHeight/2.0,  
137 - _yChartLabelWidth,  
138 - yLabelHeight)];  
139 - label.font = _labelFont;  
140 - label.textColor = _labelTextColor;  
141 - [label setTextAlignment:NSTextAlignmentRight];  
142 - label.text = labelText;  
143 -  
144 - [_labels addObject:label];  
145 - [self addSubview:label];  
146 -  
147 - }  
148 } 150 }
  151 +}
149 152
150 153
151 - [self viewCleanupForCollection:_bars]; 154 +- (void)setStrokeColor:(UIColor *)strokeColor
  155 +{
  156 + _strokeColor = strokeColor;
  157 +}
152 158
  159 +- (void)updateBar
  160 +{
153 161
154 //Add bars 162 //Add bars
155 CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - xLabelHeight; 163 CGFloat chartCavanHeight = self.frame.size.height - _chartMargin * 2 - xLabelHeight;
156 NSInteger index = 0; 164 NSInteger index = 0;
157 165
158 for (NSNumber *valueString in _yValues) { 166 for (NSNumber *valueString in _yValues) {
159 - float value = [valueString floatValue];  
160 -  
161 - float grade = (float)value / (float)_yValueMax;  
162 -  
163 - if (isnan(grade)) {  
164 - grade = 0;  
165 - }  
166 167
167 PNBar *bar; 168 PNBar *bar;
  169 +
  170 + if (_bars.count == _yValues.count) {
  171 + bar = [_bars objectAtIndex:index];
  172 + }else{
168 CGFloat barWidth; 173 CGFloat barWidth;
169 CGFloat barXPosition; 174 CGFloat barXPosition;
170 175
@@ -200,23 +205,40 @@ @@ -200,23 +205,40 @@
200 }else{ 205 }else{
201 bar.barColor = [self barColorAtIndex:index]; 206 bar.barColor = [self barColorAtIndex:index];
202 } 207 }
203 -  
204 - //Height Of Bar  
205 - bar.grade = grade;  
206 -  
207 // Add gradient 208 // Add gradient
208 bar.barColorGradientStart = _barColorGradientStart; 209 bar.barColorGradientStart = _barColorGradientStart;
209 210
210 -  
211 //For Click Index 211 //For Click Index
212 bar.tag = index; 212 bar.tag = index;
213 213
214 -  
215 [_bars addObject:bar]; 214 [_bars addObject:bar];
216 [self addSubview:bar]; 215 [self addSubview:bar];
  216 + }
  217 +
  218 + //Height Of Bar
  219 + float value = [valueString floatValue];
  220 +
  221 + float grade = (float)value / (float)_yValueMax;
  222 +
  223 + if (isnan(grade)) {
  224 + grade = 0;
  225 + }
  226 + bar.grade = grade;
217 227
218 index += 1; 228 index += 1;
219 } 229 }
  230 +}
  231 +
  232 +- (void)strokeChart
  233 +{
  234 + //Add Labels
  235 +
  236 + [self viewCleanupForCollection:_bars];
  237 +
  238 +
  239 + //Update Bar
  240 +
  241 + [self updateBar];
220 242
221 //Add chart border lines 243 //Add chart border lines
222 244
@@ -22,6 +22,7 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { @@ -22,6 +22,7 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) {
22 22
23 - (void)strokeChart; 23 - (void)strokeChart;
24 - (void)growChartByAmount:(NSNumber *)growAmount; 24 - (void)growChartByAmount:(NSNumber *)growAmount;
  25 +- (void)updateChartByCurrent:(NSNumber *)current;
25 - (id)initWithFrame:(CGRect)frame 26 - (id)initWithFrame:(CGRect)frame
26 total:(NSNumber *)total 27 total:(NSNumber *)total
27 current:(NSNumber *)current 28 current:(NSNumber *)current
@@ -37,7 +38,9 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) { @@ -37,7 +38,9 @@ typedef NS_ENUM (NSUInteger, PNChartFormatType) {
37 @property (nonatomic) NSTimeInterval duration; 38 @property (nonatomic) NSTimeInterval duration;
38 @property (nonatomic) PNChartFormatType chartType; 39 @property (nonatomic) PNChartFormatType chartType;
39 40
  41 +
40 @property (nonatomic) CAShapeLayer *circle; 42 @property (nonatomic) CAShapeLayer *circle;
  43 +@property (nonatomic) CAShapeLayer *gradientMask;
41 @property (nonatomic) CAShapeLayer *circleBackground; 44 @property (nonatomic) CAShapeLayer *circleBackground;
42 45
43 @end 46 @end
@@ -116,14 +116,14 @@ @@ -116,14 +116,14 @@
116 if (_strokeColorGradientStart) { 116 if (_strokeColorGradientStart) {
117 117
118 // Add gradient 118 // Add gradient
119 - CAShapeLayer *gradientMask = [CAShapeLayer layer]; 119 + self.gradientMask = [CAShapeLayer layer];
120 - gradientMask.fillColor = [[UIColor clearColor] CGColor]; 120 + self.gradientMask.fillColor = [[UIColor clearColor] CGColor];
121 - gradientMask.strokeColor = [[UIColor blackColor] CGColor]; 121 + self.gradientMask.strokeColor = [[UIColor blackColor] CGColor];
122 - gradientMask.lineWidth = _circle.lineWidth; 122 + self.gradientMask.lineWidth = _circle.lineWidth;
123 - gradientMask.lineCap = kCALineCapRound; 123 + self.gradientMask.lineCap = kCALineCapRound;
124 CGRect gradientFrame = CGRectMake(0, 0, 2*self.bounds.size.width, 2*self.bounds.size.height); 124 CGRect gradientFrame = CGRectMake(0, 0, 2*self.bounds.size.width, 2*self.bounds.size.height);
125 - gradientMask.frame = gradientFrame; 125 + self.gradientMask.frame = gradientFrame;
126 - gradientMask.path = _circle.path; 126 + self.gradientMask.path = _circle.path;
127 127
128 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 128 CAGradientLayer *gradientLayer = [CAGradientLayer layer];
129 gradientLayer.startPoint = CGPointMake(0.5,1.0); 129 gradientLayer.startPoint = CGPointMake(0.5,1.0);
@@ -136,13 +136,13 @@ @@ -136,13 +136,13 @@
136 ]; 136 ];
137 gradientLayer.colors = colors; 137 gradientLayer.colors = colors;
138 138
139 - [gradientLayer setMask:gradientMask]; 139 + [gradientLayer setMask:self.gradientMask];
140 140
141 [_circle addSublayer:gradientLayer]; 141 [_circle addSublayer:gradientLayer];
142 142
143 - gradientMask.strokeEnd = [_current floatValue] / [_total floatValue]; 143 + self.gradientMask.strokeEnd = [_current floatValue] / [_total floatValue];
144 144
145 - [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 145 + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
146 } 146 }
147 } 147 }
148 148
@@ -153,16 +153,27 @@ @@ -153,16 +153,27 @@
153 NSNumber *updatedValue = [NSNumber numberWithFloat:[_current floatValue] + [growAmount floatValue]]; 153 NSNumber *updatedValue = [NSNumber numberWithFloat:[_current floatValue] + [growAmount floatValue]];
154 154
155 // Add animation 155 // Add animation
  156 + [self updateChartByCurrent:updatedValue];
  157 +}
  158 +
  159 +
  160 +-(void)updateChartByCurrent:(NSNumber *)current{
  161 + // Add animation
156 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 162 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
157 pathAnimation.duration = self.duration; 163 pathAnimation.duration = self.duration;
158 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 164 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
159 pathAnimation.fromValue = @([_current floatValue] / [_total floatValue]); 165 pathAnimation.fromValue = @([_current floatValue] / [_total floatValue]);
160 - pathAnimation.toValue = @([updatedValue floatValue] / [_total floatValue]); 166 + pathAnimation.toValue = @([current floatValue] / [_total floatValue]);
161 - _circle.strokeEnd = [updatedValue floatValue] / [_total floatValue]; 167 + _circle.strokeEnd = [current floatValue] / [_total floatValue];
  168 +
  169 + if (_strokeColorGradientStart) {
  170 + self.gradientMask.strokeEnd = _circle.strokeEnd;
  171 + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  172 + }
162 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 173 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
163 174
164 - [self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([_current floatValue] + [growAmount floatValue], [_total floatValue]) withDuration:self.duration]; 175 + [self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([current floatValue], [_total floatValue]) withDuration:self.duration];
165 - _current = updatedValue; 176 + _current = current;
166 } 177 }
167 178
168 @end 179 @end
@@ -28,10 +28,15 @@ @@ -28,10 +28,15 @@
28 @property (nonatomic) NSArray *chartData; 28 @property (nonatomic) NSArray *chartData;
29 29
30 @property (nonatomic) NSMutableArray *pathPoints; 30 @property (nonatomic) NSMutableArray *pathPoints;
  31 +@property (nonatomic) NSMutableArray *xChartLabels;
  32 +@property (nonatomic) NSMutableArray *yChartLabels;
  33 +
31 @property (nonatomic) CGFloat xLabelWidth; 34 @property (nonatomic) CGFloat xLabelWidth;
32 @property (nonatomic) UIFont *xLabelFont; 35 @property (nonatomic) UIFont *xLabelFont;
33 @property (nonatomic) UIColor *xLabelColor; 36 @property (nonatomic) UIColor *xLabelColor;
34 @property (nonatomic) CGFloat yValueMax; 37 @property (nonatomic) CGFloat yValueMax;
  38 +@property (nonatomic) CGFloat yFixedValueMax;
  39 +@property (nonatomic) CGFloat yFixedValueMin;
35 @property (nonatomic) CGFloat yValueMin; 40 @property (nonatomic) CGFloat yValueMin;
36 @property (nonatomic) NSInteger yLabelNum; 41 @property (nonatomic) NSInteger yLabelNum;
37 @property (nonatomic) CGFloat yLabelHeight; 42 @property (nonatomic) CGFloat yLabelHeight;
@@ -59,4 +64,10 @@ @@ -59,4 +64,10 @@
59 64
60 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width; 65 - (void)setXLabels:(NSArray *)xLabels withWidth:(CGFloat)width;
61 66
  67 +/**
  68 + * Update Chart Value
  69 + */
  70 +
  71 +- (void)updateChartData:(NSArray *)data;
  72 +
62 @end 73 @end
This diff is collapsed. Click to expand it.
@@ -22,15 +22,30 @@ @@ -22,15 +22,30 @@
22 <state key="normal" title="Change Value"> 22 <state key="normal" title="Change Value">
23 <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 23 <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
24 </state> 24 </state>
  25 + <connections>
  26 + <action selector="changeValue:" destination="Tha-Wr-sPW" eventType="touchUpInside" id="zeG-Cp-Wjs"/>
  27 + </connections>
25 </button> 28 </button>
  29 + <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FUU-vZ-jMd">
  30 + <rect key="frame" x="53" y="81" width="215" height="30"/>
  31 + <fontDescription key="fontDescription" name="Avenir-Medium" family="Avenir" pointSize="23"/>
  32 + <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
  33 + <nil key="highlightedColor"/>
  34 + </label>
26 </subviews> 35 </subviews>
27 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 36 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
28 <constraints> 37 <constraints>
  38 + <constraint firstItem="FUU-vZ-jMd" firstAttribute="top" secondItem="znr-YO-4a4" secondAttribute="bottom" constant="17" id="DLv-qJ-h7R"/>
  39 + <constraint firstAttribute="centerX" secondItem="FUU-vZ-jMd" secondAttribute="centerX" id="YGT-a5-Zka"/>
29 <constraint firstItem="L3F-13-Wf5" firstAttribute="top" secondItem="znr-YO-4a4" secondAttribute="bottom" constant="300" id="ewm-kv-p8k"/> 40 <constraint firstItem="L3F-13-Wf5" firstAttribute="top" secondItem="znr-YO-4a4" secondAttribute="bottom" constant="300" id="ewm-kv-p8k"/>
30 <constraint firstAttribute="centerX" secondItem="L3F-13-Wf5" secondAttribute="centerX" id="zXw-WV-mro"/> 41 <constraint firstAttribute="centerX" secondItem="L3F-13-Wf5" secondAttribute="centerX" id="zXw-WV-mro"/>
31 </constraints> 42 </constraints>
32 </view> 43 </view>
33 <navigationItem key="navigationItem" title="PNChart" id="Ukg-Sg-E2z"/> 44 <navigationItem key="navigationItem" title="PNChart" id="Ukg-Sg-E2z"/>
  45 + <connections>
  46 + <outlet property="changeValueButton" destination="L3F-13-Wf5" id="JnI-y3-Xpj"/>
  47 + <outlet property="titleLabel" destination="FUU-vZ-jMd" id="dA3-KC-Ht4"/>
  48 + </connections>
34 </viewController> 49 </viewController>
35 <placeholder placeholderIdentifier="IBFirstResponder" id="kDa-u3-t6i" userLabel="First Responder" sceneMemberID="firstResponder"/> 50 <placeholder placeholderIdentifier="IBFirstResponder" id="kDa-u3-t6i" userLabel="First Responder" sceneMemberID="firstResponder"/>
36 </objects> 51 </objects>
@@ -7,7 +7,19 @@ @@ -7,7 +7,19 @@
7 // 7 //
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
  10 +#import "PNChartDelegate.h"
  11 +#import "PNChart.h"
10 12
11 -@interface PCChartViewController : UIViewController 13 +@interface PCChartViewController : UIViewController<PNChartDelegate>
  14 +
  15 +@property (nonatomic) PNLineChart * lineChart;
  16 +@property (nonatomic) PNBarChart * barChart;
  17 +@property (nonatomic) PNCircleChart * circleChart;
  18 +@property (nonatomic) PNPieChart *pieChart;
  19 +
  20 +@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  21 +
  22 +- (IBAction)changeValue:(id)sender;
  23 +@property (weak, nonatomic) IBOutlet UIButton *changeValueButton;
12 24
13 @end 25 @end
@@ -10,4 +10,189 @@ @@ -10,4 +10,189 @@
10 10
11 @implementation PCChartViewController 11 @implementation PCChartViewController
12 12
  13 +-(void)viewDidLoad
  14 +{
  15 + [super viewDidLoad];
  16 + self.titleLabel.textColor = PNFreshGreen;
  17 +
  18 + if ([self.title isEqualToString:@"Line Chart"]) {
  19 +
  20 + self.titleLabel.text = @"Line Chart";
  21 +
  22 + self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
  23 + self.lineChart.yLabelFormat = @"%1.1f";
  24 + self.lineChart.backgroundColor = [UIColor clearColor];
  25 + [self.lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
  26 + self.lineChart.showCoordinateAxis = YES;
  27 +
  28 + //Use yFixedValueMax and yFixedValueMin to Fix the Max and Min Y Value
  29 + //Only if you needed
  30 + self.lineChart.yFixedValueMax = 500.0;
  31 + self.lineChart.yFixedValueMin = 1.0;
  32 +
  33 + // Line Chart #1
  34 + NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2, @127.2, @176.2];
  35 + PNLineChartData *data01 = [PNLineChartData new];
  36 + data01.color = PNFreshGreen;
  37 + data01.itemCount = data01Array.count;
  38 + data01.inflexionPointStyle = PNLineChartPointStyleTriangle;
  39 + data01.getData = ^(NSUInteger index) {
  40 + CGFloat yValue = [data01Array[index] floatValue];
  41 + return [PNLineChartDataItem dataItemWithY:yValue];
  42 + };
  43 +
  44 + // Line Chart #2
  45 + NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2];
  46 + PNLineChartData *data02 = [PNLineChartData new];
  47 + data02.color = PNTwitterColor;
  48 + data02.itemCount = data02Array.count;
  49 + data02.inflexionPointStyle = PNLineChartPointStyleSquare;
  50 + data02.getData = ^(NSUInteger index) {
  51 + CGFloat yValue = [data02Array[index] floatValue];
  52 + return [PNLineChartDataItem dataItemWithY:yValue];
  53 + };
  54 +
  55 + self.lineChart.chartData = @[data01, data02];
  56 + [self.lineChart strokeChart];
  57 + self.lineChart.delegate = self;
  58 +
  59 +
  60 + [self.view addSubview:self.lineChart];
  61 + }
  62 + else if ([self.title isEqualToString:@"Bar Chart"])
  63 + {
  64 + self.titleLabel.text = @"Bar Chart";
  65 +
  66 + self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
  67 + self.barChart.backgroundColor = [UIColor clearColor];
  68 + self.barChart.yLabelFormatter = ^(CGFloat yValue){
  69 + CGFloat yValueParsed = yValue;
  70 + NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed];
  71 + return labelText;
  72 + };
  73 + self.barChart.labelMarginTop = 5.0;
  74 + [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
  75 + self.barChart.rotateForXAxisText = true ;
  76 + [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
  77 + [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
  78 + // Adding gradient
  79 + self.barChart.barColorGradientStart = [UIColor blueColor];
  80 +
  81 + [self.barChart strokeChart];
  82 +
  83 + self.barChart.delegate = self;
  84 +
  85 + [self.view addSubview:self.barChart];
  86 + }
  87 + else if ([self.title isEqualToString:@"Circle Chart"])
  88 + {
  89 + self.titleLabel.text = @"Circle Chart";
  90 +
  91 +
  92 + self.circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0)
  93 + total:@100
  94 + current:@60
  95 + clockwise:YES
  96 + shadow:YES];
  97 + self.circleChart.backgroundColor = [UIColor clearColor];
  98 + [self.circleChart setStrokeColor:PNGreen];
  99 + [self.circleChart setStrokeColorGradientStart:[UIColor blueColor]];
  100 + [self.circleChart strokeChart];
  101 +
  102 + [self.view addSubview:self.circleChart];
  103 + }
  104 + else if ([self.title isEqualToString:@"Pie Chart"])
  105 + {
  106 + self.titleLabel.text = @"Pie Chart";
  107 +
  108 + NSArray *items = @[[PNPieChartDataItem dataItemWithValue:10 color:PNLightGreen],
  109 + [PNPieChartDataItem dataItemWithValue:20 color:PNFreshGreen description:@"WWDC"],
  110 + [PNPieChartDataItem dataItemWithValue:40 color:PNDeepGreen description:@"GOOG I/O"],
  111 + ];
  112 +
  113 + self.pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(SCREEN_WIDTH /2.0 - 100, 135, 200.0, 200.0) items:items];
  114 + self.pieChart.descriptionTextColor = [UIColor whiteColor];
  115 + self.pieChart.descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:11.0];
  116 + self.pieChart.descriptionTextShadowColor = [UIColor clearColor];
  117 + [self.pieChart strokeChart];
  118 +
  119 +
  120 + [self.view addSubview:self.pieChart];
  121 + self.changeValueButton.hidden = YES;
  122 + }
  123 +}
  124 +
  125 +
  126 +- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex{
  127 + NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);
  128 +}
  129 +
  130 +- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{
  131 + NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);
  132 +}
  133 +
  134 +
  135 +- (IBAction)changeValue:(id)sender {
  136 +
  137 + if ([self.title isEqualToString:@"Line Chart"]) {
  138 +
  139 + // Line Chart #1
  140 + NSArray * data01Array = @[@(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300)];
  141 + PNLineChartData *data01 = [PNLineChartData new];
  142 + data01.color = PNFreshGreen;
  143 + data01.itemCount = data01Array.count;
  144 + data01.inflexionPointStyle = PNLineChartPointStyleTriangle;
  145 + data01.getData = ^(NSUInteger index) {
  146 + CGFloat yValue = [data01Array[index] floatValue];
  147 + return [PNLineChartDataItem dataItemWithY:yValue];
  148 + };
  149 +
  150 + // Line Chart #2
  151 + NSArray * data02Array = @[@(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300)];
  152 + PNLineChartData *data02 = [PNLineChartData new];
  153 + data02.color = PNTwitterColor;
  154 + data02.itemCount = data02Array.count;
  155 + data02.inflexionPointStyle = PNLineChartPointStyleSquare;
  156 + data02.getData = ^(NSUInteger index) {
  157 + CGFloat yValue = [data02Array[index] floatValue];
  158 + return [PNLineChartDataItem dataItemWithY:yValue];
  159 + };
  160 +
  161 + [self.lineChart setXLabels:@[@"DEC 1",@"DEC 2",@"DEC 3",@"DEC 4",@"DEC 5",@"DEC 6",@"DEC 7"]];
  162 + [self.lineChart updateChartData:@[data01, data02]];
  163 +
  164 + }
  165 + else if ([self.title isEqualToString:@"Bar Chart"])
  166 + {
  167 + [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]];
  168 + [self.barChart updateChartData:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]];
  169 + }
  170 + else if ([self.title isEqualToString:@"Circle Chart"])
  171 + {
  172 + [self.circleChart updateChartByCurrent:@(arc4random() % 100)];
  173 + }
  174 +
  175 +}
  176 +
  177 +- (void)userClickedOnBarAtIndex:(NSInteger)barIndex
  178 +{
  179 +
  180 + NSLog(@"Click on bar %@", @(barIndex));
  181 +
  182 + PNBar * bar = [self.barChart.bars objectAtIndex:barIndex];
  183 +
  184 + CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  185 +
  186 + animation.fromValue = @1.0;
  187 + animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  188 + animation.toValue = @1.1;
  189 + animation.duration = 0.2;
  190 + animation.repeatCount = 0;
  191 + animation.autoreverses = YES;
  192 + animation.removedOnCompletion = YES;
  193 + animation.fillMode = kCAFillModeForwards;
  194 +
  195 + [bar.layer addAnimation:animation forKey:@"Float"];
  196 +}
  197 +
13 @end 198 @end
@@ -7,11 +7,7 @@ @@ -7,11 +7,7 @@
7 // 7 //
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 -#import "PNChartDelegate.h"  
11 -#import "PNChart.h"  
12 10
13 -@interface PCChartsTableViewController : UITableViewController<PNChartDelegate> 11 +@interface PCChartsTableViewController : UITableViewController
14 -  
15 -@property (nonatomic) PNBarChart * barChart;  
16 12
17 @end 13 @end
@@ -21,47 +21,6 @@ @@ -21,47 +21,6 @@
21 if ([segue.identifier isEqualToString:@"lineChart"]) { 21 if ([segue.identifier isEqualToString:@"lineChart"]) {
22 22
23 //Add line chart 23 //Add line chart
24 - UILabel * lineChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];  
25 - lineChartLabel.text = @"Line Chart";  
26 - lineChartLabel.textColor = PNFreshGreen;  
27 - lineChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
28 - lineChartLabel.textAlignment = NSTextAlignmentCenter;  
29 -  
30 - PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];  
31 - lineChart.yLabelFormat = @"%1.1f";  
32 - lineChart.backgroundColor = [UIColor clearColor];  
33 - [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];  
34 - lineChart.showCoordinateAxis = YES;  
35 -  
36 - // Line Chart #1  
37 - NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2, @127.2, @176.2];  
38 - PNLineChartData *data01 = [PNLineChartData new];  
39 - data01.color = PNFreshGreen;  
40 - data01.itemCount = lineChart.xLabels.count;  
41 - data01.inflexionPointStyle = PNLineChartPointStyleTriangle;  
42 - data01.getData = ^(NSUInteger index) {  
43 - CGFloat yValue = [data01Array[index] floatValue];  
44 - return [PNLineChartDataItem dataItemWithY:yValue];  
45 - };  
46 -  
47 - // Line Chart #2  
48 - NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2];  
49 - PNLineChartData *data02 = [PNLineChartData new];  
50 - data02.color = PNTwitterColor;  
51 - data02.itemCount = lineChart.xLabels.count;  
52 - data02.inflexionPointStyle = PNLineChartPointStyleSquare;  
53 - data02.getData = ^(NSUInteger index) {  
54 - CGFloat yValue = [data02Array[index] floatValue];  
55 - return [PNLineChartDataItem dataItemWithY:yValue];  
56 - };  
57 -  
58 - lineChart.chartData = @[data01, data02];  
59 - [lineChart strokeChart];  
60 -  
61 - lineChart.delegate = self;  
62 -  
63 - [viewController.view addSubview:lineChartLabel];  
64 - [viewController.view addSubview:lineChart];  
65 24
66 viewController.title = @"Line Chart"; 25 viewController.title = @"Line Chart";
67 26
@@ -69,114 +28,21 @@ @@ -69,114 +28,21 @@
69 { 28 {
70 //Add bar chart 29 //Add bar chart
71 30
72 - UILabel * barChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];  
73 - barChartLabel.text = @"Bar Chart";  
74 - barChartLabel.textColor = PNFreshGreen;  
75 - barChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
76 - barChartLabel.textAlignment = NSTextAlignmentCenter;  
77 -  
78 - self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];  
79 - self.barChart.backgroundColor = [UIColor clearColor];  
80 - self.barChart.yLabelFormatter = ^(CGFloat yValue){  
81 - CGFloat yValueParsed = yValue;  
82 - NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed];  
83 - return labelText;  
84 - };  
85 - self.barChart.labelMarginTop = 5.0;  
86 - [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];  
87 - self.barChart.rotateForXAxisText = true ;  
88 - [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];  
89 - [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];  
90 - // Adding gradient  
91 - self.barChart.barColorGradientStart = [UIColor blueColor];  
92 -  
93 - [self.barChart strokeChart];  
94 -  
95 - self.barChart.delegate = self;  
96 -  
97 - [viewController.view addSubview:barChartLabel];  
98 - [viewController.view addSubview:self.barChart];  
99 -  
100 viewController.title = @"Bar Chart"; 31 viewController.title = @"Bar Chart";
101 } else if ([segue.identifier isEqualToString:@"circleChart"]) 32 } else if ([segue.identifier isEqualToString:@"circleChart"])
102 { 33 {
103 //Add circle chart 34 //Add circle chart
104 - UILabel * circleChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];  
105 - circleChartLabel.text = @"Circle Chart";  
106 - circleChartLabel.textColor = PNFreshGreen;  
107 - circleChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
108 - circleChartLabel.textAlignment = NSTextAlignmentCenter;  
109 35
110 - PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0)  
111 - total:@100  
112 - current:@60  
113 - clockwise:YES  
114 - shadow:YES];  
115 - circleChart.backgroundColor = [UIColor clearColor];  
116 - [circleChart setStrokeColor:PNGreen];  
117 - [circleChart setStrokeColorGradientStart:[UIColor blueColor]];  
118 - [circleChart strokeChart];  
119 -  
120 - [viewController.view addSubview:circleChartLabel];  
121 -  
122 - [viewController.view addSubview:circleChart];  
123 viewController.title = @"Circle Chart"; 36 viewController.title = @"Circle Chart";
124 37
125 } else if ([segue.identifier isEqualToString:@"pieChart"]) 38 } else if ([segue.identifier isEqualToString:@"pieChart"])
126 { 39 {
127 //Add pie chart 40 //Add pie chart
128 - UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];  
129 - pieChartLabel.text = @"Pie Chart";  
130 - pieChartLabel.textColor = PNFreshGreen;  
131 - pieChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
132 - pieChartLabel.textAlignment = NSTextAlignmentCenter;  
133 -  
134 - NSArray *items = @[[PNPieChartDataItem dataItemWithValue:10 color:PNLightGreen],  
135 - [PNPieChartDataItem dataItemWithValue:20 color:PNFreshGreen description:@"WWDC"],  
136 - [PNPieChartDataItem dataItemWithValue:40 color:PNDeepGreen description:@"GOOG I/O"],  
137 - ];  
138 -  
139 - PNPieChart *pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(SCREEN_WIDTH /2.0 - 100, 135, 200.0, 200.0) items:items];  
140 - pieChart.descriptionTextColor = [UIColor whiteColor];  
141 - pieChart.descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:11.0];  
142 - pieChart.descriptionTextShadowColor = [UIColor clearColor];  
143 - [pieChart strokeChart];  
144 -  
145 -  
146 - [viewController.view addSubview:pieChartLabel];  
147 - [viewController.view addSubview:pieChart];  
148 41
149 viewController.title = @"Pie Chart"; 42 viewController.title = @"Pie Chart";
150 } 43 }
151 } 44 }
152 45
153 -- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex{  
154 - NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);  
155 -}  
156 46
157 -- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{  
158 - NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);  
159 -}  
160 -  
161 -- (void)userClickedOnBarAtIndex:(NSInteger)barIndex  
162 -{  
163 -  
164 - NSLog(@"Click on bar %@", @(barIndex));  
165 -  
166 - PNBar * bar = [self.barChart.bars objectAtIndex:barIndex];  
167 -  
168 - CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];  
169 -  
170 - animation.fromValue = @1.0;  
171 - animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
172 - animation.toValue = @1.1;  
173 - animation.duration = 0.2;  
174 - animation.repeatCount = 0;  
175 - animation.autoreverses = YES;  
176 - animation.removedOnCompletion = YES;  
177 - animation.fillMode = kCAFillModeForwards;  
178 -  
179 - [bar.layer addAnimation:animation forKey:@"Float"];  
180 -}  
181 47
182 @end 48 @end