kevinzhow

Add circle chart change value

@@ -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
@@ -26,14 +26,25 @@ @@ -26,14 +26,25 @@
26 <action selector="changeValue:" destination="Tha-Wr-sPW" eventType="touchUpInside" id="zeG-Cp-Wjs"/> 26 <action selector="changeValue:" destination="Tha-Wr-sPW" eventType="touchUpInside" id="zeG-Cp-Wjs"/>
27 </connections> 27 </connections>
28 </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>
29 </subviews> 35 </subviews>
30 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 36 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
31 <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"/>
32 <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"/>
33 <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"/>
34 </constraints> 42 </constraints>
35 </view> 43 </view>
36 <navigationItem key="navigationItem" title="PNChart" id="Ukg-Sg-E2z"/> 44 <navigationItem key="navigationItem" title="PNChart" id="Ukg-Sg-E2z"/>
  45 + <connections>
  46 + <outlet property="titleLabel" destination="FUU-vZ-jMd" id="dA3-KC-Ht4"/>
  47 + </connections>
37 </viewController> 48 </viewController>
38 <placeholder placeholderIdentifier="IBFirstResponder" id="kDa-u3-t6i" userLabel="First Responder" sceneMemberID="firstResponder"/> 49 <placeholder placeholderIdentifier="IBFirstResponder" id="kDa-u3-t6i" userLabel="First Responder" sceneMemberID="firstResponder"/>
39 </objects> 50 </objects>
@@ -14,6 +14,8 @@ @@ -14,6 +14,8 @@
14 14
15 @property (nonatomic) PNLineChart * lineChart; 15 @property (nonatomic) PNLineChart * lineChart;
16 @property (nonatomic) PNBarChart * barChart; 16 @property (nonatomic) PNBarChart * barChart;
  17 +@property (nonatomic) PNCircleChart * circleChart;
  18 +@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
17 19
18 - (IBAction)changeValue:(id)sender; 20 - (IBAction)changeValue:(id)sender;
19 21
@@ -13,13 +13,11 @@ @@ -13,13 +13,11 @@
13 -(void)viewDidLoad 13 -(void)viewDidLoad
14 { 14 {
15 [super viewDidLoad]; 15 [super viewDidLoad];
  16 + self.titleLabel.textColor = PNFreshGreen;
16 17
17 if ([self.title isEqualToString:@"Line Chart"]) { 18 if ([self.title isEqualToString:@"Line Chart"]) {
18 - UILabel * lineChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)]; 19 +
19 - lineChartLabel.text = @"Line Chart"; 20 + self.titleLabel.text = @"Line Chart";
20 - lineChartLabel.textColor = PNFreshGreen;  
21 - lineChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
22 - lineChartLabel.textAlignment = NSTextAlignmentCenter;  
23 21
24 self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; 22 self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
25 self.lineChart.yLabelFormat = @"%1.1f"; 23 self.lineChart.yLabelFormat = @"%1.1f";
@@ -58,16 +56,12 @@ @@ -58,16 +56,12 @@
58 [self.lineChart strokeChart]; 56 [self.lineChart strokeChart];
59 self.lineChart.delegate = self; 57 self.lineChart.delegate = self;
60 58
61 - [self.view addSubview:lineChartLabel]; 59 +
62 [self.view addSubview:self.lineChart]; 60 [self.view addSubview:self.lineChart];
63 } 61 }
64 else if ([self.title isEqualToString:@"Bar Chart"]) 62 else if ([self.title isEqualToString:@"Bar Chart"])
65 { 63 {
66 - UILabel * barChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)]; 64 + self.titleLabel.text = @"Bar Chart";
67 - barChartLabel.text = @"Bar Chart";  
68 - barChartLabel.textColor = PNFreshGreen;  
69 - barChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
70 - barChartLabel.textAlignment = NSTextAlignmentCenter;  
71 65
72 self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; 66 self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
73 self.barChart.backgroundColor = [UIColor clearColor]; 67 self.barChart.backgroundColor = [UIColor clearColor];
@@ -88,9 +82,25 @@ @@ -88,9 +82,25 @@
88 82
89 self.barChart.delegate = self; 83 self.barChart.delegate = self;
90 84
91 - [self.view addSubview:barChartLabel];  
92 [self.view addSubview:self.barChart]; 85 [self.view addSubview:self.barChart];
93 } 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 + }
94 } 104 }
95 105
96 106
@@ -138,6 +148,10 @@ @@ -138,6 +148,10 @@
138 [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]]; 148 [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]];
139 [self.barChart updateChartData:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]]; 149 [self.barChart updateChartData:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]];
140 } 150 }
  151 + else if ([self.title isEqualToString:@"Circle Chart"])
  152 + {
  153 + [self.circleChart updateChartByCurrent:@(arc4random() % 100)];
  154 + }
141 155
142 } 156 }
143 157
@@ -32,25 +32,7 @@ @@ -32,25 +32,7 @@
32 } else if ([segue.identifier isEqualToString:@"circleChart"]) 32 } else if ([segue.identifier isEqualToString:@"circleChart"])
33 { 33 {
34 //Add circle chart 34 //Add circle chart
35 - UILabel * circleChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)]; 35 +
36 - circleChartLabel.text = @"Circle Chart";  
37 - circleChartLabel.textColor = PNFreshGreen;  
38 - circleChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
39 - circleChartLabel.textAlignment = NSTextAlignmentCenter;  
40 -  
41 - PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0)  
42 - total:@100  
43 - current:@60  
44 - clockwise:YES  
45 - shadow:YES];  
46 - circleChart.backgroundColor = [UIColor clearColor];  
47 - [circleChart setStrokeColor:PNGreen];  
48 - [circleChart setStrokeColorGradientStart:[UIColor blueColor]];  
49 - [circleChart strokeChart];  
50 -  
51 - [viewController.view addSubview:circleChartLabel];  
52 -  
53 - [viewController.view addSubview:circleChart];  
54 viewController.title = @"Circle Chart"; 36 viewController.title = @"Circle Chart";
55 37
56 } else if ([segue.identifier isEqualToString:@"pieChart"]) 38 } else if ([segue.identifier isEqualToString:@"pieChart"])