kevinzhow

add highlight on bar animation #58

@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 #import "PNChartDelegate.h" 10 #import "PNChartDelegate.h"
  11 +#import "PNBar.h"
11 12
12 #define xLabelMargin 15 13 #define xLabelMargin 15
13 #define yLabelMargin 15 14 #define yLabelMargin 15
@@ -27,6 +28,9 @@ typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue); @@ -27,6 +28,9 @@ typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue);
27 @property (nonatomic) NSArray *xLabels; 28 @property (nonatomic) NSArray *xLabels;
28 @property (nonatomic) NSArray *yLabels; 29 @property (nonatomic) NSArray *yLabels;
29 @property (nonatomic) NSArray *yValues; 30 @property (nonatomic) NSArray *yValues;
  31 +
  32 +@property (nonatomic) NSMutableArray * bars;
  33 +
30 @property (nonatomic) CGFloat xLabelWidth; 34 @property (nonatomic) CGFloat xLabelWidth;
31 @property (nonatomic) int yValueMax; 35 @property (nonatomic) int yValueMax;
32 @property (nonatomic) UIColor *strokeColor; 36 @property (nonatomic) UIColor *strokeColor;
@@ -9,10 +9,9 @@ @@ -9,10 +9,9 @@
9 #import "PNBarChart.h" 9 #import "PNBarChart.h"
10 #import "PNColor.h" 10 #import "PNColor.h"
11 #import "PNChartLabel.h" 11 #import "PNChartLabel.h"
12 -#import "PNBar.h" 12 +
13 13
14 @interface PNBarChart () { 14 @interface PNBarChart () {
15 - NSMutableArray *_bars;  
16 NSMutableArray *_labels; 15 NSMutableArray *_labels;
17 } 16 }
18 17
@@ -8,7 +8,10 @@ @@ -8,7 +8,10 @@
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 #import "PNChartDelegate.h" 10 #import "PNChartDelegate.h"
  11 +#import "PNChart.h"
11 12
12 @interface PCChartsTableViewController : UITableViewController<PNChartDelegate> 13 @interface PCChartsTableViewController : UITableViewController<PNChartDelegate>
13 14
  15 +@property (nonatomic) PNBarChart * barChart;
  16 +
14 @end 17 @end
@@ -105,23 +105,23 @@ @@ -105,23 +105,23 @@
105 barChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0]; 105 barChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];
106 barChartLabel.textAlignment = NSTextAlignmentCenter; 106 barChartLabel.textAlignment = NSTextAlignmentCenter;
107 107
108 - PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; 108 + self.barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
109 - barChart.backgroundColor = [UIColor clearColor]; 109 + self.barChart.backgroundColor = [UIColor clearColor];
110 - barChart.yLabelFormatter = ^(CGFloat yValue){ 110 + self.barChart.yLabelFormatter = ^(CGFloat yValue){
111 CGFloat yValueParsed = yValue; 111 CGFloat yValueParsed = yValue;
112 NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed]; 112 NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed];
113 return labelText; 113 return labelText;
114 }; 114 };
115 - barChart.labelMarginTop = 5.0; 115 + self.barChart.labelMarginTop = 5.0;
116 - [barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; 116 + [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
117 - [barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; 117 + [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
118 - [barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; 118 + [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
119 - [barChart strokeChart]; 119 + [self.barChart strokeChart];
120 120
121 - barChart.delegate = self; 121 + self.barChart.delegate = self;
122 122
123 [viewController.view addSubview:barChartLabel]; 123 [viewController.view addSubview:barChartLabel];
124 - [viewController.view addSubview:barChart]; 124 + [viewController.view addSubview:self.barChart];
125 125
126 viewController.title = @"Bar Chart"; 126 viewController.title = @"Bar Chart";
127 }else if ([segue.identifier isEqualToString:@"circleChart"]) 127 }else if ([segue.identifier isEqualToString:@"circleChart"])
@@ -191,7 +191,30 @@ @@ -191,7 +191,30 @@
191 191
192 - (void)userClickedOnBarCharIndex:(NSInteger)barIndex 192 - (void)userClickedOnBarCharIndex:(NSInteger)barIndex
193 { 193 {
  194 +
194 NSLog(@"Click on bar %@", @(barIndex)); 195 NSLog(@"Click on bar %@", @(barIndex));
  196 +
  197 + PNBar * bar = [self.barChart.bars objectAtIndex:barIndex];
  198 +
  199 + CABasicAnimation *animation= [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  200 +
  201 + animation.fromValue= @1.0;
  202 +
  203 + animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  204 +
  205 + animation.toValue= @1.1;
  206 +
  207 + animation.duration= 0.2;
  208 +
  209 + animation.repeatCount = 0;
  210 +
  211 + animation.autoreverses = YES;
  212 +
  213 + animation.removedOnCompletion = YES;
  214 +
  215 + animation.fillMode=kCAFillModeForwards;
  216 +
  217 + [bar.layer addAnimation:animation forKey:@"Float"];
195 } 218 }
196 219
197 @end 220 @end