Kevin

Merge pull request #83 from moflo/master

Add gradient option to bar & circle charts
... ... @@ -11,12 +11,12 @@ 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'
s.source_files = 'PNChart/*.{h,m}'
s.public_header_files = 'PNChart/*.h'
s.source_files = 'PNChart/**/*.{h,m}'
s.public_header_files = 'PNChart/**/*.h'
s.frameworks = 'CoreGraphics', 'UIKit', 'Foundation', 'QuartzCore'
s.requires_arc = true
end
... ...
... ... @@ -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,39 @@
[_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);
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 +107,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];
... ...
... ... @@ -26,6 +26,7 @@ typedef NS_ENUM(NSUInteger, PNChartFormatType) {
@property (strong, nonatomic) UICountingLabel *countingLabel;
@property (nonatomic) UIColor *strokeColor;
@property (nonatomic) UIColor *strokeColorGradientStart;
@property (nonatomic) NSNumber *total;
@property (nonatomic) NSNumber *current;
@property (nonatomic) NSNumber *lineWidth;
... ...
... ... @@ -101,6 +101,41 @@
_circle.strokeEnd = [_current floatValue] / [_total floatValue];
[_countingLabel countFrom:0 to:[_current floatValue] withDuration:1.0];
// Check if user wants to add a gradient from the start color to the bar color
if (_strokeColorGradientStart) {
// Add gradient
CAShapeLayer *gradientMask = [CAShapeLayer layer];
gradientMask.fillColor = [[UIColor clearColor] CGColor];
gradientMask.strokeColor = [[UIColor blackColor] CGColor];
gradientMask.lineWidth = _circle.lineWidth;
gradientMask.lineCap = kCALineCapRound;
CGRect gradientFrame = CGRectMake(0, 0, 2*self.bounds.size.width, 2*self.bounds.size.height);
gradientMask.frame = gradientFrame;
gradientMask.path = _circle.path;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(0.5,0.0);
gradientLayer.frame = gradientFrame;
UIColor *endColor = (_strokeColor ? _strokeColor : [UIColor greenColor]);
NSArray *colors = @[
(id)endColor.CGColor,
(id)_strokeColorGradientStart.CGColor
];
gradientLayer.colors = colors;
[gradientLayer setMask:gradientMask];
[_circle addSublayer:gradientLayer];
gradientMask.strokeEnd = [_current floatValue] / [_total floatValue];
[gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
}
... ...
... ... @@ -67,6 +67,7 @@
CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
if (_showLabel) {
NSInteger index = 0;
NSInteger num = _yLabelNum + 1;
... ... @@ -80,6 +81,7 @@
index += 1;
num -= 1;
}
}
}
... ...
This diff is collapsed. Click to expand it.
... ... @@ -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];
... ... @@ -142,6 +147,7 @@
PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:@100 andCurrent:@60 andClockwise:YES andShadow:YES];
circleChart.backgroundColor = [UIColor clearColor];
[circleChart setStrokeColor:PNGreen];
[circleChart setStrokeColorGradientStart:[UIColor blueColor]];
[circleChart strokeChart];
[viewController.view addSubview:circleChartLabel];
... ... @@ -152,7 +158,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
... ...