Kevin

Merge pull request #48 from orlandoaleman/master

FIX related with XLabels 
@@ -11,12 +11,9 @@ @@ -11,12 +11,9 @@
11 11
12 @interface PNBar : UIView 12 @interface PNBar : UIView
13 13
14 -@property (nonatomic) float grade; 14 +- (void)rollBack;
15 -  
16 -@property (nonatomic,strong) CAShapeLayer * chartLine;  
17 -  
18 -@property (nonatomic, strong) UIColor * barColor;  
19 -  
20 --(void)rollBack;  
21 15
  16 +@property (nonatomic) float grade;
  17 +@property (nonatomic) CAShapeLayer *chartLine;
  18 +@property (nonatomic) UIColor *barColor;
22 @end 19 @end
@@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
14 - (id)initWithFrame:(CGRect)frame 14 - (id)initWithFrame:(CGRect)frame
15 { 15 {
16 self = [super initWithFrame:frame]; 16 self = [super initWithFrame:frame];
  17 +
17 if (self) { 18 if (self) {
18 // Initialization code 19 // Initialization code
19 _chartLine = [CAShapeLayer layer]; 20 _chartLine = [CAShapeLayer layer];
@@ -22,57 +23,61 @@ @@ -22,57 +23,61 @@
22 _chartLine.lineWidth = self.frame.size.width; 23 _chartLine.lineWidth = self.frame.size.width;
23 _chartLine.strokeEnd = 0.0; 24 _chartLine.strokeEnd = 0.0;
24 self.clipsToBounds = YES; 25 self.clipsToBounds = YES;
25 - [self.layer addSublayer:_chartLine]; 26 + [self.layer addSublayer:_chartLine];
26 self.layer.cornerRadius = 2.0; 27 self.layer.cornerRadius = 2.0;
27 } 28 }
  29 +
28 return self; 30 return self;
29 } 31 }
30 32
31 --(void)setGrade:(float)grade 33 +
  34 +- (void)setGrade:(float)grade
32 { 35 {
33 - _grade = grade; 36 + _grade = grade;
34 - UIBezierPath *progressline = [UIBezierPath bezierPath]; 37 + UIBezierPath *progressline = [UIBezierPath bezierPath];
35 - 38 +
36 - [progressline moveToPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height)]; 39 + [progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)];
37 - [progressline addLineToPoint:CGPointMake(self.frame.size.width/2.0, (1 - grade) * self.frame.size.height)]; 40 + [progressline addLineToPoint:CGPointMake(self.frame.size.width / 2.0, (1 - grade) * self.frame.size.height)];
38 - 41 +
39 [progressline setLineWidth:1.0]; 42 [progressline setLineWidth:1.0];
40 [progressline setLineCapStyle:kCGLineCapSquare]; 43 [progressline setLineCapStyle:kCGLineCapSquare];
41 - _chartLine.path = progressline.CGPath; 44 + _chartLine.path = progressline.CGPath;
42 - 45 +
43 - if (_barColor) { 46 + if (_barColor) {
44 - _chartLine.strokeColor = [_barColor CGColor]; 47 + _chartLine.strokeColor = [_barColor CGColor];
45 - }else{ 48 + }
46 - _chartLine.strokeColor = [PNGreen CGColor]; 49 + else {
47 - } 50 + _chartLine.strokeColor = [PNGreen CGColor];
48 - 51 + }
  52 +
49 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 53 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
50 pathAnimation.duration = 1.0; 54 pathAnimation.duration = 1.0;
51 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 55 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
52 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 56 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
53 pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 57 pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
54 [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 58 [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
55 - 59 +
56 _chartLine.strokeEnd = 1.0; 60 _chartLine.strokeEnd = 1.0;
57 } 61 }
58 62
59 --(void)rollBack{ 63 +
60 - [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 64 +- (void)rollBack
  65 +{
  66 + [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations: ^{
61 _chartLine.strokeColor = [UIColor clearColor].CGColor; 67 _chartLine.strokeColor = [UIColor clearColor].CGColor;
62 } completion:nil]; 68 } completion:nil];
63 -  
64 -  
65 } 69 }
  70 +
  71 +
66 // Only override drawRect: if you perform custom drawing. 72 // Only override drawRect: if you perform custom drawing.
67 // An empty implementation adversely affects performance during animation. 73 // An empty implementation adversely affects performance during animation.
68 - (void)drawRect:(CGRect)rect 74 - (void)drawRect:(CGRect)rect
69 { 75 {
70 - //Draw BG 76 + //Draw BG
71 CGContextRef context = UIGraphicsGetCurrentContext(); 77 CGContextRef context = UIGraphicsGetCurrentContext();
72 - 78 +
73 CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor); 79 CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
74 - CGContextFillRect(context, rect); 80 + CGContextFillRect(context, rect);
75 -  
76 } 81 }
77 82
78 83
@@ -8,10 +8,11 @@ @@ -8,10 +8,11 @@
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 10
11 -#define chartMargin 10 11 +#define chartMargin 10
12 -#define xLabelMargin 15 12 +#define xLabelMargin 15
13 -#define yLabelMargin 15 13 +#define yLabelMargin 15
14 -#define yLabelHeight 11 14 +#define yLabelHeight 11
  15 +#define xLabelHeight 20
15 16
16 @interface PNBarChart : UIView 17 @interface PNBarChart : UIView
17 18
@@ -19,24 +20,16 @@ @@ -19,24 +20,16 @@
19 * This method will call and stroke the line in animation 20 * This method will call and stroke the line in animation
20 */ 21 */
21 22
22 --(void)strokeChart; 23 +- (void)strokeChart;
23 -  
24 -@property (strong, nonatomic) NSArray * xLabels;  
25 -  
26 -@property (strong, nonatomic) NSArray * yLabels;  
27 -  
28 -@property (strong, nonatomic) NSArray * yValues;  
29 24
  25 +@property (nonatomic) NSArray *xLabels;
  26 +@property (nonatomic) NSArray *yLabels;
  27 +@property (nonatomic) NSArray *yValues;
30 @property (nonatomic) CGFloat xLabelWidth; 28 @property (nonatomic) CGFloat xLabelWidth;
31 -  
32 @property (nonatomic) int yValueMax; 29 @property (nonatomic) int yValueMax;
33 - 30 +@property (nonatomic) UIColor *strokeColor;
34 -@property (nonatomic, strong) UIColor * strokeColor; 31 +@property (nonatomic) NSArray *strokeColors;
35 - 32 +@property (nonatomic) UIColor *barBackgroundColor;
36 -@property (nonatomic, strong) NSArray * strokeColors;  
37 -  
38 -@property (nonatomic, strong) UIColor * barBackgroundColor;  
39 -  
40 @property (nonatomic) BOOL showLabel; 33 @property (nonatomic) BOOL showLabel;
41 34
42 @end 35 @end
@@ -11,9 +11,9 @@ @@ -11,9 +11,9 @@
11 #import "PNChartLabel.h" 11 #import "PNChartLabel.h"
12 #import "PNBar.h" 12 #import "PNBar.h"
13 13
14 -@interface PNBarChart() { 14 +@interface PNBarChart () {
15 - NSMutableArray* _bars; 15 + NSMutableArray *_bars;
16 - NSMutableArray* _labels; 16 + NSMutableArray *_labels;
17 } 17 }
18 18
19 - (UIColor *)barColorAtIndex:(NSUInteger)index; 19 - (UIColor *)barColorAtIndex:(NSUInteger)index;
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 - (id)initWithFrame:(CGRect)frame 24 - (id)initWithFrame:(CGRect)frame
25 { 25 {
26 self = [super initWithFrame:frame]; 26 self = [super initWithFrame:frame];
  27 +
27 if (self) { 28 if (self) {
28 // Initialization code 29 // Initialization code
29 self.backgroundColor = [UIColor whiteColor]; 30 self.backgroundColor = [UIColor whiteColor];
@@ -37,23 +38,26 @@ @@ -37,23 +38,26 @@
37 return self; 38 return self;
38 } 39 }
39 40
40 --(void)setYValues:(NSArray *)yValues 41 +
  42 +- (void)setYValues:(NSArray *)yValues
41 { 43 {
42 _yValues = yValues; 44 _yValues = yValues;
43 [self setYLabels:yValues]; 45 [self setYLabels:yValues];
44 46
45 - _xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count]; 47 + _xLabelWidth = (self.frame.size.width - chartMargin * 2) / [_yValues count];
46 } 48 }
47 49
48 --(void)setYLabels:(NSArray *)yLabels 50 +
  51 +- (void)setYLabels:(NSArray *)yLabels
49 { 52 {
50 NSInteger max = 0; 53 NSInteger max = 0;
51 - for (NSString * valueString in yLabels) { 54 +
  55 + for (NSString *valueString in yLabels) {
52 NSInteger value = [valueString integerValue]; 56 NSInteger value = [valueString integerValue];
  57 +
53 if (value > max) { 58 if (value > max) {
54 max = value; 59 max = value;
55 } 60 }
56 -  
57 } 61 }
58 62
59 //Min value for Y label 63 //Min value for Y label
@@ -64,47 +68,54 @@ @@ -64,47 +68,54 @@
64 _yValueMax = (int)max; 68 _yValueMax = (int)max;
65 } 69 }
66 70
67 --(void)setXLabels:(NSArray *)xLabels 71 +
  72 +- (void)setXLabels:(NSArray *)xLabels
68 { 73 {
69 - [self viewCleanupForCollection:_labels];  
70 _xLabels = xLabels; 74 _xLabels = xLabels;
71 75
72 if (_showLabel) { 76 if (_showLabel) {
73 - _xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count]; 77 + _xLabelWidth = (self.frame.size.width - chartMargin * 2) / [xLabels count];
74 -  
75 - for(int index = 0; index < xLabels.count; index++)  
76 - {  
77 - NSString* labelText = xLabels[index];  
78 - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)];  
79 - [label setTextAlignment:NSTextAlignmentCenter];  
80 - label.text = labelText;  
81 - [_labels addObject:label];  
82 - [self addSubview:label];  
83 - }  
84 } 78 }
85 } 79 }
86 80
87 --(void)setStrokeColor:(UIColor *)strokeColor 81 +
  82 +- (void)setStrokeColor:(UIColor *)strokeColor
88 { 83 {
89 - _strokeColor = strokeColor; 84 + _strokeColor = strokeColor;
90 } 85 }
91 86
92 --(void)strokeChart 87 +
  88 +- (void)strokeChart
93 { 89 {
  90 + [self viewCleanupForCollection:_labels];
  91 +
  92 + for (int index = 0; index < _xLabels.count; index++) {
  93 + NSString *labelText = _xLabels[index];
  94 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)];
  95 + [label setTextAlignment:NSTextAlignmentCenter];
  96 + label.text = labelText;
  97 + [_labels addObject:label];
  98 + [self addSubview:label];
  99 + }
  100 +
94 [self viewCleanupForCollection:_bars]; 101 [self viewCleanupForCollection:_bars];
95 - CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0; 102 +
  103 + CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - xLabelHeight * 2;
96 NSInteger index = 0; 104 NSInteger index = 0;
97 105
98 - for (NSString * valueString in _yValues) { 106 + for (NSString *valueString in _yValues) {
99 float value = [valueString floatValue]; 107 float value = [valueString floatValue];
100 108
101 float grade = (float)value / (float)_yValueMax; 109 float grade = (float)value / (float)_yValueMax;
102 - PNBar * bar; 110 + PNBar *bar;
  111 +
103 if (_showLabel) { 112 if (_showLabel) {
104 - bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)]; 113 + bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - xLabelHeight - chartMargin, _xLabelWidth * 0.5, chartCavanHeight)];
105 - }else{ 114 + }
106 - bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)]; 115 + else {
  116 + bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight, _xLabelWidth * 0.6, chartCavanHeight)];
107 } 117 }
  118 +
108 bar.backgroundColor = _barBackgroundColor; 119 bar.backgroundColor = _barBackgroundColor;
109 bar.barColor = [self barColorAtIndex:index]; 120 bar.barColor = [self barColorAtIndex:index];
110 bar.grade = grade; 121 bar.grade = grade;
@@ -115,7 +126,8 @@ @@ -115,7 +126,8 @@
115 } 126 }
116 } 127 }
117 128
118 -- (void)viewCleanupForCollection:(NSMutableArray*)array 129 +
  130 +- (void)viewCleanupForCollection:(NSMutableArray *)array
119 { 131 {
120 if (array.count) { 132 if (array.count) {
121 [array makeObjectsPerformSelector:@selector(removeFromSuperview)]; 133 [array makeObjectsPerformSelector:@selector(removeFromSuperview)];
@@ -123,15 +135,18 @@ @@ -123,15 +135,18 @@
123 } 135 }
124 } 136 }
125 137
  138 +
126 #pragma mark - Class extension methods 139 #pragma mark - Class extension methods
127 140
128 - (UIColor *)barColorAtIndex:(NSUInteger)index 141 - (UIColor *)barColorAtIndex:(NSUInteger)index
129 { 142 {
130 if ([self.strokeColors count] == [self.yValues count]) { 143 if ([self.strokeColors count] == [self.yValues count]) {
131 return self.strokeColors[index]; 144 return self.strokeColors[index];
132 - } else { 145 + }
  146 + else {
133 return self.strokeColor; 147 return self.strokeColor;
134 } 148 }
135 } 149 }
136 150
  151 +
137 @end 152 @end
@@ -14,13 +14,13 @@ @@ -14,13 +14,13 @@
14 * When user click on the chart line 14 * When user click on the chart line
15 * 15 *
16 */ 16 */
17 -- (void)userClickedOnLinePoint:(CGPoint )point lineIndex:(NSInteger)lineIndex; 17 +- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex;
18 18
19 /** 19 /**
20 * When user click on the chart line key point 20 * When user click on the chart line key point
21 * 21 *
22 */ 22 */
23 -- (void)userClickedOnLineKeyPoint:(CGPoint )point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex; 23 +- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex;
24 24
25 25
26 @end 26 @end
@@ -14,27 +14,20 @@ @@ -14,27 +14,20 @@
14 - (id)initWithFrame:(CGRect)frame 14 - (id)initWithFrame:(CGRect)frame
15 { 15 {
16 self = [super initWithFrame:frame]; 16 self = [super initWithFrame:frame];
  17 +
17 if (self) { 18 if (self) {
18 // Initialization code 19 // Initialization code
19 [self setLineBreakMode:NSLineBreakByWordWrapping]; 20 [self setLineBreakMode:NSLineBreakByWordWrapping];
20 [self setMinimumScaleFactor:11.0f]; 21 [self setMinimumScaleFactor:11.0f];
21 [self setNumberOfLines:0]; 22 [self setNumberOfLines:0];
22 [self setFont:[UIFont boldSystemFontOfSize:11.0f]]; 23 [self setFont:[UIFont boldSystemFontOfSize:11.0f]];
23 - [self setTextColor: PNDeepGrey]; 24 + [self setTextColor:PNDeepGrey];
24 self.backgroundColor = [UIColor clearColor]; 25 self.backgroundColor = [UIColor clearColor];
25 [self setTextAlignment:NSTextAlignmentLeft]; 26 [self setTextAlignment:NSTextAlignmentLeft];
26 self.userInteractionEnabled = YES; 27 self.userInteractionEnabled = YES;
27 } 28 }
28 - return self;  
29 -}  
30 29
31 -/* 30 + return self;
32 -// Only override drawRect: if you perform custom drawing.  
33 -// An empty implementation adversely affects performance during animation.  
34 -- (void)drawRect:(CGRect)rect  
35 -{  
36 - // Drawing code  
37 } 31 }
38 -*/  
39 32
40 @end 33 @end
@@ -14,17 +14,17 @@ @@ -14,17 +14,17 @@
14 14
15 @interface PNCircleChart : UIView 15 @interface PNCircleChart : UIView
16 16
17 --(void)strokeChart; 17 +- (void)strokeChart;
18 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise; 18 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise;
19 19
20 -@property (nonatomic, strong) UIColor *strokeColor; 20 +@property (nonatomic) UIColor *strokeColor;
21 -@property (nonatomic, strong) UIColor *labelColor; 21 +@property (nonatomic) UIColor *labelColor;
22 -@property (nonatomic, strong) NSNumber * total; 22 +@property (nonatomic) NSNumber *total;
23 -@property (nonatomic, strong) NSNumber * current; 23 +@property (nonatomic) NSNumber *current;
24 -@property (nonatomic, strong) NSNumber * lineWidth; 24 +@property (nonatomic) NSNumber *lineWidth;
25 @property (nonatomic) BOOL clockwise; 25 @property (nonatomic) BOOL clockwise;
26 26
27 -@property(nonatomic,strong) CAShapeLayer * circle; 27 +@property (nonatomic) CAShapeLayer *circle;
28 -@property(nonatomic,strong) CAShapeLayer * circleBG; 28 +@property (nonatomic) CAShapeLayer *circleBG;
29 29
30 @end 30 @end
@@ -22,25 +22,27 @@ @@ -22,25 +22,27 @@
22 if (!_labelColor) { 22 if (!_labelColor) {
23 _labelColor = PNDeepGrey; 23 _labelColor = PNDeepGrey;
24 } 24 }
  25 +
25 return _labelColor; 26 return _labelColor;
26 } 27 }
27 28
28 29
29 -- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise { 30 +- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise
  31 +{
30 self = [super initWithFrame:frame]; 32 self = [super initWithFrame:frame];
31 - 33 +
32 if (self) { 34 if (self) {
33 _total = total; 35 _total = total;
34 _current = current; 36 _current = current;
35 _strokeColor = PNFreshGreen; 37 _strokeColor = PNFreshGreen;
36 - _clockwise = clockwise; 38 + _clockwise = clockwise;
37 - 39 +
38 - CGFloat startAngle = clockwise ? -90.0f : 270.0f; 40 + CGFloat startAngle = clockwise ? -90.0f : 270.0f;
39 - CGFloat endAngle = clockwise ? -90.01f : 270.01f; 41 + CGFloat endAngle = clockwise ? -90.01f : 270.01f;
40 - 42 +
41 _lineWidth = [NSNumber numberWithFloat:8.0]; 43 _lineWidth = [NSNumber numberWithFloat:8.0];
42 - UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x,self.center.y) radius:self.frame.size.height*0.5 startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:clockwise]; 44 + UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x, self.center.y) radius:self.frame.size.height * 0.5 startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:clockwise];
43 - 45 +
44 _circle = [CAShapeLayer layer]; 46 _circle = [CAShapeLayer layer];
45 _circle.path = circlePath.CGPath; 47 _circle.path = circlePath.CGPath;
46 _circle.lineCap = kCALineCapRound; 48 _circle.lineCap = kCALineCapRound;
@@ -56,50 +58,49 @@ @@ -56,50 +58,49 @@
56 _circleBG.strokeColor = PNLightYellow.CGColor; 58 _circleBG.strokeColor = PNLightYellow.CGColor;
57 _circleBG.strokeEnd = 1.0; 59 _circleBG.strokeEnd = 1.0;
58 _circleBG.zPosition = -1; 60 _circleBG.zPosition = -1;
59 - 61 +
60 [self.layer addSublayer:_circle]; 62 [self.layer addSublayer:_circle];
61 [self.layer addSublayer:_circleBG]; 63 [self.layer addSublayer:_circleBG];
62 64
63 - _gradeLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)]; 65 + _gradeLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];
64 -  
65 } 66 }
66 - 67 +
67 return self; 68 return self;
68 -  
69 } 69 }
70 70
71 --(void)strokeChart 71 +
  72 +- (void)strokeChart
72 { 73 {
73 //Add count label 74 //Add count label
74 - 75 +
75 [_gradeLabel setTextAlignment:NSTextAlignmentCenter]; 76 [_gradeLabel setTextAlignment:NSTextAlignmentCenter];
76 [_gradeLabel setFont:[UIFont boldSystemFontOfSize:13.0f]]; 77 [_gradeLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];
77 [_gradeLabel setTextColor:self.labelColor]; 78 [_gradeLabel setTextColor:self.labelColor];
78 - [_gradeLabel setCenter:CGPointMake(self.center.x,self.center.y)]; 79 + [_gradeLabel setCenter:CGPointMake(self.center.x, self.center.y)];
79 _gradeLabel.method = UILabelCountingMethodEaseInOut; 80 _gradeLabel.method = UILabelCountingMethodEaseInOut;
80 _gradeLabel.format = @"%d%%"; 81 _gradeLabel.format = @"%d%%";
81 - 82 +
82 - 83 +
83 [self addSubview:_gradeLabel]; 84 [self addSubview:_gradeLabel];
84 - 85 +
85 //Add circle params 86 //Add circle params
86 - 87 +
87 _circle.lineWidth = [_lineWidth floatValue]; 88 _circle.lineWidth = [_lineWidth floatValue];
88 _circleBG.lineWidth = [_lineWidth floatValue]; 89 _circleBG.lineWidth = [_lineWidth floatValue];
89 _circleBG.strokeEnd = 1.0; 90 _circleBG.strokeEnd = 1.0;
90 _circle.strokeColor = _strokeColor.CGColor; 91 _circle.strokeColor = _strokeColor.CGColor;
91 - 92 +
92 //Add Animation 93 //Add Animation
93 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 94 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
94 pathAnimation.duration = 1.0; 95 pathAnimation.duration = 1.0;
95 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 96 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
96 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 97 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
97 - pathAnimation.toValue = [NSNumber numberWithFloat:[_current floatValue]/[_total floatValue]]; 98 + pathAnimation.toValue = [NSNumber numberWithFloat:[_current floatValue] / [_total floatValue]];
98 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 99 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
99 - _circle.strokeEnd = [_current floatValue]/[_total floatValue]; 100 + _circle.strokeEnd = [_current floatValue] / [_total floatValue];
100 - 101 +
101 - [_gradeLabel countFrom:0 to:[_current floatValue]/[_total floatValue]*100 withDuration:1.0]; 102 + [_gradeLabel countFrom:0 to:[_current floatValue] / [_total floatValue] * 100 withDuration:1.0];
102 -  
103 } 103 }
104 104
  105 +
105 @end 106 @end
@@ -7,45 +7,44 @@ @@ -7,45 +7,44 @@
7 // 7 //
8 8
9 #import <Foundation/Foundation.h> 9 #import <Foundation/Foundation.h>
  10 +
10 /* 11 /*
11 * System Versioning Preprocessor Macros 12 * System Versioning Preprocessor Macros
12 */ 13 */
13 14
14 -#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) 15 +#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
15 - 16 +
16 -#define PNGrey [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0f] 17 +#define PNGrey [UIColor colorWithRed:246.0 / 255.0 green:246.0 / 255.0 blue:246.0 / 255.0 alpha:1.0f]
17 -#define PNLightBlue [UIColor colorWithRed:94.0/255.0 green:147.0/255.0 blue:196.0/255.0 alpha:1.0f] 18 +#define PNLightBlue [UIColor colorWithRed:94.0 / 255.0 green:147.0 / 255.0 blue:196.0 / 255.0 alpha:1.0f]
18 -#define PNGreen [UIColor colorWithRed:77.0/255.0 green:186.0/255.0 blue:122.0/255.0 alpha:1.0f] 19 +#define PNGreen [UIColor colorWithRed:77.0 / 255.0 green:186.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
19 -#define PNTitleColor [UIColor colorWithRed:0.0/255.0 green:189.0/255.0 blue:113.0/255.0 alpha:1.0f] 20 +#define PNTitleColor [UIColor colorWithRed:0.0 / 255.0 green:189.0 / 255.0 blue:113.0 / 255.0 alpha:1.0f]
20 -#define PNButtonGrey [UIColor colorWithRed:141.0/255.0 green:141.0/255.0 blue:141.0/255.0 alpha:1.0f] 21 +#define PNButtonGrey [UIColor colorWithRed:141.0 / 255.0 green:141.0 / 255.0 blue:141.0 / 255.0 alpha:1.0f]
21 -#define PNFreshGreen [UIColor colorWithRed:77.0/255.0 green:196.0/255.0 blue:122.0/255.0 alpha:1.0f] 22 +#define PNFreshGreen [UIColor colorWithRed:77.0 / 255.0 green:196.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
22 -#define PNRed [UIColor colorWithRed:245.0/255.0 green:94.0/255.0 blue:78.0/255.0 alpha:1.0f] 23 +#define PNRed [UIColor colorWithRed:245.0 / 255.0 green:94.0 / 255.0 blue:78.0 / 255.0 alpha:1.0f]
23 -#define PNMauve [UIColor colorWithRed:88.0/255.0 green:75.0/255.0 blue:103.0/255.0 alpha:1.0f] 24 +#define PNMauve [UIColor colorWithRed:88.0 / 255.0 green:75.0 / 255.0 blue:103.0 / 255.0 alpha:1.0f]
24 -#define PNBrown [UIColor colorWithRed:119.0/255.0 green:107.0/255.0 blue:95.0/255.0 alpha:1.0f] 25 +#define PNBrown [UIColor colorWithRed:119.0 / 255.0 green:107.0 / 255.0 blue:95.0 / 255.0 alpha:1.0f]
25 -#define PNBlue [UIColor colorWithRed:82.0/255.0 green:116.0/255.0 blue:188.0/255.0 alpha:1.0f] 26 +#define PNBlue [UIColor colorWithRed:82.0 / 255.0 green:116.0 / 255.0 blue:188.0 / 255.0 alpha:1.0f]
26 -#define PNDarkBlue [UIColor colorWithRed:121.0/255.0 green:134.0/255.0 blue:142.0/255.0 alpha:1.0f] 27 +#define PNDarkBlue [UIColor colorWithRed:121.0 / 255.0 green:134.0 / 255.0 blue:142.0 / 255.0 alpha:1.0f]
27 -#define PNYellow [UIColor colorWithRed:242.0/255.0 green:197.0/255.0 blue:117.0/255.0 alpha:1.0f] 28 +#define PNYellow [UIColor colorWithRed:242.0 / 255.0 green:197.0 / 255.0 blue:117.0 / 255.0 alpha:1.0f]
28 -#define PNWhite [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0f] 29 +#define PNWhite [UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1.0f]
29 -#define PNDeepGrey [UIColor colorWithRed:99.0/255.0 green:99.0/255.0 blue:99.0/255.0 alpha:1.0f] 30 +#define PNDeepGrey [UIColor colorWithRed:99.0 / 255.0 green:99.0 / 255.0 blue:99.0 / 255.0 alpha:1.0f]
30 -#define PNPinkGrey [UIColor colorWithRed:200.0/255.0 green:193.0/255.0 blue:193.0/255.0 alpha:1.0f] 31 +#define PNPinkGrey [UIColor colorWithRed:200.0 / 255.0 green:193.0 / 255.0 blue:193.0 / 255.0 alpha:1.0f]
31 -#define PNHealYellow [UIColor colorWithRed:245.0/255.0 green:242.0/255.0 blue:238.0/255.0 alpha:1.0f] 32 +#define PNHealYellow [UIColor colorWithRed:245.0 / 255.0 green:242.0 / 255.0 blue:238.0 / 255.0 alpha:1.0f]
32 -#define PNLightGrey [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0f] 33 +#define PNLightGrey [UIColor colorWithRed:225.0 / 255.0 green:225.0 / 255.0 blue:225.0 / 255.0 alpha:1.0f]
33 -#define PNCleanGrey [UIColor colorWithRed:251.0/255.0 green:251.0/255.0 blue:251.0/255.0 alpha:1.0f] 34 +#define PNCleanGrey [UIColor colorWithRed:251.0 / 255.0 green:251.0 / 255.0 blue:251.0 / 255.0 alpha:1.0f]
34 -#define PNLightYellow [UIColor colorWithRed:241.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0f] 35 +#define PNLightYellow [UIColor colorWithRed:241.0 / 255.0 green:240.0 / 255.0 blue:240.0 / 255.0 alpha:1.0f]
35 -#define PNDarkYellow [UIColor colorWithRed:152.0/255.0 green:150.0/255.0 blue:159.0/255.0 alpha:1.0f] 36 +#define PNDarkYellow [UIColor colorWithRed:152.0 / 255.0 green:150.0 / 255.0 blue:159.0 / 255.0 alpha:1.0f]
36 -#define PNPinkDark [UIColor colorWithRed:170.0/255.0 green:165.0/255.0 blue:165.0/255.0 alpha:1.0f] 37 +#define PNPinkDark [UIColor colorWithRed:170.0 / 255.0 green:165.0 / 255.0 blue:165.0 / 255.0 alpha:1.0f]
37 -#define PNCloudWhite [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0f] 38 +#define PNCloudWhite [UIColor colorWithRed:244.0 / 255.0 green:244.0 / 255.0 blue:244.0 / 255.0 alpha:1.0f]
38 -#define PNBlack [UIColor colorWithRed:45.0/255.0 green:45.0/255.0 blue:45.0/255.0 alpha:1.0f] 39 +#define PNBlack [UIColor colorWithRed:45.0 / 255.0 green:45.0 / 255.0 blue:45.0 / 255.0 alpha:1.0f]
39 -#define PNStarYellow [UIColor colorWithRed:252.0/255.0 green:223.0/255.0 blue:101.0/255.0 alpha:1.0f] 40 +#define PNStarYellow [UIColor colorWithRed:252.0 / 255.0 green:223.0 / 255.0 blue:101.0 / 255.0 alpha:1.0f]
40 -#define PNTwitterColor [UIColor colorWithRed:0.0/255.0 green:171.0/255.0 blue:243.0/255.0 alpha:1.0] 41 +#define PNTwitterColor [UIColor colorWithRed:0.0 / 255.0 green:171.0 / 255.0 blue:243.0 / 255.0 alpha:1.0]
41 -#define PNWeiboColor [UIColor colorWithRed:250.0/255.0 green:0.0/255.0 blue:33.0/255.0 alpha:1.0] 42 +#define PNWeiboColor [UIColor colorWithRed:250.0 / 255.0 green:0.0 / 255.0 blue:33.0 / 255.0 alpha:1.0]
42 -#define PNiOSGreenColor [UIColor colorWithRed:98.0/255.0 green:247.0/255.0 blue:77.0/255.0 alpha:1.0] 43 +#define PNiOSGreenColor [UIColor colorWithRed:98.0 / 255.0 green:247.0 / 255.0 blue:77.0 / 255.0 alpha:1.0]
43 44
44 45
45 @interface PNColor : NSObject 46 @interface PNColor : NSObject
46 47
47 -  
48 - (UIImage *)imageFromColor:(UIColor *)color; 48 - (UIImage *)imageFromColor:(UIColor *)color;
49 49
50 -  
51 @end 50 @end
@@ -10,19 +10,10 @@ @@ -10,19 +10,10 @@
10 10
11 @implementation PNColor 11 @implementation PNColor
12 12
13 --(id)init{ 13 +- (UIImage *)imageFromColor:(UIColor *)color
14 - if (self = [super init]) {  
15 -  
16 -  
17 - }  
18 - return self;  
19 -}  
20 -  
21 -  
22 -  
23 --(UIImage *)imageFromColor:(UIColor*)color  
24 { 14 {
25 CGRect rect = CGRectMake(0, 0, 1, 1); 15 CGRect rect = CGRectMake(0, 0, 1, 1);
  16 +
26 UIGraphicsBeginImageContext(rect.size); 17 UIGraphicsBeginImageContext(rect.size);
27 CGContextRef context = UIGraphicsGetCurrentContext(); 18 CGContextRef context = UIGraphicsGetCurrentContext();
28 CGContextSetFillColorWithColor(context, 19 CGContextSetFillColorWithColor(context,
@@ -35,6 +26,4 @@ @@ -35,6 +26,4 @@
35 } 26 }
36 27
37 28
38 -  
39 -  
40 @end 29 @end
@@ -6,14 +6,11 @@ @@ -6,14 +6,11 @@
6 // Copyright (c) 2013年 kevinzhow. All rights reserved. 6 // Copyright (c) 2013年 kevinzhow. All rights reserved.
7 // 7 //
8 8
9 -  
10 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
11 #import <QuartzCore/QuartzCore.h> 10 #import <QuartzCore/QuartzCore.h>
12 #import "PNChartDelegate.h" 11 #import "PNChartDelegate.h"
13 12
14 13
15 -  
16 -  
17 @interface PNLineChart : UIView 14 @interface PNLineChart : UIView
18 15
19 /** 16 /**
@@ -22,18 +19,18 @@ @@ -22,18 +19,18 @@
22 19
23 - (void)strokeChart; 20 - (void)strokeChart;
24 21
25 -@property(nonatomic,retain) id<PNChartDelegate> delegate; 22 +@property (nonatomic, retain) id<PNChartDelegate> delegate;
26 23
27 -@property (strong, nonatomic) NSArray * xLabels; 24 +@property (nonatomic) NSArray *xLabels;
28 25
29 -@property (strong, nonatomic) NSArray * yLabels; 26 +@property (nonatomic) NSArray *yLabels;
30 27
31 /** 28 /**
32 * Array of `LineChartData` objects, one for each line. 29 * Array of `LineChartData` objects, one for each line.
33 */ 30 */
34 -@property (strong, nonatomic) NSArray *chartData; 31 +@property (nonatomic) NSArray *chartData;
35 32
36 -@property (strong, nonatomic) NSMutableArray * pathPoints; 33 +@property (nonatomic) NSMutableArray *pathPoints;
37 34
38 @property (nonatomic) CGFloat xLabelWidth; 35 @property (nonatomic) CGFloat xLabelWidth;
39 36
@@ -18,9 +18,9 @@ @@ -18,9 +18,9 @@
18 //------------------------------------------------------------------------------------------------ 18 //------------------------------------------------------------------------------------------------
19 @interface PNLineChart () 19 @interface PNLineChart ()
20 20
21 -@property (nonatomic,strong) NSMutableArray *chartLineArray; // Array[CAShapeLayer] 21 +@property (nonatomic) NSMutableArray *chartLineArray; // Array[CAShapeLayer]
22 22
23 -@property (strong, nonatomic) NSMutableArray *chartPath; //Array of line path, one for each line. 23 +@property (nonatomic) NSMutableArray *chartPath; //Array of line path, one for each line.
24 24
25 - (void)setDefaultValues; 25 - (void)setDefaultValues;
26 26
@@ -34,100 +34,110 @@ @@ -34,100 +34,110 @@
34 34
35 #pragma mark initialization 35 #pragma mark initialization
36 36
37 -- (id)initWithCoder:(NSCoder *)coder { 37 +- (id)initWithCoder:(NSCoder *)coder
  38 +{
38 self = [super initWithCoder:coder]; 39 self = [super initWithCoder:coder];
  40 +
39 if (self) { 41 if (self) {
40 [self setDefaultValues]; 42 [self setDefaultValues];
41 } 43 }
  44 +
42 return self; 45 return self;
43 } 46 }
44 47
45 -- (id)initWithFrame:(CGRect)frame { 48 +
  49 +- (id)initWithFrame:(CGRect)frame
  50 +{
46 self = [super initWithFrame:frame]; 51 self = [super initWithFrame:frame];
  52 +
47 if (self) { 53 if (self) {
48 [self setDefaultValues]; 54 [self setDefaultValues];
49 } 55 }
  56 +
50 return self; 57 return self;
51 } 58 }
52 59
  60 +
53 #pragma mark instance methods 61 #pragma mark instance methods
54 62
55 --(void)setYLabels:(NSArray *)yLabels 63 +- (void)setYLabels:(NSArray *)yLabels
56 { 64 {
57 - 65 + CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
58 - CGFloat yStep = (_yValueMax-_yValueMin) / _yLabelNum; 66 + CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
59 - CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; 67 +
60 - 68 +
61 -  
62 NSInteger index = 0; 69 NSInteger index = 0;
63 - NSInteger num = _yLabelNum+1; 70 + NSInteger num = _yLabelNum + 1;
64 - while (num > 0) { 71 +
65 - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)]; 72 + while (num > 0) {
66 - [label setTextAlignment:NSTextAlignmentRight]; 73 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)];
67 - label.text = [NSString stringWithFormat:@"%1.f",_yValueMin + (yStep * index)]; 74 + [label setTextAlignment:NSTextAlignmentRight];
68 - [self addSubview:label]; 75 + label.text = [NSString stringWithFormat:@"%1.f", _yValueMin + (yStep * index)];
69 - index +=1 ; 76 + [self addSubview:label];
70 - num -= 1; 77 + index += 1;
71 - } 78 + num -= 1;
72 - 79 + }
73 } 80 }
74 81
75 --(void)setXLabels:(NSArray *)xLabels 82 +
  83 +- (void)setXLabels:(NSArray *)xLabels
76 { 84 {
77 _xLabels = xLabels; 85 _xLabels = xLabels;
78 - NSString* labelText; 86 + NSString *labelText;
79 - if(_showLabel){ 87 +
80 - _xLabelWidth = _chartCavanWidth/[xLabels count]; 88 + if (_showLabel) {
81 - 89 + _xLabelWidth = _chartCavanWidth / [xLabels count];
82 - for(int index = 0; index < xLabels.count; index++) 90 +
83 - { 91 + for (int index = 0; index < xLabels.count; index++) {
84 labelText = xLabels[index]; 92 labelText = xLabels[index];
85 - PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2*_chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)]; 93 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)];
86 [label setTextAlignment:NSTextAlignmentCenter]; 94 [label setTextAlignment:NSTextAlignmentCenter];
87 label.text = labelText; 95 label.text = labelText;
88 [self addSubview:label]; 96 [self addSubview:label];
89 } 97 }
90 -  
91 - }else{  
92 - _xLabelWidth = (self.frame.size.width)/[xLabels count];  
93 } 98 }
94 - 99 + else {
  100 + _xLabelWidth = (self.frame.size.width) / [xLabels count];
  101 + }
95 } 102 }
96 103
97 --(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 104 +
  105 +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
98 { 106 {
99 [self touchPoint:touches withEvent:event]; 107 [self touchPoint:touches withEvent:event];
100 [self touchKeyPoint:touches withEvent:event]; 108 [self touchKeyPoint:touches withEvent:event];
101 } 109 }
102 110
103 --(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 111 +
  112 +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
104 { 113 {
105 [self touchPoint:touches withEvent:event]; 114 [self touchPoint:touches withEvent:event];
106 [self touchKeyPoint:touches withEvent:event]; 115 [self touchKeyPoint:touches withEvent:event];
107 } 116 }
108 117
109 --(void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event 118 +
  119 +- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
110 { 120 {
111 //Get the point user touched 121 //Get the point user touched
112 UITouch *touch = [touches anyObject]; 122 UITouch *touch = [touches anyObject];
113 CGPoint touchPoint = [touch locationInView:self]; 123 CGPoint touchPoint = [touch locationInView:self];
114 - 124 +
115 for (int p = _pathPoints.count - 1; p >= 0; p--) { 125 for (int p = _pathPoints.count - 1; p >= 0; p--) {
116 NSArray *linePointsArray = _pathPoints[p]; 126 NSArray *linePointsArray = _pathPoints[p];
117 - 127 +
118 for (int i = 0; i < linePointsArray.count - 1; i += 1) { 128 for (int i = 0; i < linePointsArray.count - 1; i += 1) {
119 CGPoint p1 = [linePointsArray[i] CGPointValue]; 129 CGPoint p1 = [linePointsArray[i] CGPointValue];
120 - CGPoint p2 = [linePointsArray[i+1] CGPointValue]; 130 + CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
121 - 131 +
122 // Closest distance from point to line 132 // Closest distance from point to line
123 - float distance = fabsf(((p2.x - p1.x)*(touchPoint.y - p1.y))-((p1.x-touchPoint.x)*(p1.y-p2.y))); 133 + float distance = fabsf(((p2.x - p1.x) * (touchPoint.y - p1.y)) - ((p1.x - touchPoint.x) * (p1.y - p2.y)));
124 - distance /= hypot(p2.x-p1.x, p1.y-p2.y); 134 + distance /= hypot(p2.x - p1.x, p1.y - p2.y);
125 - 135 +
126 if (distance <= 5.0) { 136 if (distance <= 5.0) {
127 // Conform to delegate parameters, figure out what bezier path this CGPoint belongs to. 137 // Conform to delegate parameters, figure out what bezier path this CGPoint belongs to.
128 for (UIBezierPath *path in _chartPath) { 138 for (UIBezierPath *path in _chartPath) {
129 BOOL pointContainsPath = CGPathContainsPoint(path.CGPath, NULL, p1, NO); 139 BOOL pointContainsPath = CGPathContainsPoint(path.CGPath, NULL, p1, NO);
130 - 140 +
131 if (pointContainsPath) { 141 if (pointContainsPath) {
132 [_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]]; 142 [_delegate userClickedOnLinePoint:touchPoint lineIndex:[_chartPath indexOfObject:path]];
133 return; 143 return;
@@ -138,121 +148,123 @@ @@ -138,121 +148,123 @@
138 } 148 }
139 } 149 }
140 150
141 --(void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event 151 +
  152 +- (void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event
142 { 153 {
143 //Get the point user touched 154 //Get the point user touched
144 UITouch *touch = [touches anyObject]; 155 UITouch *touch = [touches anyObject];
145 CGPoint touchPoint = [touch locationInView:self]; 156 CGPoint touchPoint = [touch locationInView:self];
146 - 157 +
147 for (int p = _pathPoints.count - 1; p >= 0; p--) { 158 for (int p = _pathPoints.count - 1; p >= 0; p--) {
148 NSArray *linePointsArray = _pathPoints[p]; 159 NSArray *linePointsArray = _pathPoints[p];
149 - 160 +
150 for (int i = 0; i < linePointsArray.count - 1; i += 1) { 161 for (int i = 0; i < linePointsArray.count - 1; i += 1) {
151 CGPoint p1 = [linePointsArray[i] CGPointValue]; 162 CGPoint p1 = [linePointsArray[i] CGPointValue];
152 - CGPoint p2 = [linePointsArray[i+1] CGPointValue]; 163 + CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
153 - 164 +
154 float distanceToP1 = fabsf(hypot(touchPoint.x - p1.x, touchPoint.y - p1.y)); 165 float distanceToP1 = fabsf(hypot(touchPoint.x - p1.x, touchPoint.y - p1.y));
155 float distanceToP2 = hypot(touchPoint.x - p2.x, touchPoint.y - p2.y); 166 float distanceToP2 = hypot(touchPoint.x - p2.x, touchPoint.y - p2.y);
156 - 167 +
157 float distance = MIN(distanceToP1, distanceToP2); 168 float distance = MIN(distanceToP1, distanceToP2);
158 - 169 +
159 if (distance <= 10.0) { 170 if (distance <= 10.0) {
160 [_delegate userClickedOnLineKeyPoint:touchPoint 171 [_delegate userClickedOnLineKeyPoint:touchPoint
161 lineIndex:p 172 lineIndex:p
162 - andPointIndex:(distance == distanceToP2 ? i+1 : i)]; 173 + andPointIndex:(distance == distanceToP2 ? i + 1 : i)];
163 return; 174 return;
164 } 175 }
165 } 176 }
166 } 177 }
167 } 178 }
168 179
169 --(void)strokeChart 180 +
  181 +- (void)strokeChart
170 { 182 {
171 _chartPath = [[NSMutableArray alloc] init]; 183 _chartPath = [[NSMutableArray alloc] init];
  184 +
172 //Draw each line 185 //Draw each line
173 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) { 186 for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
174 PNLineChartData *chartData = self.chartData[lineIndex]; 187 PNLineChartData *chartData = self.chartData[lineIndex];
175 - CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[lineIndex]; 188 + CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex];
176 CGFloat yValue; 189 CGFloat yValue;
177 CGFloat innerGrade; 190 CGFloat innerGrade;
178 CGPoint point; 191 CGPoint point;
179 - 192 +
180 UIGraphicsBeginImageContext(self.frame.size); 193 UIGraphicsBeginImageContext(self.frame.size);
181 - UIBezierPath * progressline = [UIBezierPath bezierPath]; 194 + UIBezierPath *progressline = [UIBezierPath bezierPath];
182 [_chartPath addObject:progressline]; 195 [_chartPath addObject:progressline];
183 -  
184 196
185 - 197 + if (!_showLabel) {
186 - if(!_showLabel){ 198 + _chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight;
187 - _chartCavanHeight = self.frame.size.height - 2*_yLabelHeight;  
188 _chartCavanWidth = self.frame.size.width; 199 _chartCavanWidth = self.frame.size.width;
189 _chartMargin = 0.0; 200 _chartMargin = 0.0;
190 - _xLabelWidth = (_chartCavanWidth / ([_xLabels count] -1)); 201 + _xLabelWidth = (_chartCavanWidth / ([_xLabels count] - 1));
191 } 202 }
192 - 203 +
193 - NSMutableArray * linePointsArray = [[NSMutableArray alloc] init]; 204 + NSMutableArray *linePointsArray = [[NSMutableArray alloc] init];
194 [progressline setLineWidth:3.0]; 205 [progressline setLineWidth:3.0];
195 [progressline setLineCapStyle:kCGLineCapRound]; 206 [progressline setLineCapStyle:kCGLineCapRound];
196 [progressline setLineJoinStyle:kCGLineJoinRound]; 207 [progressline setLineJoinStyle:kCGLineJoinRound];
197 -  
198 -  
199 -  
200 -  
201 - for (NSUInteger i = 0; i < chartData.itemCount; i++) {  
202 208
  209 + for (NSUInteger i = 0; i < chartData.itemCount; i++) {
203 yValue = chartData.getData(i).y; 210 yValue = chartData.getData(i).y;
204 - 211 +
205 - innerGrade = (yValue - _yValueMin) / ( _yValueMax - _yValueMin); 212 + innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin);
206 - 213 +
207 - point = CGPointMake(2*_chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + ( _yLabelHeight /2 )); 214 + point = CGPointMake(2 * _chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2));
208 - 215 +
209 if (i != 0) { 216 if (i != 0) {
210 [progressline addLineToPoint:point]; 217 [progressline addLineToPoint:point];
211 } 218 }
212 - 219 +
213 [progressline moveToPoint:point]; 220 [progressline moveToPoint:point];
214 [linePointsArray addObject:[NSValue valueWithCGPoint:point]]; 221 [linePointsArray addObject:[NSValue valueWithCGPoint:point]];
215 } 222 }
  223 +
216 [_pathPoints addObject:[linePointsArray copy]]; 224 [_pathPoints addObject:[linePointsArray copy]];
  225 +
217 // setup the color of the chart line 226 // setup the color of the chart line
218 if (chartData.color) { 227 if (chartData.color) {
219 chartLine.strokeColor = [chartData.color CGColor]; 228 chartLine.strokeColor = [chartData.color CGColor];
220 - }else{ 229 + }
  230 + else {
221 chartLine.strokeColor = [PNGreen CGColor]; 231 chartLine.strokeColor = [PNGreen CGColor];
222 } 232 }
223 - 233 +
224 [progressline stroke]; 234 [progressline stroke];
225 - 235 +
226 chartLine.path = progressline.CGPath; 236 chartLine.path = progressline.CGPath;
227 - 237 +
228 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 238 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
229 pathAnimation.duration = 1.0; 239 pathAnimation.duration = 1.0;
230 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 240 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
231 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 241 pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
232 pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 242 pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
233 [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 243 [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
234 - 244 +
235 chartLine.strokeEnd = 1.0; 245 chartLine.strokeEnd = 1.0;
236 - 246 +
237 UIGraphicsEndImageContext(); 247 UIGraphicsEndImageContext();
238 } 248 }
239 } 249 }
240 250
241 -- (void)setChartData:(NSArray *)data { 251 +
  252 +- (void)setChartData:(NSArray *)data
  253 +{
242 if (data != _chartData) { 254 if (data != _chartData) {
243 -  
244 NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count]; 255 NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count];
245 CGFloat yMax = 0.0f; 256 CGFloat yMax = 0.0f;
246 CGFloat yMin = MAXFLOAT; 257 CGFloat yMin = MAXFLOAT;
247 CGFloat yValue; 258 CGFloat yValue;
  259 +
248 // remove all shape layers before adding new ones 260 // remove all shape layers before adding new ones
249 for (CALayer *layer in self.chartLineArray) { 261 for (CALayer *layer in self.chartLineArray) {
250 [layer removeFromSuperlayer]; 262 [layer removeFromSuperlayer];
251 } 263 }
  264 +
252 self.chartLineArray = [NSMutableArray arrayWithCapacity:data.count]; 265 self.chartLineArray = [NSMutableArray arrayWithCapacity:data.count];
253 - 266 +
254 for (PNLineChartData *chartData in data) { 267 for (PNLineChartData *chartData in data) {
255 -  
256 // create as many chart line layers as there are data-lines 268 // create as many chart line layers as there are data-lines
257 CAShapeLayer *chartLine = [CAShapeLayer layer]; 269 CAShapeLayer *chartLine = [CAShapeLayer layer];
258 chartLine.lineCap = kCALineCapRound; 270 chartLine.lineCap = kCALineCapRound;
@@ -262,7 +274,7 @@ @@ -262,7 +274,7 @@
262 chartLine.strokeEnd = 0.0; 274 chartLine.strokeEnd = 0.0;
263 [self.layer addSublayer:chartLine]; 275 [self.layer addSublayer:chartLine];
264 [self.chartLineArray addObject:chartLine]; 276 [self.chartLineArray addObject:chartLine];
265 - 277 +
266 for (NSUInteger i = 0; i < chartData.itemCount; i++) { 278 for (NSUInteger i = 0; i < chartData.itemCount; i++) {
267 yValue = chartData.getData(i).y; 279 yValue = chartData.getData(i).y;
268 [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]]; 280 [yLabelsArray addObject:[NSString stringWithFormat:@"%2f", yValue]];
@@ -270,31 +282,34 @@ @@ -270,31 +282,34 @@
270 yMin = fminf(yMin, yValue); 282 yMin = fminf(yMin, yValue);
271 } 283 }
272 } 284 }
273 - 285 +
274 // Min value for Y label 286 // Min value for Y label
275 if (yMax < 5) { 287 if (yMax < 5) {
276 yMax = 5.0f; 288 yMax = 5.0f;
277 } 289 }
278 - if (yMin < 0){ 290 +
  291 + if (yMin < 0) {
279 yMin = 0.0f; 292 yMin = 0.0f;
280 } 293 }
281 - 294 +
282 _yValueMin = yMin; 295 _yValueMin = yMin;
283 _yValueMax = yMax; 296 _yValueMax = yMax;
284 - 297 +
285 _chartData = data; 298 _chartData = data;
286 - 299 +
287 if (_showLabel) { 300 if (_showLabel) {
288 [self setYLabels:yLabelsArray]; 301 [self setYLabels:yLabelsArray];
289 } 302 }
290 - 303 +
291 [self setNeedsDisplay]; 304 [self setNeedsDisplay];
292 } 305 }
293 } 306 }
294 307
  308 +
295 #pragma mark private methods 309 #pragma mark private methods
296 310
297 -- (void)setDefaultValues { 311 +- (void)setDefaultValues
  312 +{
298 // Initialization code 313 // Initialization code
299 self.backgroundColor = [UIColor whiteColor]; 314 self.backgroundColor = [UIColor whiteColor];
300 self.clipsToBounds = YES; 315 self.clipsToBounds = YES;
@@ -302,14 +317,15 @@ @@ -302,14 +317,15 @@
302 _showLabel = YES; 317 _showLabel = YES;
303 _pathPoints = [[NSMutableArray alloc] init]; 318 _pathPoints = [[NSMutableArray alloc] init];
304 self.userInteractionEnabled = YES; 319 self.userInteractionEnabled = YES;
305 - 320 +
306 _yLabelNum = 5.0; 321 _yLabelNum = 5.0;
307 _yLabelHeight = [[[[PNChartLabel alloc] init] font] pointSize]; 322 _yLabelHeight = [[[[PNChartLabel alloc] init] font] pointSize];
308 - 323 +
309 _chartMargin = 30; 324 _chartMargin = 30;
310 325
311 - _chartCavanWidth = self.frame.size.width - _chartMargin *2; 326 + _chartCavanWidth = self.frame.size.width - _chartMargin * 2;
312 _chartCavanHeight = self.frame.size.height - _chartMargin * 2; 327 _chartCavanHeight = self.frame.size.height - _chartMargin * 2;
313 } 328 }
314 329
  330 +
315 @end 331 @end
@@ -7,18 +7,12 @@ @@ -7,18 +7,12 @@
7 7
8 @class PNLineChartDataItem; 8 @class PNLineChartDataItem;
9 9
10 -  
11 typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item); 10 typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item);
12 11
13 -/** 12 +
14 -*  
15 -*/  
16 @interface PNLineChartData : NSObject 13 @interface PNLineChartData : NSObject
17 14
18 @property (strong) UIColor *color; 15 @property (strong) UIColor *color;
19 -  
20 @property NSUInteger itemCount; 16 @property NSUInteger itemCount;
21 -  
22 @property (copy) LCLineChartDataGetter getData; 17 @property (copy) LCLineChartDataGetter getData;
23 -  
24 @end 18 @end
@@ -5,10 +5,10 @@ @@ -5,10 +5,10 @@
5 5
6 #import <Foundation/Foundation.h> 6 #import <Foundation/Foundation.h>
7 7
8 -@interface PNLineChartDataItem : NSObject  
9 8
10 -@property (readonly) CGFloat y; // should be within the y range 9 +@interface PNLineChartDataItem : NSObject
11 10
12 + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y; 11 + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y;
13 12
  13 +@property (readonly) CGFloat y; // should be within the y range
14 @end 14 @end
@@ -5,30 +5,35 @@ @@ -5,30 +5,35 @@
5 5
6 #import "PNLineChartDataItem.h" 6 #import "PNLineChartDataItem.h"
7 7
  8 +
8 //------------------------------------------------------------------------------------------------ 9 //------------------------------------------------------------------------------------------------
9 // private interface declaration 10 // private interface declaration
10 //------------------------------------------------------------------------------------------------ 11 //------------------------------------------------------------------------------------------------
11 @interface PNLineChartDataItem () 12 @interface PNLineChartDataItem ()
  13 +- (id)initWithY:(CGFloat)y;
12 14
13 @property (readwrite) CGFloat y; // should be within the y range 15 @property (readwrite) CGFloat y; // should be within the y range
  16 +@end
14 17
15 -- (id)initWithY:(CGFloat)y;  
16 18
17 -@end  
18 19
19 //------------------------------------------------------------------------------------------------ 20 //------------------------------------------------------------------------------------------------
20 // public interface implementation 21 // public interface implementation
21 //------------------------------------------------------------------------------------------------ 22 //------------------------------------------------------------------------------------------------
22 @implementation PNLineChartDataItem 23 @implementation PNLineChartDataItem
23 24
24 -+ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y { 25 ++ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y
  26 +{
25 return [[PNLineChartDataItem alloc] initWithY:y]; 27 return [[PNLineChartDataItem alloc] initWithY:y];
26 } 28 }
27 29
28 -- (id)initWithY:(CGFloat)y { 30 +
  31 +- (id)initWithY:(CGFloat)y
  32 +{
29 if ((self = [super init])) { 33 if ((self = [super init])) {
30 self.y = y; 34 self.y = y;
31 } 35 }
  36 +
32 return self; 37 return self;
33 } 38 }
34 39
@@ -7,4 +7,4 @@ DEPENDENCIES: @@ -7,4 +7,4 @@ DEPENDENCIES:
7 SPEC CHECKSUMS: 7 SPEC CHECKSUMS:
8 UICountingLabel: bb976960c0130785a600aac592954c23eb395096 8 UICountingLabel: bb976960c0130785a600aac592954c23eb395096
9 9
10 -COCOAPODS: 0.29.0 10 +COCOAPODS: 0.32.1