mobileflowllc

Add gradient color fill to bar chart

@@ -11,7 +11,7 @@ Pod::Spec.new do |s| @@ -11,7 +11,7 @@ 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
@@ -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,43 @@ @@ -64,6 +64,43 @@
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 +
  79 + CGMutablePathRef t = CGPathCreateMutable();
  80 + CGPathMoveToPoint(t, NULL, 0, 0);
  81 + CGPathAddLineToPoint(t, NULL, self.bounds.size.width, self.bounds.size.height);
  82 + gradientMask.path = progressline.CGPath;
  83 +
  84 +
  85 + CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  86 + gradientLayer.startPoint = CGPointMake(0.5,1.0);
  87 + gradientLayer.endPoint = CGPointMake(0.5,0.0);
  88 + gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  89 + UIColor *endColor = (_barColor ? _barColor : [UIColor greenColor]);
  90 + NSArray *colors = @[
  91 + (id)_barColorGradientStart.CGColor,
  92 + (id)endColor.CGColor
  93 + ];
  94 + gradientLayer.colors = colors;
  95 +
  96 + [gradientLayer setMask:gradientMask];
  97 +
  98 + [_chartLine addSublayer:gradientLayer];
  99 +
  100 + gradientMask.strokeEnd = 1.0;
  101 + [gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  102 + }
  103 +
67 } 104 }
68 105
69 106
@@ -74,6 +111,17 @@ @@ -74,6 +111,17 @@
74 } completion:nil]; 111 } completion:nil];
75 } 112 }
76 113
  114 +- (void)setBarColorGradientStart:(UIColor *)barColorGradientStart
  115 +{
  116 + // Set gradient color, remove any existing sublayer first
  117 + for (CALayer *sublayer in [_chartLine sublayers]) {
  118 + [sublayer removeFromSuperlayer];
  119 + }
  120 + _barColorGradientStart = barColorGradientStart;
  121 +
  122 + [self setGrade:_grade];
  123 +
  124 +}
77 125
78 // Only override drawRect: if you perform custom drawing. 126 // Only override drawRect: if you perform custom drawing.
79 // An empty implementation adversely affects performance during animation. 127 // 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
@@ -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];
@@ -152,7 +157,7 @@ @@ -152,7 +157,7 @@
152 }else if ([segue.identifier isEqualToString:@"pieChart"]) 157 }else if ([segue.identifier isEqualToString:@"pieChart"])
153 { 158 {
154 159
155 - //Add LineChart 160 + //Add PieChart
156 UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)]; 161 UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];
157 pieChartLabel.text = @"Pie Chart"; 162 pieChartLabel.text = @"Pie Chart";
158 pieChartLabel.textColor = PNFreshGreen; 163 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