kevinzhow

Merge remote-tracking branch 'origin/develop'

Conflicts:
	PNChart.podspec
@@ -16,7 +16,7 @@ Pod::Spec.new do |s| @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
16 # 16 #
17 17
18 s.name = "PNChart" 18 s.name = "PNChart"
19 - s.version = "0.5.6" 19 + s.version = "0.6.0"
20 s.summary = "A simple and beautiful chart lib with animation used in Piner for iOS" 20 s.summary = "A simple and beautiful chart lib with animation used in Piner for iOS"
21 21
22 s.description = <<-DESC 22 s.description = <<-DESC
@@ -41,141 +41,6 @@ Pod::Spec.new do |s| @@ -41,141 +41,6 @@ Pod::Spec.new do |s|
41 41
42 You will need LLVM 3.0 or later in order to build PNChart. 42 You will need LLVM 3.0 or later in order to build PNChart.
43 43
44 -  
45 -  
46 -  
47 - ## Usage  
48 -  
49 - ### Cocoapods  
50 -  
51 - [CocoaPods](http://cocoapods.org) is the recommended way to add PNChart to your project.  
52 -  
53 - 1. Add a pod entry for PNChart to your Podfile `pod 'PNChart', '~> 0.5'`  
54 - 2. Install the pod(s) by running `pod install`.  
55 - 3. Include PNChart wherever you need it with `#import "PNChart.h"`.  
56 -  
57 -  
58 - ### Copy the PNChart folder to your project  
59 -  
60 -  
61 - [![](https://dl.dropboxusercontent.com/u/1599662/line.png)](https://dl.dropboxusercontent.com/u/1599662/line.png)  
62 -  
63 - ```objective-c  
64 - #import "PNChart.h"  
65 -  
66 - //For LineChart  
67 - PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];  
68 - [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];  
69 -  
70 - // Line Chart No.1  
71 - NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2];  
72 - PNLineChartData *data01 = [PNLineChartData new];  
73 - data01.color = PNFreshGreen;  
74 - data01.itemCount = lineChart.xLabels.count;  
75 - data01.getData = ^(NSUInteger index) {  
76 - CGFloat yValue = [data01Array[index] floatValue];  
77 - return [PNLineChartDataItem dataItemWithY:yValue];  
78 - };  
79 - // Line Chart No.2  
80 - NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2];  
81 - PNLineChartData *data02 = [PNLineChartData new];  
82 - data02.color = PNTwitterColor;  
83 - data02.itemCount = lineChart.xLabels.count;  
84 - data02.getData = ^(NSUInteger index) {  
85 - CGFloat yValue = [data02Array[index] floatValue];  
86 - return [PNLineChartDataItem dataItemWithY:yValue];  
87 - };  
88 -  
89 - lineChart.chartData = @[data01, data02];  
90 - [lineChart strokeChart];  
91 -  
92 - ```  
93 -  
94 - [![](https://dl.dropboxusercontent.com/u/1599662/bar.png)](https://dl.dropboxusercontent.com/u/1599662/bar.png)  
95 -  
96 - ```objective-c  
97 - #import "PNChart.h"  
98 -  
99 - //For BarChart  
100 - PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];  
101 - [barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];  
102 - [barChart setYValues:@[@1, @10, @2, @6, @3]];  
103 - [barChart strokeChart];  
104 -  
105 - ```  
106 -  
107 - [![](https://dl.dropboxusercontent.com/u/1599662/circle.png)](https://dl.dropboxusercontent.com/u/1599662/circle.png)  
108 -  
109 -  
110 - ```objective-c  
111 - #import "PNChart.h"  
112 -  
113 - //For CircleChart  
114 -  
115 - PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:60] andClockwise:NO];  
116 - circleChart.backgroundColor = [UIColor clearColor];  
117 - [circleChart setStrokeColor:PNGreen];  
118 - [circleChart strokeChart];  
119 -  
120 - ```  
121 -  
122 -  
123 - [![](https://dl.dropboxusercontent.com/u/1599662/pie.png)](https://dl.dropboxusercontent.com/u/1599662/pie.png)  
124 -  
125 - ```objective-c  
126 - # import "PNChart.h"  
127 - //For PieChart  
128 - NSArray *items = @[[PNPieChartDataItem dataItemWithValue:10 color:PNRed],  
129 - [PNPieChartDataItem dataItemWithValue:20 color:PNBlue description:@"WWDC"],  
130 - [PNPieChartDataItem dataItemWithValue:40 color:PNGreen description:@"GOOL I/O"],  
131 - ];  
132 -  
133 -  
134 -  
135 - PNPieChart *pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(40.0, 155.0, 240.0, 240.0) items:items];  
136 - pieChart.descriptionTextColor = [UIColor whiteColor];  
137 - pieChart.descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:14.0];  
138 - [pieChart strokeChart];  
139 - ```  
140 -  
141 - #### Callback  
142 -  
143 - Currently callback only works on Linechart  
144 -  
145 - ```objective-c  
146 - #import "PNChart.h"  
147 -  
148 - //For LineChart  
149 -  
150 - lineChart.delegate = self;  
151 -  
152 -  
153 - ```  
154 -  
155 - ```objective-c  
156 -  
157 - //For DelegateMethod  
158 -  
159 -  
160 - -(void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex{  
161 - NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);  
162 - }  
163 -  
164 - -(void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{  
165 - NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);  
166 - }  
167 -  
168 - ```  
169 -  
170 -  
171 - ## License  
172 -  
173 - This code is distributed under the terms and conditions of the [MIT license](LICENSE).  
174 -  
175 - ## SpecialThanks  
176 -  
177 - [@lexrus](http://twitter.com/lexrus) CocoaPods Spec  
178 -  
179 DESC 44 DESC
180 45
181 s.homepage = "https://github.com/kevinzhow/PNChart" 46 s.homepage = "https://github.com/kevinzhow/PNChart"
@@ -225,7 +90,7 @@ Pod::Spec.new do |s| @@ -225,7 +90,7 @@ Pod::Spec.new do |s|
225 # Supports git, hg, bzr, svn and HTTP. 90 # Supports git, hg, bzr, svn and HTTP.
226 # 91 #
227 92
228 - s.source = { :git => "https://github.com/kevinzhow/PNChart.git", :tag => "0.5.6" } 93 + s.source = { :git => "https://github.com/kevinzhow/PNChart.git", :tag => "0.6.0" }
229 94
230 95
231 # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 96 # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
@@ -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,45 +56,69 @@ @@ -55,45 +56,69 @@
55 _chartLine.strokeColor = [PNGreen CGColor]; 56 _chartLine.strokeColor = [PNGreen CGColor];
56 } 57 }
57 58
58 - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 59 + if (_grade) {
59 - pathAnimation.duration = 1.0; 60 +
60 - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 61 + CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
61 - pathAnimation.fromValue = @0.0f; 62 + pathAnimation.fromValue = (id)_chartLine.path;
62 - pathAnimation.toValue = @1.0f; 63 + pathAnimation.toValue = (id)[progressline CGPath];
63 - [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 64 + pathAnimation.duration = 0.5f;
64 - 65 + pathAnimation.autoreverses = NO;
65 - _chartLine.strokeEnd = 1.0; 66 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
66 - 67 + [_chartLine addAnimation:pathAnimation forKey:@"animationKey"];
67 - // Check if user wants to add a gradient from the start color to the bar color 68 +
68 - if (_barColorGradientStart) { 69 + _chartLine.path = progressline.CGPath;
69 - 70 +
70 - // Add gradient 71 + if (_barColorGradientStart) {
71 - CAShapeLayer *gradientMask = [CAShapeLayer layer]; 72 +
72 - gradientMask.fillColor = [[UIColor clearColor] CGColor]; 73 + // Add gradient
73 - gradientMask.strokeColor = [[UIColor blackColor] CGColor]; 74 + [self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"];
74 - gradientMask.lineWidth = self.frame.size.width; 75 + self.gradientMask.path = progressline.CGPath;
75 - gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 76 + }
76 - gradientMask.path = progressline.CGPath; 77 +
77 - 78 + }else{
78 - 79 + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
79 - CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 80 + pathAnimation.duration = 1.0;
80 - gradientLayer.startPoint = CGPointMake(0.5,1.0); 81 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
81 - gradientLayer.endPoint = CGPointMake(0.5,0.0); 82 + pathAnimation.fromValue = @0.0f;
82 - gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 83 + pathAnimation.toValue = @1.0f;
83 - UIColor *endColor = (_barColor ? _barColor : [UIColor greenColor]); 84 + [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
84 - NSArray *colors = @[ 85 +
85 - (id)_barColorGradientStart.CGColor, 86 + _chartLine.strokeEnd = 1.0;
86 - (id)endColor.CGColor 87 +
87 - ]; 88 + _chartLine.path = progressline.CGPath;
88 - gradientLayer.colors = colors; 89 + // Check if user wants to add a gradient from the start color to the bar color
89 - 90 + if (_barColorGradientStart) {
90 - [gradientLayer setMask:gradientMask]; 91 +
91 - 92 + // Add gradient
92 - [_chartLine addSublayer:gradientLayer]; 93 + self.gradientMask = [CAShapeLayer layer];
93 - 94 + self.gradientMask.fillColor = [[UIColor clearColor] CGColor];
94 - gradientMask.strokeEnd = 1.0; 95 + self.gradientMask.strokeColor = [[UIColor blackColor] CGColor];
95 - [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 96 + self.gradientMask.lineWidth = self.frame.size.width;
  97 + self.gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  98 + self.gradientMask.path = progressline.CGPath;
  99 +
  100 +
  101 + CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  102 + gradientLayer.startPoint = CGPointMake(0.5,1.0);
  103 + gradientLayer.endPoint = CGPointMake(0.5,0.0);
  104 + gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  105 + UIColor *endColor = (_barColor ? _barColor : [UIColor greenColor]);
  106 + NSArray *colors = @[
  107 + (id)_barColorGradientStart.CGColor,
  108 + (id)endColor.CGColor
  109 + ];
  110 + gradientLayer.colors = colors;
  111 +
  112 + [gradientLayer setMask:self.gradientMask];
  113 +
  114 + [_chartLine addSublayer:gradientLayer];
  115 +
  116 + self.gradientMask.strokeEnd = 1.0;
  117 + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  118 + }
96 } 119 }
  120 +
  121 + _grade = grade;
97 122
98 } 123 }
99 124
@@ -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;
@@ -57,9 +59,41 @@ @@ -57,9 +59,41 @@
57 } else { 59 } else {
58 [self getYValueMax:yValues]; 60 [self getYValueMax:yValues];
59 } 61 }
  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;
  73 +
  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 +}
60 93
61 - 94 +-(void)updateChartData:(NSArray *)data{
62 - _xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [_yValues count]; 95 + self.yValues = data;
  96 + [self updateBar];
63 } 97 }
64 98
65 - (void)getYValueMax:(NSArray *)yLabels 99 - (void)getYValueMax:(NSArray *)yLabels
@@ -76,29 +110,19 @@ @@ -76,29 +110,19 @@
76 - (void)setXLabels:(NSArray *)xLabels 110 - (void)setXLabels:(NSArray *)xLabels
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 -} 119 +
84 -  
85 -  
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;
101 - 125 +
102 if (labelAddCount == _xLabelSkip) { 126 if (labelAddCount == _xLabelSkip) {
103 NSString *labelText = [_xLabels[index] description]; 127 NSString *labelText = [_xLabels[index] description];
104 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectZero]; 128 PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectZero];
@@ -118,106 +142,104 @@ @@ -118,106 +142,104 @@
118 label.center = CGPointMake(labelXPosition, 142 label.center = CGPointMake(labelXPosition,
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
152 - 155 +{
  156 + _strokeColor = strokeColor;
  157 +}
153 158
  159 +- (void)updateBar
  160 +{
  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]; 167 +
160 -  
161 - float grade = (float)value / (float)_yValueMax;  
162 -  
163 - if (isnan(grade)) {  
164 - grade = 0;  
165 - }  
166 -  
167 PNBar *bar; 168 PNBar *bar;
168 - CGFloat barWidth; 169 +
169 - CGFloat barXPosition; 170 + if (_bars.count == _yValues.count) {
170 - 171 + bar = [_bars objectAtIndex:index];
171 - if (_barWidth) {  
172 - barWidth = _barWidth;  
173 - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 - _barWidth /2.0;  
174 }else{ 172 }else{
175 - barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth * 0.25; 173 + CGFloat barWidth;
176 - if (_showLabel) { 174 + CGFloat barXPosition;
177 - barWidth = _xLabelWidth * 0.5; 175 +
178 - 176 + if (_barWidth) {
  177 + barWidth = _barWidth;
  178 + barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth /2.0 - _barWidth /2.0;
  179 + }else{
  180 + barXPosition = index * _xLabelWidth + _chartMargin + _xLabelWidth * 0.25;
  181 + if (_showLabel) {
  182 + barWidth = _xLabelWidth * 0.5;
  183 +
  184 + }
  185 + else {
  186 + barWidth = _xLabelWidth * 0.6;
  187 +
  188 + }
179 } 189 }
180 - else { 190 +
181 - barWidth = _xLabelWidth * 0.6; 191 + bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position
182 - 192 + self.frame.size.height - chartCavanHeight - xLabelHeight - _chartMargin, //Bar Y position
  193 + barWidth, // Bar witdh
  194 + chartCavanHeight)]; //Bar height
  195 +
  196 + //Change Bar Radius
  197 + bar.barRadius = _barRadius;
  198 +
  199 + //Change Bar Background color
  200 + bar.backgroundColor = _barBackgroundColor;
  201 +
  202 + //Bar StrokColor First
  203 + if (self.strokeColor) {
  204 + bar.barColor = self.strokeColor;
  205 + }else{
  206 + bar.barColor = [self barColorAtIndex:index];
183 } 207 }
  208 + // Add gradient
  209 + bar.barColorGradientStart = _barColorGradientStart;
  210 +
  211 + //For Click Index
  212 + bar.tag = index;
  213 +
  214 + [_bars addObject:bar];
  215 + [self addSubview:bar];
184 } 216 }
185 - 217 +
186 - bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position  
187 - self.frame.size.height - chartCavanHeight - xLabelHeight - _chartMargin, //Bar Y position  
188 - barWidth, // Bar witdh  
189 - chartCavanHeight)]; //Bar height  
190 -  
191 - //Change Bar Radius  
192 - bar.barRadius = _barRadius;  
193 -  
194 - //Change Bar Background color  
195 - bar.backgroundColor = _barBackgroundColor;  
196 -  
197 - //Bar StrokColor First  
198 - if (self.strokeColor) {  
199 - bar.barColor = self.strokeColor;  
200 - }else{  
201 - bar.barColor = [self barColorAtIndex:index];  
202 - }  
203 -  
204 //Height Of Bar 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 + }
205 bar.grade = grade; 226 bar.grade = grade;
  227 +
  228 + index += 1;
  229 + }
  230 +}
206 231
207 - // Add gradient 232 +- (void)strokeChart
208 - bar.barColorGradientStart = _barColorGradientStart; 233 +{
209 - 234 + //Add Labels
210 -  
211 - //For Click Index  
212 - bar.tag = index;  
213 -  
214 235
215 - [_bars addObject:bar]; 236 + [self viewCleanupForCollection:_bars];
216 - [self addSubview:bar];  
217 237
218 - index += 1;  
219 - }  
220 238
  239 + //Update Bar
  240 +
  241 + [self updateBar];
  242 +
221 //Add chart border lines 243 //Add chart border lines
222 244
223 if (_showChartBorder) { 245 if (_showChartBorder) {
@@ -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
@@ -28,7 +28,7 @@ You will need LLVM 3.0 or later in order to build PNChart. @@ -28,7 +28,7 @@ You will need LLVM 3.0 or later in order to build PNChart.
28 28
29 [CocoaPods](http://cocoapods.org) is the recommended way to add PNChart to your project. 29 [CocoaPods](http://cocoapods.org) is the recommended way to add PNChart to your project.
30 30
31 -1. Add a pod entry for PNChart to your Podfile `pod 'PNChart', '~> 0.5.5'` 31 +1. Add a pod entry for PNChart to your Podfile `pod 'PNChart', '~> 0.6.0'`
32 2. Install the pod(s) by running `pod install`. 32 2. Install the pod(s) by running `pod install`.
33 3. Include PNChart wherever you need it with `#import "PNChart.h"`. 33 3. Include PNChart wherever you need it with `#import "PNChart.h"`.
34 34
@@ -116,12 +116,54 @@ pieChart.descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:14.0] @@ -116,12 +116,54 @@ pieChart.descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:14.0]
116 [pieChart strokeChart]; 116 [pieChart strokeChart];
117 ``` 117 ```
118 118
119 -#### Callback 119 +#### Update Value
120 120
121 -Currently callback only works on Linechart 121 +Now it's easy to update value in real time
122 122
123 ```objective-c 123 ```objective-c
124 - #import "PNChart.h" 124 +if ([self.title isEqualToString:@"Line Chart"]) {
  125 +
  126 + // Line Chart #1
  127 + NSArray * data01Array = @[@(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300)];
  128 + PNLineChartData *data01 = [PNLineChartData new];
  129 + data01.color = PNFreshGreen;
  130 + data01.itemCount = data01Array.count;
  131 + data01.inflexionPointStyle = PNLineChartPointStyleTriangle;
  132 + data01.getData = ^(NSUInteger index) {
  133 + CGFloat yValue = [data01Array[index] floatValue];
  134 + return [PNLineChartDataItem dataItemWithY:yValue];
  135 + };
  136 +
  137 + // Line Chart #2
  138 + NSArray * data02Array = @[@(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300), @(arc4random() % 300)];
  139 + PNLineChartData *data02 = [PNLineChartData new];
  140 + data02.color = PNTwitterColor;
  141 + data02.itemCount = data02Array.count;
  142 + data02.inflexionPointStyle = PNLineChartPointStyleSquare;
  143 + data02.getData = ^(NSUInteger index) {
  144 + CGFloat yValue = [data02Array[index] floatValue];
  145 + return [PNLineChartDataItem dataItemWithY:yValue];
  146 + };
  147 +
  148 + [self.lineChart setXLabels:@[@"DEC 1",@"DEC 2",@"DEC 3",@"DEC 4",@"DEC 5",@"DEC 6",@"DEC 7"]];
  149 + [self.lineChart updateChartData:@[data01, data02]];
  150 +
  151 +}
  152 +else if ([self.title isEqualToString:@"Bar Chart"])
  153 +{
  154 + [self.barChart setXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]];
  155 + [self.barChart updateChartData:@[@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30),@(arc4random() % 30)]];
  156 +}
  157 +else if ([self.title isEqualToString:@"Circle Chart"])
  158 +{
  159 + [self.circleChart updateChartByCurrent:@(arc4random() % 100)];
  160 +}
  161 +```
  162 +
  163 +#### Callback
  164 +
  165 +```objective-c
  166 +#import "PNChart.h"
125 167
126 //For LineChart 168 //For LineChart
127 169