forked from johnnywjy/JYRadarChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJYRadarChart.m
More file actions
251 lines (215 loc) · 8.57 KB
/
Copy pathJYRadarChart.m
File metadata and controls
251 lines (215 loc) · 8.57 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
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
//
// JYRadarChart.m
// JYRadarChart
//
// Created by jy on 13-10-31.
// Copyright (c) 2013年 wcode. All rights reserved.
//
#import "JYRadarChart.h"
#import "JYLegendView.h"
#define PADDING 13
#define LEGEND_PADDING 3
#define ATTRIBUTE_TEXT_SIZE 10
#define COLOR_HUE_STEP 5
#define MAX_NUM_OF_COLOR 17
@interface JYRadarChart ()
@property (nonatomic, assign) NSUInteger numOfV;
@property (nonatomic, strong) JYLegendView *legendView;
@property (nonatomic, strong) UIFont *scaleFont;
@end
@implementation JYRadarChart
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
_maxValue = 100.0;
_centerPoint = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
_r = MIN(self.frame.size.width / 2 - PADDING, self.frame.size.height / 2 - PADDING);
_steps = 1;
_drawPoints = NO;
_showLegend = NO;
_showStepText = NO;
_fillArea = NO;
_minValue = 0;
_colorOpacity = 1.0;
_backgroundLineColor = [UIColor darkGrayColor];
self.legendView = [[JYLegendView alloc] initWithFrame:CGRectMake(frame.size.width - 60, 10, 50, 70)];
self.legendView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
self.legendView.backgroundColor = [UIColor clearColor];
self.legendView.colors = [NSMutableArray array];
self.attributes = @[@"you", @"should", @"set", @"these", @"data", @"titles,",
@"this", @"is", @"just", @"a", @"placeholder"];
self.scaleFont = [UIFont systemFontOfSize:ATTRIBUTE_TEXT_SIZE];
}
return self;
}
- (void)setShowLegend:(BOOL)showLegend {
_showLegend = showLegend;
if (_showLegend) {
[self addSubview:self.legendView];
}
else {
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:[JYLegendView class]]) {
[subView removeFromSuperview];
}
}
}
}
- (void)setTitles:(NSArray *)titles {
self.legendView.titles = titles;
}
- (void)setColors:(NSArray *)colors {
[self.legendView.colors removeAllObjects];
for (UIColor *color in colors) {
[self.legendView.colors addObject:[color colorWithAlphaComponent:self.colorOpacity]];
}
}
- (void)setNeedsDisplay {
[super setNeedsDisplay];
[self.legendView sizeToFit];
[self.legendView setNeedsDisplay];
}
- (void)setDataSeries:(NSArray *)dataSeries {
_dataSeries = dataSeries;
_numOfV = [_dataSeries[0] count];
if (self.legendView.colors.count < _dataSeries.count) {
for (int i = 0; i < _dataSeries.count; i++) {
UIColor *color = [UIColor colorWithHue:1.0 * (i * COLOR_HUE_STEP % MAX_NUM_OF_COLOR) / MAX_NUM_OF_COLOR
saturation:1
brightness:1
alpha:self.colorOpacity];
self.legendView.colors[i] = color;
}
}
}
- (void)layoutSubviews {
[self.legendView sizeToFit];
CGRect r = self.legendView.frame;
r.origin.x = self.frame.size.width - self.legendView.frame.size.width - LEGEND_PADDING;
r.origin.y = LEGEND_PADDING;
self.legendView.frame = r;
[self bringSubviewToFront:self.legendView];
}
- (void)drawRect:(CGRect)rect {
NSArray *colors = [self.legendView.colors copy];
CGFloat radPerV = M_PI * 2 / _numOfV;
CGContextRef context = UIGraphicsGetCurrentContext();
//draw attribute text
CGFloat height = [self.scaleFont lineHeight];
CGFloat padding = 2.0;
for (int i = 0; i < _numOfV; i++) {
NSString *attributeName = _attributes[i];
CGPoint pointOnEdge = CGPointMake(_centerPoint.x - _r * sin(i * radPerV), _centerPoint.y - _r * cos(i * radPerV));
CGSize attributeTextSize = JY_TEXT_SIZE(attributeName, self.scaleFont);
NSInteger width = attributeTextSize.width;
CGFloat xOffset = pointOnEdge.x >= _centerPoint.x ? width / 2.0 + padding : -width / 2.0 - padding;
CGFloat yOffset = pointOnEdge.y >= _centerPoint.y ? height / 2.0 + padding : -height / 2.0 - padding;
CGPoint legendCenter = CGPointMake(pointOnEdge.x + xOffset, pointOnEdge.y + yOffset);
if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) {
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode:NSLineBreakByClipping];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSDictionary *attributes = @{ NSFontAttributeName: self.scaleFont,
NSParagraphStyleAttributeName: paragraphStyle };
[attributeName drawInRect:CGRectMake(legendCenter.x - width / 2.0,
legendCenter.y - height / 2.0,
width,
height)
withAttributes:attributes];
} else {
[attributeName drawInRect:CGRectMake(legendCenter.x - width / 2.0,
legendCenter.y - height / 2.0,
width,
height)
withFont:self.scaleFont
lineBreakMode:NSLineBreakByClipping
alignment:NSTextAlignmentCenter];
}
}
//draw steps line
//static CGFloat dashedPattern[] = {3,3};
//TODO: make this color a variable
[[UIColor lightGrayColor] setStroke];
CGContextSaveGState(context);
for (int step = 1; step <= _steps; step++) {
for (int i = 0; i <= _numOfV; ++i) {
if (i == 0) {
CGContextMoveToPoint(context, _centerPoint.x, _centerPoint.y - _r * step / _steps);
}
else {
// CGContextSetLineDash(context, 0, dashedPattern, 2);
CGContextAddLineToPoint(context, _centerPoint.x - _r * sin(i * radPerV) * step / _steps,
_centerPoint.y - _r * cos(i * radPerV) * step / _steps);
}
}
CGContextStrokePath(context);
}
CGContextRestoreGState(context);
//draw lines from center
//TODO: make this color a variable
[[UIColor darkGrayColor] setStroke];
for (int i = 0; i < _numOfV; i++) {
CGContextMoveToPoint(context, _centerPoint.x, _centerPoint.y);
CGContextAddLineToPoint(context, _centerPoint.x - _r * sin(i * radPerV),
_centerPoint.y - _r * cos(i * radPerV));
CGContextStrokePath(context);
}
//end of base except axis label
CGContextSetLineWidth(context, 2.0);
//draw lines
for (int serie = 0; serie < [_dataSeries count]; serie++) {
if (self.fillArea) {
[colors[serie] setFill];
}
else {
[colors[serie] setStroke];
}
for (int i = 0; i < _numOfV; ++i) {
CGFloat value = [_dataSeries[serie][i] floatValue];
if (i == 0) {
CGContextMoveToPoint(context, _centerPoint.x, _centerPoint.y - (value - _minValue) / (_maxValue - _minValue) * _r);
}
else {
CGContextAddLineToPoint(context, _centerPoint.x - (value - _minValue) / (_maxValue - _minValue) * _r * sin(i * radPerV),
_centerPoint.y - (value - _minValue) / (_maxValue - _minValue) * _r * cos(i * radPerV));
}
}
CGFloat value = [_dataSeries[serie][0] floatValue];
CGContextAddLineToPoint(context, _centerPoint.x, _centerPoint.y - (value - _minValue) / (_maxValue - _minValue) * _r);
if (self.fillArea) {
CGContextFillPath(context);
}
else {
CGContextStrokePath(context);
}
//draw data points
if (_drawPoints) {
for (int i = 0; i < _numOfV; i++) {
CGFloat value = [_dataSeries[serie][i] floatValue];
CGFloat xVal = _centerPoint.x - (value - _minValue) / (_maxValue - _minValue) * _r * sin(i * radPerV);
CGFloat yVal = _centerPoint.y - (value - _minValue) / (_maxValue - _minValue) * _r * cos(i * radPerV);
[colors[serie] setFill];
CGContextFillEllipseInRect(context, CGRectMake(xVal - 4, yVal - 4, 8, 8));
[self.backgroundColor setFill];
CGContextFillEllipseInRect(context, CGRectMake(xVal - 2, yVal - 2, 4, 4));
}
}
}
if (self.showStepText) {
//draw step label text, alone y axis
//TODO: make this color a variable
[[UIColor blackColor] setFill];
for (int step = 0; step <= _steps; step++) {
CGFloat value = _minValue + (_maxValue - _minValue) * step / _steps;
NSString *currentLabel = [NSString stringWithFormat:@"%.0f", value];
JY_DRAW_TEXT_IN_RECT(currentLabel,
CGRectMake(_centerPoint.x + 3,
_centerPoint.y - _r * step / _steps - 3,
20,
10),
self.scaleFont);
}
}
}
@end