kevinzhow

Merge branch 'master' of github.com:kevinzhow/PNChart

... ... @@ -20,4 +20,8 @@
@property (nonatomic) CGFloat barRadius;
@property (nonatomic) CAShapeLayer *gradientMask;
@property (nonatomic) CAShapeLayer *gradeLayer;
@property (nonatomic) CATextLayer* textLayer;
@end
... ...
... ... @@ -8,6 +8,7 @@
#import "PNBar.h"
#import "PNColor.h"
#import <CoreText/CoreText.h>
@implementation PNBar
... ... @@ -38,12 +39,14 @@
- (void)setGrade:(float)grade
{
NSLog(@"New garde %f",grade);
// NSLog(@"New garde %f",grade);
CGFloat startPosY = (1 - grade) * self.frame.size.height;
UIBezierPath *progressline = [UIBezierPath bezierPath];
[progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width / 2.0, (1 - grade) * self.frame.size.height)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width / 2.0, startPosY)];
[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];
... ... @@ -73,6 +76,12 @@
// Add gradient
[self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"];
self.gradientMask.path = progressline.CGPath;
// add text
[self setGradeFrame:grade startPosY:startPosY];
CABasicAnimation* opacityAnimation = [self fadeAnimation];
[self.textLayer addAnimation:opacityAnimation forKey:nil];
}
}else{
... ... @@ -115,6 +124,11 @@
self.gradientMask.strokeEnd = 1.0;
[self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
//set grade
[self setGradeFrame:grade startPosY:startPosY];
CABasicAnimation* opacityAnimation = [self fadeAnimation];
[self.textLayer addAnimation:opacityAnimation forKey:nil];
}
}
... ... @@ -153,4 +167,55 @@
}
// add number display on the top of bar
-(CGPathRef)gradePath:(CGRect)rect
{
return nil;
}
-(CATextLayer*)textLayer
{
if (!_textLayer) {
_textLayer = [[CATextLayer alloc]init];
[_textLayer setString:@"0"];
[_textLayer setAlignmentMode:kCAAlignmentCenter];
[_textLayer setForegroundColor:[[UIColor blackColor] CGColor]];
}
return _textLayer;
}
-(void)setGradeFrame:(CGFloat)grade startPosY:(CGFloat)startPosY
{
CGFloat textheigt = self.bounds.size.width;
CGFloat textWidth = self.bounds.size.width;
CGFloat textStartPosY;
if (startPosY < textheigt) {
textStartPosY = startPosY;
}
else {
textStartPosY = startPosY - textheigt;
}
[_chartLine addSublayer:self.textLayer];
[self.textLayer setFontSize:textheigt/2];
[self.textLayer setString:[[NSString alloc]initWithFormat:@"%0.f",grade*100]];
[self.textLayer setFrame:CGRectMake(0, textStartPosY, textWidth, textheigt)];
self.textLayer.contentsScale = [UIScreen mainScreen].scale;
}
-(CABasicAnimation*)fadeAnimation
{
CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.duration = 2.0;
return fadeAnimation;
}
@end
... ...
... ... @@ -10,7 +10,6 @@
#import "PNColor.h"
#import "PNChartLabel.h"
@interface PNBarChart () {
NSMutableArray *_xChartLabels;
NSMutableArray *_yChartLabels;
... ... @@ -62,53 +61,64 @@
_showChartBorder = NO;
_yChartLabelWidth = 18;
_rotateForXAxisText = false;
yLabelFormatter = ^(CGFloat yValue){
return [NSString stringWithFormat:@"%1.f",yValue];
};
}
- (void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
//make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
int yLabelsDifTotal = (int)[NSSet setWithArray:yValues].count;
_yLabelSum = yLabelsDifTotal % 2 == 0 ? yLabelsDifTotal : yLabelsDifTotal + 1;
if (_showLabel) {
[self __addYCoordinateLabelsValues];
}
}
#pragma mark - Private Method
#pragma mark - 添加柱状图的Y轴坐标
- (void)__addYCoordinateLabelsValues{
[self viewCleanupForCollection:_yChartLabels];
NSArray *yAxisValues = _yLabels ? _yLabels : _yValues;
if (_yMaxValue) {
_yValueMax = _yMaxValue;
} else {
[self getYValueMax:yValues];
[self getYValueMax:yAxisValues];
}
if (_yChartLabels) {
[self viewCleanupForCollection:_yChartLabels];
}else{
_yLabels = [NSMutableArray new];
if (_yLabelSum==4) {
_yLabelSum = yAxisValues.count;
(_yLabelSum % 2 == 0) ? _yLabelSum : _yLabelSum++;
}
if (_showLabel) {
//Add y labels
float yLabelSectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
for (int index = 0; index < _yLabelSum; index++) {
NSString *labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - index) / (float)_yLabelSum ));
float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
for (int i = 0; i < _yLabelSum; i++) {
NSString *labelText;
if (_yLabels) {
float yAsixValue = [_yLabels[_yLabels.count - i - 1] floatValue];
labelText= _yLabelFormatter(yAsixValue);
} else {
labelText = _yLabelFormatter((float)_yValueMax * ( (_yLabelSum - i) / (float)_yLabelSum ));
}
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0,
yLabelSectionHeight * index + _chartMargin - kYLabelHeight/2.0,
_yChartLabelWidth,
kYLabelHeight)];
CGRect frame = (CGRect){0, sectionHeight * i + _chartMargin - kYLabelHeight/2.0, _yChartLabelWidth, kYLabelHeight};
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:frame];
label.font = _labelFont;
label.textColor = _labelTextColor;
[label setTextAlignment:NSTextAlignmentRight];
label.text = labelText;
[_yChartLabels addObject:label];
[self addSubview:label];
}
[_yChartLabels addObject:label];
}
}
-(void)updateChartData:(NSArray *)data{
self.yValues = data;
[self updateBar];
... ... @@ -136,8 +146,9 @@
_xChartLabels = [NSMutableArray new];
}
if (_showLabel) {
_xLabelWidth = (self.frame.size.width - _chartMargin * 2) / [xLabels count];
if (_showLabel) {
int labelAddCount = 0;
for (int index = 0; index < _xLabels.count; index++) {
labelAddCount += 1;
... ...
... ... @@ -11,6 +11,7 @@
#import "PNChartLabel.h"
#import "PNLineChartData.h"
#import "PNLineChartDataItem.h"
#import <CoreText/CoreText.h>
@interface PNLineChart ()
... ... @@ -21,6 +22,9 @@
@property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line
@property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line
// display grade
@property (nonatomic) NSMutableArray *gradeStringPaths;
@end
@implementation PNLineChart
... ... @@ -312,6 +316,7 @@
{
_chartPath = [[NSMutableArray alloc] init];
_pointPath = [[NSMutableArray alloc] init];
_gradeStringPaths = [NSMutableArray array];
[self calculateChartPath:_chartPath andPointsPath:_pointPath andPathKeyPoints:_pathPoints andPathStartEndPoints:_endPointsOfPath];
// Draw each line
... ... @@ -351,6 +356,12 @@
[CATransaction commit];
NSMutableArray* textLayerArray = [self.gradeStringPaths objectAtIndex:lineIndex];
for (CATextLayer* textLayer in textLayerArray) {
CABasicAnimation* fadeAnimation = [self fadeAnimation];
[textLayer addAnimation:fadeAnimation forKey:nil];
}
UIGraphicsEndImageContext();
}
}
... ... @@ -374,6 +385,10 @@
[chartPath insertObject:progressline atIndex:lineIndex];
[pointsPath insertObject:pointPath atIndex:lineIndex];
NSMutableArray* gradePathArray = [NSMutableArray array];
[self.gradeStringPaths addObject:gradePathArray];
if (!_showLabel) {
_chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight;
_chartCavanWidth = self.frame.size.width;
... ... @@ -394,7 +409,7 @@
if (!(_yValueMax - _yValueMin)) {
innerGrade = 0.5;
} else {
innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin) == 0 ? 0.5 : (yValue - _yValueMin) / (_yValueMax - _yValueMin);
innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin);
}
CGFloat offSetX = (_chartCavanWidth) / (chartData.itemCount);
... ... @@ -411,6 +426,11 @@
[pointPath moveToPoint:CGPointMake(circleCenter.x + (inflexionWidth / 2), circleCenter.y)];
[pointPath addArcWithCenter:circleCenter radius:inflexionWidth / 2 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//jet text display text
CATextLayer* textLayer = [self createTextLayer];
[self setGradeFrame:textLayer grade:yValue pointCenter:circleCenter width:inflexionWidth];
[gradePathArray addObject:textLayer];
if ( i != 0 ) {
// calculate the point for line
... ... @@ -442,6 +462,11 @@
[pointPath addLineToPoint:CGPointMake(squareCenter.x - (inflexionWidth / 2), squareCenter.y + (inflexionWidth / 2))];
[pointPath closePath];
// text display text
CATextLayer* textLayer = [self createTextLayer];
[self setGradeFrame:textLayer grade:yValue pointCenter:squareCenter width:inflexionWidth];
[gradePathArray addObject:textLayer];
if ( i != 0 ) {
// calculate the point for line
... ... @@ -475,6 +500,11 @@
[pointPath addLineToPoint:endPoint];
[pointPath closePath];
// text display text
CATextLayer* textLayer = [self createTextLayer];
[self setGradeFrame:textLayer grade:yValue pointCenter:middlePoint width:inflexionWidth];
[gradePathArray addObject:textLayer];
if ( i != 0 ) {
// calculate the point for triangle
float distance = sqrt(pow(x - last_x, 2) + pow(y - last_y, 2) ) * 1.4 ;
... ... @@ -963,4 +993,48 @@
return squareImageView;
}
#pragma mark setter and getter
-(CATextLayer*)createTextLayer
{
CATextLayer * textLayer = [[CATextLayer alloc]init];
[textLayer setString:@"0"];
[textLayer setAlignmentMode:kCAAlignmentCenter];
[textLayer setForegroundColor:[[UIColor blackColor] CGColor]];
return textLayer;
}
-(void)setGradeFrame:(CATextLayer*)textLayer grade:(CGFloat)grade pointCenter:(CGPoint)pointCenter width:(CGFloat)width
{
CGFloat textheigt = width*3;
CGFloat textWidth = width*4;
CGFloat textStartPosY;
if (pointCenter.y > textheigt) {
textStartPosY = pointCenter.y - textheigt;
}
else {
textStartPosY = pointCenter.y;
}
[self.layer addSublayer:textLayer];
[textLayer setFontSize:textheigt/2];
[textLayer setString:[[NSString alloc]initWithFormat:@"%ld",(NSInteger)(grade*100)]];
[textLayer setFrame:CGRectMake(0, 0, textWidth, textheigt)];
[textLayer setPosition:CGPointMake(pointCenter.x, textStartPosY)];
textLayer.contentsScale = [UIScreen mainScreen].scale;
}
-(CABasicAnimation*)fadeAnimation
{
CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.duration = 2.0;
return fadeAnimation;
}
@end
... ...
... ... @@ -103,7 +103,12 @@
self.barChart.labelMarginTop = 5.0;
[self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
self.barChart.rotateForXAxisText = true ;
self.barChart.yLabelSum=5;
self.barChart.yMaxValue=100;
[self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
// self.barChart.yLabels = @[@0,@20,@40,@60];
[self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
// Adding gradient
self.barChart.barColorGradientStart = [UIColor blueColor];
... ...