forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlChart.m
More file actions
304 lines (242 loc) · 9.6 KB
/
Copy pathControlChart.m
File metadata and controls
304 lines (242 loc) · 9.6 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
296
297
298
299
300
301
302
303
304
#import "ControlChart.h"
static NSString *const kDataLine = @"Data Line";
static NSString *const kCenterLine = @"Center Line";
static NSString *const kControlLine = @"Control Line";
static NSString *const kWarningLine = @"Warning Line";
static const NSUInteger numberOfPoints = 11;
@interface ControlChart()
@property (nonatomic, readwrite, strong) CPTNumberArray plotData;
@property (nonatomic, readwrite, assign) double meanValue;
@property (nonatomic, readwrite, assign) double standardError;
@end
@implementation ControlChart
@synthesize plotData;
@synthesize meanValue;
@synthesize standardError;
+(void)load
{
[super registerPlotItem:self];
}
-(instancetype)init
{
if ( (self = [super init]) ) {
self.title = @"Control Chart";
self.section = kLinePlots;
}
return self;
}
-(void)generateData
{
if ( self.plotData == nil ) {
CPTMutableNumberArray contentArray = [NSMutableArray array];
double sum = 0.0;
for ( NSUInteger i = 0; i < numberOfPoints; i++ ) {
double y = 12.0 * arc4random() / (double)UINT32_MAX + 5.0;
sum += y;
[contentArray addObject:@(y)];
}
self.plotData = contentArray;
self.meanValue = sum / numberOfPoints;
sum = 0.0;
for ( NSNumber *value in contentArray ) {
double error = [value doubleValue] - self.meanValue;
sum += error * error;
}
double stdDev = sqrt( ( 1.0 / (numberOfPoints - 1) ) * sum );
self.standardError = stdDev / sqrt(numberOfPoints);
}
}
-(void)renderInGraphHostingView:(CPTGraphHostingView *)hostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGRect bounds = hostingView.bounds;
#else
CGRect bounds = NSRectToCGRect(hostingView.bounds);
#endif
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[self addGraph:graph toHostingView:hostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
graph.plotAreaFrame.paddingTop = self.titleSize * CPTFloat(0.5);
graph.plotAreaFrame.paddingRight = self.titleSize * CPTFloat(0.5);
graph.plotAreaFrame.paddingBottom = self.titleSize * CPTFloat(1.5);
graph.plotAreaFrame.paddingLeft = self.titleSize * CPTFloat(1.5);
graph.plotAreaFrame.masksToBorder = NO;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:CPTFloat(0.2)] colorWithAlphaComponent:CPTFloat(0.75)];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:CPTFloat(0.1)];
CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle];
redLineStyle.lineWidth = 10.0;
redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5];
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.maximumFractionDigits = 0;
// Axes
// X axis
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.labelFormatter = labelFormatter;
x.title = @"X Axis";
x.titleOffset = self.titleSize * CPTFloat(1.25);
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.labelFormatter = labelFormatter;
y.title = @"Y Axis";
y.titleOffset = self.titleSize * CPTFloat(1.25);
// Center line
CPTScatterPlot *centerLinePlot = [[CPTScatterPlot alloc] init];
centerLinePlot.identifier = kCenterLine;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor greenColor];
centerLinePlot.dataLineStyle = lineStyle;
centerLinePlot.dataSource = self;
[graph addPlot:centerLinePlot];
// Control lines
CPTScatterPlot *controlLinePlot = [[CPTScatterPlot alloc] init];
controlLinePlot.identifier = kControlLine;
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor redColor];
lineStyle.dashPattern = @[@10, @6];
controlLinePlot.dataLineStyle = lineStyle;
controlLinePlot.dataSource = self;
[graph addPlot:controlLinePlot];
// Warning lines
CPTScatterPlot *warningLinePlot = [[CPTScatterPlot alloc] init];
warningLinePlot.identifier = kWarningLine;
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor orangeColor];
lineStyle.dashPattern = @[@5, @5];
warningLinePlot.dataLineStyle = lineStyle;
warningLinePlot.dataSource = self;
[graph addPlot:warningLinePlot];
// Data line
CPTScatterPlot *linePlot = [[CPTScatterPlot alloc] init];
linePlot.identifier = kDataLine;
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 3.0;
linePlot.dataLineStyle = lineStyle;
linePlot.dataSource = self;
[graph addPlot:linePlot];
// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
linePlot.plotSymbol = plotSymbol;
// Auto scale the plot space to fit the plot data
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
[plotSpace scaleToFitPlots:@[linePlot]];
// Adjust visible ranges so plot symbols along the edges are not clipped
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
x.orthogonalPosition = yRange.location;
y.orthogonalPosition = xRange.location;
x.visibleRange = xRange;
y.visibleRange = yRange;
x.gridLinesRange = yRange;
y.gridLinesRange = xRange;
[xRange expandRangeByFactor:@1.05];
[yRange expandRangeByFactor:@1.05];
plotSpace.xRange = xRange;
plotSpace.yRange = yRange;
// Add legend
graph.legend = [CPTLegend legendWithPlots:@[linePlot, controlLinePlot, warningLinePlot, centerLinePlot]];
graph.legend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
graph.legend.textStyle = x.titleTextStyle;
graph.legend.borderLineStyle = x.axisLineStyle;
graph.legend.cornerRadius = 5.0;
graph.legend.numberOfRows = 1;
graph.legendAnchor = CPTRectAnchorBottom;
graph.legendDisplacement = CGPointMake( 0.0, self.titleSize * CPTFloat(4.0) );
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( plot.identifier == kDataLine ) {
return self.plotData.count;
}
else if ( plot.identifier == kCenterLine ) {
return 2;
}
else {
return 5;
}
}
-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
double number = NAN;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
if ( plot.identifier == kDataLine ) {
number = (double)index;
}
else {
switch ( index % 3 ) {
case 0:
number = 0.0;
break;
case 1:
number = (double)(self.plotData.count - 1);
break;
case 2:
number = NAN;
break;
}
}
break;
case CPTScatterPlotFieldY:
if ( plot.identifier == kDataLine ) {
number = [self.plotData[index] doubleValue];
}
else if ( plot.identifier == kCenterLine ) {
number = self.meanValue;
}
else if ( plot.identifier == kControlLine ) {
switch ( index ) {
case 0:
case 1:
number = self.meanValue + 3.0 * self.standardError;
break;
case 2:
number = NAN;
break;
case 3:
case 4:
number = self.meanValue - 3.0 * self.standardError;
break;
}
}
else if ( plot.identifier == kWarningLine ) {
switch ( index ) {
case 0:
case 1:
number = self.meanValue + 2.0 * self.standardError;
break;
case 2:
number = NAN;
break;
case 3:
case 4:
number = self.meanValue - 2.0 * self.standardError;
break;
}
}
break;
}
return number;
}
@end