单军华
2016-12-21 8329ef237d1841d377718813a0b452ea0df64378
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
//
//  JHRadarChart.m
//  JHChartDemo
//
//  Created by 简豪 on 16/9/9.
//  Copyright © 2016年 JH. All rights reserved.
//
 
#import "JHRadarChart.h"
 
@interface JHRadarChart ()
 
@property (nonatomic,assign)CGFloat chartRadius;
@property (nonatomic,strong)NSMutableArray * drawPointArray;
 
@property (nonatomic,strong)NSMutableArray * baseDrawPointArray;
@end
 
 
@implementation JHRadarChart
 
 
-(NSMutableArray *)drawPointArray{
    
    if (!_drawPointArray) {
        _drawPointArray = [NSMutableArray array];
    }
    
    return _drawPointArray;
}
 
 
-(NSMutableArray *)baseDrawPointArray{
    
    if (!_baseDrawPointArray) {
        _baseDrawPointArray = [NSMutableArray array];
    }
    return _baseDrawPointArray;
}
 
-(void)setLayerCount:(NSInteger)layerCount{
    
    if (layerCount<=0) {
        return;
    }
    
    _layerCount = layerCount;
    
}
 
-(instancetype)initWithFrame:(CGRect)frame{
    
 
    if (self = [super initWithFrame:frame]) {
        
        self.layerCount = 3;
        self.chartRadius = (self.frame.size.height - 50 * 2) / 2.0;
        self.chartOrigin = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight(self.frame) / 2.0);
        self.layerFillColor = [UIColor colorWithWhite:0.5 alpha:0.3];
        self.layerBoardColor = [UIColor colorWithWhite:0.5 alpha:0.3];
        self.speraLineColor = [UIColor whiteColor];
        self.perfectNumber = 100.0;
        self.descTextFont = [UIFont systemFontOfSize:14];
        self.descTextColor = [UIColor darkGrayColor];
    }
    
    
    return self;
    
}
 
 
/**
 *  初始化点数组
 */
- (void)configDrawingData{
    
    
    if (self.valueDataArray.count==0) {
        return;
    }
    
 
    CGFloat perAngle = M_PI * 2 / self.valueDescArray.count;
    for (NSInteger i = 0; i<self.valueDataArray.count; i++) {
        
        NSArray *valueArray = [self.valueDataArray objectAtIndex:i];
        NSMutableArray *cacheArray = [NSMutableArray array];
        
        for (NSInteger j = 0; j<valueArray.count; j++) {
            CGFloat value = [[valueArray objectAtIndex:j] floatValue];
            
            value = (value>self.perfectNumber?self.perfectNumber:value);
            
            CGPoint cachePoint = CGPointMake(self.chartOrigin.x + value / self.perfectNumber * self.chartRadius * sin(j * perAngle), self.chartOrigin.y - value / self.perfectNumber * self.chartRadius * cos(j * perAngle));
            [cacheArray addObject:[NSValue valueWithCGPoint:cachePoint]];
            
        }
        
        [self.drawPointArray addObject:[cacheArray copy]];
        
    }
    
}
 
 
- (void)configBaseViewDataArray{
    
    [self.baseDrawPointArray removeAllObjects];
    CGFloat perLength = self.chartRadius / self.layerCount;
    
    CGFloat perAngle = M_PI * 2 / self.valueDescArray.count;
    
    for (NSInteger i = 0; i<self.layerCount; i++) {
        
        NSMutableArray *cacheArray = [NSMutableArray array];
        CGFloat cacheLength = (i+1) * perLength;
        for (NSInteger j = 0; j<self.valueDescArray.count; j++) {
            
            CGPoint cachePoint = CGPointMake(self.chartOrigin.x + cacheLength * sin(j * perAngle) , self.chartOrigin.y - cacheLength * cos(j * perAngle));
            
//            NSLog(@"-----%-----i== %ld     ======%@",j * perAngle,i,NSStringFromCGPoint(cachePoint));
            NSValue *cacheValue = [NSValue valueWithCGPoint:cachePoint];
            [cacheArray addObject:cacheValue];
            
            
            if (i==0) {
                
                CGFloat width = [self sizeOfStringWithMaxSize:CGSizeMake(100, 20) textFont:self.descTextFont.pointSize aimString:self.valueDescArray[j]].width;
                UILabel *cacheLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 20)];
                cacheLabel.font = self.descTextFont;
                cacheLabel.center = CGPointMake(self.chartOrigin.x + (self.chartRadius + width / 2 + 5) * sin(j * perAngle) ,self.chartOrigin.y - (self.chartRadius + 20 / 2 + 5) * cos(j * perAngle));
                cacheLabel.text = self.valueDescArray[j];
                cacheLabel.textColor = self.descTextColor;
                [self addSubview:cacheLabel];
            }
 
            
        }
        
        [self.baseDrawPointArray addObject:[cacheArray copy]];
        [cacheArray removeAllObjects];
        
    }
    
    
    
}
 
 
/**
 *  添加基本的视图模块
 */
- (void)drawBaseView{
    for (NSInteger i = self.baseDrawPointArray.count-1; i>=0; i--) {
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        NSArray * cacheArray = [self.baseDrawPointArray objectAtIndex:i];
        for (NSInteger j = 0; j<cacheArray.count; j++) {
            
            NSValue *cacheValue = [cacheArray objectAtIndex:j];
            CGPoint currentCachePoint = [cacheValue CGPointValue];
            
            
            NSLog(@"i== %ld     ======%@",i,NSStringFromCGPoint(currentCachePoint));
            if (j==0) {
                [path moveToPoint:currentCachePoint];
            }else if(j==cacheArray.count){
                [path addLineToPoint:currentCachePoint];
                [path moveToPoint:currentCachePoint];
            }else{
                [path addLineToPoint:currentCachePoint];
            }
        }
        
        [path closePath];
        shapeLayer.path = path.CGPath;
        shapeLayer.fillColor = self.layerFillColor.CGColor;
        shapeLayer.strokeColor = self.layerBoardColor.CGColor;
        [self.layer addSublayer:shapeLayer];
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path closePath];
    [path moveToPoint:self.chartOrigin];
    NSMutableArray * cacheArray = [self.baseDrawPointArray lastObject];
    for (NSInteger j = 0; j<cacheArray.count; j++) {
        NSValue *cacheValue = [cacheArray objectAtIndex:j];
        CGPoint currentCachePoint = [cacheValue CGPointValue];
        
        [path addLineToPoint:currentCachePoint];
        [path moveToPoint:self.chartOrigin];
    }
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = self.speraLineColor.CGColor;
    [self.layer addSublayer:shapeLayer];
    
    
    
 
}
 
 
- (void)drawValueView{
    
    
    if (self.drawPointArray.count==0) {
        return;
    }
    
    
    for (NSInteger i = 0 ; i<self.drawPointArray.count; i++) {
        
        NSArray *cacheArray = [self.drawPointArray objectAtIndex:i];
        UIBezierPath *path = [UIBezierPath bezierPath];
        for (NSInteger j = 0; j<cacheArray.count; j++) {
            
            
            
            if (j==0) {
                [path moveToPoint:[[cacheArray objectAtIndex:j] CGPointValue]];
            }else{
                [path addLineToPoint:[[cacheArray objectAtIndex:j] CGPointValue]];
            }
 
        }
        [path closePath];
        CAShapeLayer *shaper = [CAShapeLayer layer];
        shaper.path = path.CGPath;
        shaper.borderWidth = 1.0;
        UIColor *cacheColor = [UIColor clearColor];
        if (self.valueDrawFillColorArray.count>i) {
            cacheColor = self.valueDrawFillColorArray[i];
        }
        shaper.fillColor = cacheColor.CGColor;
        
        
        if (self.valueBoardColorArray.count>i) {
            cacheColor = self.valueBoardColorArray[i];
        }else{
            cacheColor = [UIColor clearColor];
        }
        shaper.strokeColor = cacheColor.CGColor;
        
        
        [self.layer addSublayer:shaper];
        
    }
    
    
    
}
 
-(void)showAnimation{
    
    [self configBaseViewDataArray];
    [self configDrawingData];
    [self drawBaseView];
    [self drawValueView];
    
}
 
 
 
 
 
@end