PNLineChart.m
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// PNLineChart.m
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import "PNLineChart.h"
#import "PNColor.h"
#import "PNChartLabel.h"
@implementation PNLineChart
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
_chartLine = [CAShapeLayer layer];
_chartLine.lineCap = kCALineCapRound;
_chartLine.lineJoin = kCALineJoinBevel;
_chartLine.fillColor = [[UIColor whiteColor] CGColor];
_chartLine.lineWidth = 3.0;
_chartLine.strokeEnd = 0.0;
_showLabel = YES;
[self.layer addSublayer:_chartLine];
}
return self;
}
-(void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
_xLabelWidth = (self.frame.size.width)/[_yValues count];
float max = 0;
for (NSString * valueString in yValues) {
float value = [valueString floatValue];
if (value > max) {
max = value;
}
}
//Min value for Y label
if (max < 5) {
max = 5;
}
_yValueMax = (float)max;
if (_showLabel) {
[self setYLabels:yValues];
}
}
-(void)setYLabels:(NSArray *)yLabels
{
float level = _yValueMax /[yLabels count];
NSInteger index = 0;
NSInteger num = [yLabels count] + 1;
while (num > 0) {
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0 ;
CGFloat levelHeight = chartCavanHeight /5.0;
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0,chartCavanHeight - index * levelHeight + (levelHeight - yLabelHeight) , 20.0, yLabelHeight)];
[label setTextAlignment:NSTextAlignmentRight];
label.text = [NSString stringWithFormat:@"%1.f",level * index];
[self addSubview:label];
index +=1 ;
num -= 1;
}
}
-(void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
if(_showLabel){
_xLabelWidth = (self.frame.size.width - chartMargin - 30.0)/[xLabels count];
for (NSString * labelText in xLabels) {
NSInteger index = [xLabels indexOfObject:labelText];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + 30.0, self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
}
}
}
-(void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
_chartLine.strokeColor = [strokeColor CGColor];
}
-(void)strokeChart
{
UIGraphicsBeginImageContext(self.frame.size);
UIBezierPath *progressline = [UIBezierPath bezierPath];
CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue];
CGFloat xPosition = 0;
CGFloat chartCavanHeight = self.frame.size.height - 40.0;
if(_showLabel){
chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
xPosition = _xLabelWidth;
}
float grade = (float)firstValue / (float)_yValueMax;
[progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + 20.0)];
[progressline setLineWidth:3.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
NSInteger index = 0;
for (NSString * valueString in _yValues) {
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
if (index != 0) {
[progressline addLineToPoint:CGPointMake(index * _xLabelWidth + 30.0+ _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0)];
[progressline moveToPoint:CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0 )];
}
index += 1;
}
[progressline stroke];
_chartLine.path = progressline.CGPath;
if (_strokeColor) {
_chartLine.strokeColor = [_strokeColor CGColor];
}else{
_chartLine.strokeColor = [PNGreen CGColor];
}
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
pathAnimation.autoreverses = NO;
[_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_chartLine.strokeEnd = 1.0;
UIGraphicsEndImageContext();
}
@end