Kevin

Merge pull request #83 from moflo/master

Add gradient option to bar & circle charts
@@ -11,12 +11,12 @@ Pod::Spec.new do |s| @@ -11,12 +11,12 @@ Pod::Spec.new do |s|
11 s.author = { "Kevin" => "kevinchou.c@gmail.com" } 11 s.author = { "Kevin" => "kevinchou.c@gmail.com" }
12 12
13 s.platform = :ios, '6.0' 13 s.platform = :ios, '6.0'
14 - s.source = { :git => "https://github.com/kevinzhow/PNChart.git" } 14 + s.source = { :git => "https://github.com/moflo/PNChart.git" }
15 15
16 s.ios.dependency 'UICountingLabel', '~> 1.0.0' 16 s.ios.dependency 'UICountingLabel', '~> 1.0.0'
17 17
18 - s.source_files = 'PNChart/*.{h,m}' 18 + s.source_files = 'PNChart/**/*.{h,m}'
19 - s.public_header_files = 'PNChart/*.h' 19 + s.public_header_files = 'PNChart/**/*.h'
20 s.frameworks = 'CoreGraphics', 'UIKit', 'Foundation', 'QuartzCore' 20 s.frameworks = 'CoreGraphics', 'UIKit', 'Foundation', 'QuartzCore'
21 s.requires_arc = true 21 s.requires_arc = true
22 end 22 end
@@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
16 @property (nonatomic) float grade; 16 @property (nonatomic) float grade;
17 @property (nonatomic) CAShapeLayer *chartLine; 17 @property (nonatomic) CAShapeLayer *chartLine;
18 @property (nonatomic) UIColor *barColor; 18 @property (nonatomic) UIColor *barColor;
  19 +@property (nonatomic) UIColor *barColorGradientStart;
19 @property (nonatomic) CGFloat barRadius; 20 @property (nonatomic) CGFloat barRadius;
20 21
21 @end 22 @end
@@ -64,6 +64,39 @@ @@ -64,6 +64,39 @@
64 [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 64 [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
65 65
66 _chartLine.strokeEnd = 1.0; 66 _chartLine.strokeEnd = 1.0;
  67 +
  68 + // Check if user wants to add a gradient from the start color to the bar color
  69 + if (_barColorGradientStart) {
  70 +
  71 + // Add gradient
  72 + CAShapeLayer *gradientMask = [CAShapeLayer layer];
  73 + gradientMask.fillColor = [[UIColor clearColor] CGColor];
  74 + gradientMask.strokeColor = [[UIColor blackColor] CGColor];
  75 + //gradientMask.lineWidth = 4;
  76 + gradientMask.lineWidth = self.frame.size.width;
  77 + gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  78 + gradientMask.path = progressline.CGPath;
  79 +
  80 +
  81 + CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  82 + gradientLayer.startPoint = CGPointMake(0.5,1.0);
  83 + gradientLayer.endPoint = CGPointMake(0.5,0.0);
  84 + gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  85 + UIColor *endColor = (_barColor ? _barColor : [UIColor greenColor]);
  86 + NSArray *colors = @[
  87 + (id)_barColorGradientStart.CGColor,
  88 + (id)endColor.CGColor
  89 + ];
  90 + gradientLayer.colors = colors;
  91 +
  92 + [gradientLayer setMask:gradientMask];
  93 +
  94 + [_chartLine addSublayer:gradientLayer];
  95 +
  96 + gradientMask.strokeEnd = 1.0;
  97 + [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  98 + }
  99 +
67 } 100 }
68 101
69 102
@@ -74,6 +107,17 @@ @@ -74,6 +107,17 @@
74 } completion:nil]; 107 } completion:nil];
75 } 108 }
76 109
  110 +- (void)setBarColorGradientStart:(UIColor *)barColorGradientStart
  111 +{
  112 + // Set gradient color, remove any existing sublayer first
  113 + for (CALayer *sublayer in [_chartLine sublayers]) {
  114 + [sublayer removeFromSuperlayer];
  115 + }
  116 + _barColorGradientStart = barColorGradientStart;
  117 +
  118 + [self setGrade:_grade];
  119 +
  120 +}
77 121
78 // Only override drawRect: if you perform custom drawing. 122 // Only override drawRect: if you perform custom drawing.
79 // An empty implementation adversely affects performance during animation. 123 // An empty implementation adversely affects performance during animation.
@@ -123,6 +123,12 @@ typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue); @@ -123,6 +123,12 @@ typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue);
123 */ 123 */
124 @property (nonatomic) CGFloat yMinValue; 124 @property (nonatomic) CGFloat yMinValue;
125 125
  126 +/*
  127 + switch to indicate that the bar should be filled as a gradient
  128 + */
  129 +@property (nonatomic) UIColor *barColorGradientStart;
  130 +
  131 +
126 @property (nonatomic, retain) id<PNChartDelegate> delegate; 132 @property (nonatomic, retain) id<PNChartDelegate> delegate;
127 133
128 @end 134 @end
@@ -205,9 +205,14 @@ @@ -205,9 +205,14 @@
205 //Height Of Bar 205 //Height Of Bar
206 bar.grade = grade; 206 bar.grade = grade;
207 207
  208 + // Add gradient
  209 + bar.barColorGradientStart = _barColorGradientStart;
  210 +
  211 +
208 //For Click Index 212 //For Click Index
209 bar.tag = index; 213 bar.tag = index;
210 214
  215 +
211 [_bars addObject:bar]; 216 [_bars addObject:bar];
212 [self addSubview:bar]; 217 [self addSubview:bar];
213 218
@@ -26,6 +26,7 @@ typedef NS_ENUM(NSUInteger, PNChartFormatType) { @@ -26,6 +26,7 @@ typedef NS_ENUM(NSUInteger, PNChartFormatType) {
26 26
27 @property (strong, nonatomic) UICountingLabel *countingLabel; 27 @property (strong, nonatomic) UICountingLabel *countingLabel;
28 @property (nonatomic) UIColor *strokeColor; 28 @property (nonatomic) UIColor *strokeColor;
  29 +@property (nonatomic) UIColor *strokeColorGradientStart;
29 @property (nonatomic) NSNumber *total; 30 @property (nonatomic) NSNumber *total;
30 @property (nonatomic) NSNumber *current; 31 @property (nonatomic) NSNumber *current;
31 @property (nonatomic) NSNumber *lineWidth; 32 @property (nonatomic) NSNumber *lineWidth;
@@ -101,6 +101,41 @@ @@ -101,6 +101,41 @@
101 _circle.strokeEnd = [_current floatValue] / [_total floatValue]; 101 _circle.strokeEnd = [_current floatValue] / [_total floatValue];
102 102
103 [_countingLabel countFrom:0 to:[_current floatValue] withDuration:1.0]; 103 [_countingLabel countFrom:0 to:[_current floatValue] withDuration:1.0];
  104 +
  105 +
  106 + // Check if user wants to add a gradient from the start color to the bar color
  107 + if (_strokeColorGradientStart) {
  108 +
  109 + // Add gradient
  110 + CAShapeLayer *gradientMask = [CAShapeLayer layer];
  111 + gradientMask.fillColor = [[UIColor clearColor] CGColor];
  112 + gradientMask.strokeColor = [[UIColor blackColor] CGColor];
  113 + gradientMask.lineWidth = _circle.lineWidth;
  114 + gradientMask.lineCap = kCALineCapRound;
  115 + CGRect gradientFrame = CGRectMake(0, 0, 2*self.bounds.size.width, 2*self.bounds.size.height);
  116 + gradientMask.frame = gradientFrame;
  117 + gradientMask.path = _circle.path;
  118 +
  119 + CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  120 + gradientLayer.startPoint = CGPointMake(0.5,1.0);
  121 + gradientLayer.endPoint = CGPointMake(0.5,0.0);
  122 + gradientLayer.frame = gradientFrame;
  123 + UIColor *endColor = (_strokeColor ? _strokeColor : [UIColor greenColor]);
  124 + NSArray *colors = @[
  125 + (id)endColor.CGColor,
  126 + (id)_strokeColorGradientStart.CGColor
  127 + ];
  128 + gradientLayer.colors = colors;
  129 +
  130 + [gradientLayer setMask:gradientMask];
  131 +
  132 + [_circle addSublayer:gradientLayer];
  133 +
  134 + gradientMask.strokeEnd = [_current floatValue] / [_total floatValue];
  135 +
  136 + [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  137 + }
  138 +
104 } 139 }
105 140
106 141
@@ -67,6 +67,7 @@ @@ -67,6 +67,7 @@
67 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum; 67 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
68 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; 68 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
69 69
  70 + if (_showLabel) {
70 71
71 NSInteger index = 0; 72 NSInteger index = 0;
72 NSInteger num = _yLabelNum + 1; 73 NSInteger num = _yLabelNum + 1;
@@ -80,6 +81,7 @@ @@ -80,6 +81,7 @@
80 index += 1; 81 index += 1;
81 num -= 1; 82 num -= 1;
82 } 83 }
  84 + }
83 } 85 }
84 86
85 87
This diff is collapsed. Click to expand it.
@@ -119,8 +119,13 @@ @@ -119,8 +119,13 @@
119 [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; 119 [self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
120 [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; 120 [self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
121 [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; 121 [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
  122 + // Adding gradient
  123 + self.barChart.barColorGradientStart = [UIColor blueColor];
  124 +
122 [self.barChart strokeChart]; 125 [self.barChart strokeChart];
123 126
  127 +
  128 +
124 self.barChart.delegate = self; 129 self.barChart.delegate = self;
125 130
126 [viewController.view addSubview:barChartLabel]; 131 [viewController.view addSubview:barChartLabel];
@@ -142,6 +147,7 @@ @@ -142,6 +147,7 @@
142 PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:@100 andCurrent:@60 andClockwise:YES andShadow:YES]; 147 PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:@100 andCurrent:@60 andClockwise:YES andShadow:YES];
143 circleChart.backgroundColor = [UIColor clearColor]; 148 circleChart.backgroundColor = [UIColor clearColor];
144 [circleChart setStrokeColor:PNGreen]; 149 [circleChart setStrokeColor:PNGreen];
  150 + [circleChart setStrokeColorGradientStart:[UIColor blueColor]];
145 [circleChart strokeChart]; 151 [circleChart strokeChart];
146 152
147 [viewController.view addSubview:circleChartLabel]; 153 [viewController.view addSubview:circleChartLabel];
@@ -152,7 +158,7 @@ @@ -152,7 +158,7 @@
152 }else if ([segue.identifier isEqualToString:@"pieChart"]) 158 }else if ([segue.identifier isEqualToString:@"pieChart"])
153 { 159 {
154 160
155 - //Add LineChart 161 + //Add PieChart
156 UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)]; 162 UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];
157 pieChartLabel.text = @"Pie Chart"; 163 pieChartLabel.text = @"Pie Chart";
158 pieChartLabel.textColor = PNFreshGreen; 164 pieChartLabel.textColor = PNFreshGreen;
@@ -5,6 +5,6 @@ DEPENDENCIES: @@ -5,6 +5,6 @@ DEPENDENCIES:
5 - UICountingLabel (~> 1.0.0) 5 - UICountingLabel (~> 1.0.0)
6 6
7 SPEC CHECKSUMS: 7 SPEC CHECKSUMS:
8 - UICountingLabel: bb976960c0130785a600aac592954c23eb395096 8 + UICountingLabel: 0a0e9e34bf4690dbd127aaec552d19ed938087a9
9 9
10 COCOAPODS: 0.32.1 10 COCOAPODS: 0.32.1