-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUULineChart.m
executable file
·279 lines (244 loc) · 9.55 KB
/
UULineChart.m
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// UULineChart.m
// UUChartDemo
//
// Created by shake on 14-7-24.
// Copyright (c) 2014年 uyiuyao. All rights reserved.
//
#import "UULineChart.h"
#import "UUColor.h"
#import "UUChartLabel.h"
@implementation UULineChart
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.clipsToBounds = YES;
}
return self;
}
-(void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
[self setYLabels:yValues];
}
-(void)setYLabels:(NSArray *)yLabels
{
NSInteger max = 0;
NSInteger min = 1000000000;
for (NSArray *ary in yLabels) {
for (NSString *valueString in ary) {
NSInteger value = [valueString integerValue];
if (value > max) {
max = value;
}
if (value < min) {
min = value;
}
}
}
if (max < 5) {
max = 5;
}
if (self.showRange) {
_yValueMin = min;
}else{
_yValueMin = 0;
}
_yValueMax = (int)max;
if (_chooseRange.max!=_chooseRange.min) {
_yValueMax = _chooseRange.max;
_yValueMin = _chooseRange.min;
}
float level = (_yValueMax-_yValueMin) /4.0;
CGFloat chartCavanHeight = self.frame.size.height - UULabelHeight*3;
CGFloat levelHeight = chartCavanHeight /4.0;
for (int i=0; i<5; i++) {
UUChartLabel * label = [[UUChartLabel alloc] initWithFrame:CGRectMake(0.0,chartCavanHeight-i*levelHeight+5, UUYLabelwidth, UULabelHeight)];
label.text = [NSString stringWithFormat:@"%d",(int)(level * i+_yValueMin)];
[self addSubview:label];
}
if ([super respondsToSelector:@selector(setMarkRange:)]) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(UUYLabelwidth, (1-(_markRange.max-_yValueMin)/(_yValueMax-_yValueMin))*chartCavanHeight+UULabelHeight, self.frame.size.width-UUYLabelwidth, (_markRange.max-_markRange.min)/(_yValueMax-_yValueMin)*chartCavanHeight)];
view.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.1];
[self addSubview:view];
}
//画横线
for (int i=0; i<5; i++) {
if ([_ShowHorizonLine[i] integerValue]>0) {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(UUYLabelwidth,UULabelHeight+i*levelHeight)];
[path addLineToPoint:CGPointMake(self.frame.size.width,UULabelHeight+i*levelHeight)];
[path closePath];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [[[UIColor blackColor] colorWithAlphaComponent:0.1] CGColor];
shapeLayer.fillColor = [[UIColor whiteColor] CGColor];
shapeLayer.lineWidth = 1;
[self.layer addSublayer:shapeLayer];
}
}
}
-(void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
CGFloat num = 0;
if (xLabels.count>=20) {
num=20.0;
}else if (xLabels.count<=1){
num=1.0;
}else{
num = xLabels.count;
}
_xLabelWidth = (self.frame.size.width - UUYLabelwidth)/num;
for (int i=0; i<xLabels.count; i++) {
NSString *labelText = xLabels[i];
UUChartLabel * label = [[UUChartLabel alloc] initWithFrame:CGRectMake(i * _xLabelWidth+UUYLabelwidth, self.frame.size.height - UULabelHeight, _xLabelWidth, UULabelHeight)];
label.text = labelText;
[self addSubview:label];
}
//画竖线
for (int i=0; i<xLabels.count+1; i++) {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(UUYLabelwidth+i*_xLabelWidth,UULabelHeight)];
[path addLineToPoint:CGPointMake(UUYLabelwidth+i*_xLabelWidth,self.frame.size.height-2*UULabelHeight)];
[path closePath];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [[[UIColor blackColor] colorWithAlphaComponent:0.1] CGColor];
shapeLayer.fillColor = [[UIColor whiteColor] CGColor];
shapeLayer.lineWidth = 1;
[self.layer addSublayer:shapeLayer];
}
}
-(void)setColors:(NSArray *)colors
{
_colors = colors;
}
- (void)setMarkRange:(CGRange)markRange
{
_markRange = markRange;
}
- (void)setChooseRange:(CGRange)chooseRange
{
_chooseRange = chooseRange;
}
- (void)setShowHorizonLine:(NSMutableArray *)ShowHorizonLine
{
_ShowHorizonLine = ShowHorizonLine;
}
-(void)strokeChart
{
for (int i=0; i<_yValues.count; i++) {
NSArray *childAry = _yValues[i];
if (childAry.count==0) {
return;
}
//获取最大最小位置
CGFloat max = [childAry[0] floatValue];
CGFloat min = [childAry[0] floatValue];
NSInteger max_i;
NSInteger min_i;
for (int j=0; j<childAry.count; j++){
CGFloat num = [childAry[j] floatValue];
if (max<=num){
max = num;
max_i = j;
}
if (min>=num){
min = num;
min_i = j;
}
}
//划线
CAShapeLayer *_chartLine = [CAShapeLayer layer];
_chartLine.lineCap = kCALineCapRound;
_chartLine.lineJoin = kCALineJoinBevel;
_chartLine.fillColor = [[UIColor whiteColor] CGColor];
_chartLine.lineWidth = 2.0;
_chartLine.strokeEnd = 0.0;
[self.layer addSublayer:_chartLine];
UIBezierPath *progressline = [UIBezierPath bezierPath];
CGFloat firstValue = [[childAry objectAtIndex:0] floatValue];
CGFloat xPosition = (UUYLabelwidth + _xLabelWidth/2.0);
CGFloat chartCavanHeight = self.frame.size.height - UULabelHeight*3;
float grade = ((float)firstValue-_yValueMin) / ((float)_yValueMax-_yValueMin);
//第一个点
BOOL isShowMaxAndMinPoint = YES;
if (self.ShowMaxMinArray) {
if ([self.ShowMaxMinArray[i] intValue]>0) {
isShowMaxAndMinPoint = (max_i==0 || min_i==0)?NO:YES;
}else{
isShowMaxAndMinPoint = YES;
}
}
[self addPoint:CGPointMake(xPosition, chartCavanHeight - grade * chartCavanHeight+UULabelHeight)
index:i
isShow:isShowMaxAndMinPoint
value:firstValue];
[progressline moveToPoint:CGPointMake(xPosition, chartCavanHeight - grade * chartCavanHeight+UULabelHeight)];
[progressline setLineWidth:2.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
NSInteger index = 0;
for (NSString * valueString in childAry) {
float grade =([valueString floatValue]-_yValueMin) / ((float)_yValueMax-_yValueMin);
if (index != 0) {
CGPoint point = CGPointMake(xPosition+index*_xLabelWidth, chartCavanHeight - grade * chartCavanHeight+UULabelHeight);
[progressline addLineToPoint:point];
BOOL isShowMaxAndMinPoint = YES;
if (self.ShowMaxMinArray) {
if ([self.ShowMaxMinArray[i] intValue]>0) {
isShowMaxAndMinPoint = (max_i==index || min_i==index)?NO:YES;
}else{
isShowMaxAndMinPoint = YES;
}
}
[progressline moveToPoint:point];
[self addPoint:point
index:i
isShow:isShowMaxAndMinPoint
value:[valueString floatValue]];
// [progressline stroke];
}
index += 1;
}
_chartLine.path = progressline.CGPath;
if ([[_colors objectAtIndex:i] CGColor]) {
_chartLine.strokeColor = [[_colors objectAtIndex:i] CGColor];
}else{
_chartLine.strokeColor = [UUGreen CGColor];
}
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = childAry.count*0.4;
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;
}
}
- (void)addPoint:(CGPoint)point index:(NSInteger)index isShow:(BOOL)isHollow value:(CGFloat)value
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 8, 8)];
view.center = point;
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 4;
view.layer.borderWidth = 2;
view.layer.borderColor = [[_colors objectAtIndex:index] CGColor]?[[_colors objectAtIndex:index] CGColor]:UUGreen.CGColor;
if (isHollow) {
view.backgroundColor = [UIColor whiteColor];
}else{
view.backgroundColor = [_colors objectAtIndex:index]?[_colors objectAtIndex:index]:UUGreen;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point.x-UUTagLabelwidth/2.0, point.y-UULabelHeight*2, UUTagLabelwidth, UULabelHeight)];
label.font = [UIFont systemFontOfSize:10];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = view.backgroundColor;
label.text = [NSString stringWithFormat:@"%d",(int)value];
[self addSubview:label];
}
[self addSubview:view];
}
@end