mobileflowllc

Add gradient color fill to bar chart

... ... @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.author = { "Kevin" => "kevinchou.c@gmail.com" }
s.platform = :ios, '6.0'
s.source = { :git => "https://github.com/kevinzhow/PNChart.git" }
s.source = { :git => "https://github.com/moflo/PNChart.git" }
s.ios.dependency 'UICountingLabel', '~> 1.0.0'
... ...
... ... @@ -16,6 +16,7 @@
@property (nonatomic) float grade;
@property (nonatomic) CAShapeLayer *chartLine;
@property (nonatomic) UIColor *barColor;
@property (nonatomic) UIColor *barColorGradientStart;
@property (nonatomic) CGFloat barRadius;
@end
... ...
... ... @@ -64,6 +64,43 @@
[_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_chartLine.strokeEnd = 1.0;
// Check if user wants to add a gradient from the start color to the bar color
if (_barColorGradientStart) {
// Add gradient
CAShapeLayer *gradientMask = [CAShapeLayer layer];
gradientMask.fillColor = [[UIColor clearColor] CGColor];
gradientMask.strokeColor = [[UIColor blackColor] CGColor];
//gradientMask.lineWidth = 4;
gradientMask.lineWidth = self.frame.size.width;
gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGMutablePathRef t = CGPathCreateMutable();
CGPathMoveToPoint(t, NULL, 0, 0);
CGPathAddLineToPoint(t, NULL, self.bounds.size.width, self.bounds.size.height);
gradientMask.path = progressline.CGPath;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(0.5,0.0);
gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
UIColor *endColor = (_barColor ? _barColor : [UIColor greenColor]);
NSArray *colors = @[
(id)_barColorGradientStart.CGColor,
(id)endColor.CGColor
];
gradientLayer.colors = colors;
[gradientLayer setMask:gradientMask];
[_chartLine addSublayer:gradientLayer];
gradientMask.strokeEnd = 1.0;
[gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
}
... ... @@ -74,6 +111,17 @@
} completion:nil];
}
- (void)setBarColorGradientStart:(UIColor *)barColorGradientStart
{
// Set gradient color, remove any existing sublayer first
for (CALayer *sublayer in [_chartLine sublayers]) {
[sublayer removeFromSuperlayer];
}
_barColorGradientStart = barColorGradientStart;
[self setGrade:_grade];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
... ...
... ... @@ -123,6 +123,12 @@ typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue);
*/
@property (nonatomic) CGFloat yMinValue;
/*
switch to indicate that the bar should be filled as a gradient
*/
@property (nonatomic) UIColor *barColorGradientStart;
@property (nonatomic, retain) id<PNChartDelegate> delegate;
@end
... ...
... ... @@ -205,9 +205,14 @@
//Height Of Bar
bar.grade = grade;
// Add gradient
bar.barColorGradientStart = _barColorGradientStart;
//For Click Index
bar.tag = index;
[_bars addObject:bar];
[self addSubview:bar];
... ...
... ... @@ -119,8 +119,13 @@
[self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
[self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
[self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
// Adding gradient
self.barChart.barColorGradientStart = [UIColor blueColor];
[self.barChart strokeChart];
self.barChart.delegate = self;
[viewController.view addSubview:barChartLabel];
... ... @@ -152,7 +157,7 @@
}else if ([segue.identifier isEqualToString:@"pieChart"])
{
//Add LineChart
//Add PieChart
UILabel * pieChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];
pieChartLabel.text = @"Pie Chart";
pieChartLabel.textColor = PNFreshGreen;
... ...
... ... @@ -5,6 +5,6 @@ DEPENDENCIES:
- UICountingLabel (~> 1.0.0)
SPEC CHECKSUMS:
UICountingLabel: bb976960c0130785a600aac592954c23eb395096
UICountingLabel: 0a0e9e34bf4690dbd127aaec552d19ed938087a9
COCOAPODS: 0.32.1
... ...