-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTViewController.m
More file actions
295 lines (231 loc) · 11 KB
/
Copy pathHTViewController.m
File metadata and controls
295 lines (231 loc) · 11 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// HTViewController.m
// CorePlotProgram
//
// Created by wb-shangguanhaitao on 14-2-19.
// Copyright (c) 2014年 shangguan. All rights reserved.
//
#import "HTViewController.h"
#import "CorePlot-CocoaTouch.h"
#import <runetype.h>
#define GREEN_PLOT_IDENTIFIER @"Green Plot"
#define BLUE_PLOT_IDENTIFIER @"Blue Plot"
@interface HTViewController ()<CPTPlotDataSource>
@property (nonatomic, strong) CPTXYGraph *graph;
@property (nonatomic, strong) NSMutableArray *dataForPlot;
/**
* 设置图表绘制的配置
*/
- (void)setupCoreplotViews;
/**
* 简便获取绘制的范围
*
* @param location 绘制的起始位置
* @param length 绘制的长度
*
* @return 绘制范围配置的实例
*/
- (CPTPlotRange *)CPTPlotRangeFromFloat:(float)location length:(float)length;
@end
@implementation HTViewController
#pragma mark - Superclass API
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCoreplotViews];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Private API
- (void)setupCoreplotViews
{
// Create graph from theme: 设置主题
//
_graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme * theme = [CPTTheme themeNamed:kCPTSlateTheme];
[_graph applyTheme:theme];
CPTGraphHostingView * hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling
hostingView.hostedGraph = _graph;
_graph.paddingLeft = _graph.paddingRight = 10.0;
_graph.paddingTop = _graph.paddingBottom = 10.0;
// Setup plot space: 设置一屏内可显示的x,y量度范围
//
CPTXYPlotSpace * plotSpace = (CPTXYPlotSpace *)_graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(3.0)];
// Axes: 设置x,y轴属性,如原点,量度间隔,标签,刻度,颜色等
//
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)_graph.axisSet;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 2.0f;
lineStyle.lineColor = [CPTColor whiteColor];
CPTXYAxis * x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1.0"); // 原点的 x 位置
x.majorIntervalLength = CPTDecimalFromString(@"0.5"); // x轴主刻度:显示数字标签的量度间隔
x.minorTicksPerInterval = 2; // x轴细分刻度:每一个主刻度范围内显示细分刻度的个数
x.minorTickLineStyle = lineStyle;
// 需要排除的不显示数字的主刻度
NSArray * exclusionRanges = [NSArray arrayWithObjects:
[self CPTPlotRangeFromFloat:0.99 length:0.02],
[self CPTPlotRangeFromFloat:2.99 length:0.02],
nil];
x.labelExclusionRanges = exclusionRanges;
CPTXYAxis * y = axisSet.yAxis;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2.0"); // 原点的 y 位置
y.majorIntervalLength = CPTDecimalFromString(@"0.5"); // y轴主刻度:显示数字标签的量度间隔
y.minorTicksPerInterval = 4; // y轴细分刻度:每一个主刻度范围内显示细分刻度的个数
y.minorTickLineStyle = lineStyle;
exclusionRanges = [NSArray arrayWithObjects:
[self CPTPlotRangeFromFloat:1.99 length:0.02],
[self CPTPlotRangeFromFloat:2.99 length:0.02],
nil];
y.labelExclusionRanges = exclusionRanges;
// y.delegate = self;
// Add some initial data
//
// Create a red-blue plot area
//
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor blueColor];
CPTScatterPlot * boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier = BLUE_PLOT_IDENTIFIER;
boundLinePlot.dataSource = self;
// boundLinePlot.showLabels = NO;
// Do a red-blue gradient: 渐变色区域
//
CPTColor * blueColor = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPTColor * redColor = [CPTColor colorWithComponentRed:1.0 green:0.3 blue:0.3 alpha:0.8];
CPTGradient * areaGradient1 = [CPTGradient gradientWithBeginningColor:blueColor
endingColor:redColor];
areaGradient1.angle = -90.0f;
CPTFill * areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber numberWithFloat:1.0] decimalValue]; // 渐变色的起点位置
boundLinePlot.areaFill2 = [CPTFill fillWithColor:[CPTColor orangeColor]];
boundLinePlot.areaBaseValue2 = [[NSDecimalNumber numberWithFloat:1.5] decimalValue]; // 渐变色的起点位置
// Add plot symbols: 表示数值的符号的形状
//
CPTMutableLineStyle * symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
symbolLineStyle.lineWidth = 2.0;
CPTPlotSymbol * plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
boundLinePlot.plotSymbol = plotSymbol;
_dataForPlot = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 100; i++ ) {
id x = [NSNumber numberWithFloat:0 + i * 0.05];
id y = [NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
[_dataForPlot addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
[_graph addPlot:boundLinePlot];
// Create a green plot area: 画破折线
//
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 3.f;
lineStyle.lineColor = [CPTColor greenColor];
lineStyle.dashPattern = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:5.0f],
[NSNumber numberWithFloat:5.0f], nil];
CPTScatterPlot * dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.identifier = GREEN_PLOT_IDENTIFIER;
dataSourceLinePlot.dataSource = self;
// Put an area gradient under the plot above
//
CPTColor * areaColor = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];
CPTGradient * areaGradient = [CPTGradient gradientWithBeginningColor:areaColor
endingColor:[CPTColor clearColor]];
areaGradient.angle = -90.0f;
areaGradientFill = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue= CPTDecimalFromString(@"1.75");
// Animate in the new plot: 淡入动画
dataSourceLinePlot.opacity = 0.0f;
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 3.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];
[_graph addPlot:dataSourceLinePlot];
}
-(CPTPlotRange *)CPTPlotRangeFromFloat:(float)location length:(float)length
{
return [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(location) length:CPTDecimalFromFloat(length)];
}
#pragma mark - Plot Data Source API
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return [_dataForPlot count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSString * key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
NSNumber * num = [[_dataForPlot objectAtIndex:index] valueForKey:key];
// Green plot gets shifted above the blue
if ([(NSString *)plot.identifier isEqualToString:GREEN_PLOT_IDENTIFIER]) {
if (fieldEnum == CPTScatterPlotFieldY) {
num = [NSNumber numberWithDouble:[num doubleValue] + 1.0];
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", (unsigned long)index]];
CPTMutableTextStyle *textStyle = [label.textStyle mutableCopy];
textStyle.color = [CPTColor lightGrayColor];
label.textStyle = textStyle;
return label;
}
#pragma mark - Axis Delegate API
-(BOOL)axis:(CPTAxis *)axis shouldUpdateAxisLabelsAtLocations:(NSSet *)locations
{
static CPTTextStyle * positiveStyle = nil;
static CPTTextStyle * negativeStyle = nil;
NSNumberFormatter * formatter = axis.labelFormatter;
CGFloat labelOffset = axis.labelOffset;
NSDecimalNumber * zero = [NSDecimalNumber zero];
NSMutableSet * newLabels = [NSMutableSet set];
for (NSDecimalNumber * tickLocation in locations) {
CPTTextStyle *theLabelTextStyle;
if ([tickLocation isGreaterThanOrEqualTo:zero]) {
if (!positiveStyle) {
CPTMutableTextStyle * newStyle = [axis.labelTextStyle mutableCopy];
newStyle.color = [CPTColor greenColor];
positiveStyle = newStyle;
}
theLabelTextStyle = positiveStyle;
}
else {
if (!negativeStyle) {
CPTMutableTextStyle * newStyle = [axis.labelTextStyle mutableCopy];
newStyle.color = [CPTColor redColor];
negativeStyle = newStyle;
}
theLabelTextStyle = negativeStyle;
}
NSString * labelString = [formatter stringForObjectValue:tickLocation];
CPTTextLayer * newLabelLayer= [[CPTTextLayer alloc] initWithText:labelString style:theLabelTextStyle];
CPTAxisLabel * newLabel = [[CPTAxisLabel alloc] initWithContentLayer:newLabelLayer];
newLabel.tickLocation = tickLocation.decimalValue;
newLabel.offset = labelOffset;
[newLabels addObject:newLabel];
}
axis.axisLabels = newLabels;
return NO;
}
@end