This repository was archived by the owner on Jul 31, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCPTFunctionDataSource.m
More file actions
373 lines (278 loc) · 11.6 KB
/
Copy pathCPTFunctionDataSource.m
File metadata and controls
373 lines (278 loc) · 11.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#import "CPTFunctionDataSource.h"
#import "CPTMutablePlotRange.h"
#import "CPTNumericData.h"
#import "CPTScatterPlot.h"
#import "CPTUtilities.h"
#import "CPTXYPlotSpace.h"
#import "tgmath.h"
/// @cond
static void *const CPTFunctionDataSourceKVOContext = (void *)&CPTFunctionDataSourceKVOContext;
@interface CPTFunctionDataSource()
@property (nonatomic, readwrite) double cachedStep;
@property (nonatomic, readwrite) NSUInteger dataCount;
@property (nonatomic, readwrite) NSUInteger cachedCount;
@property (nonatomic, readwrite, retain) CPTMutablePlotRange *cachedPlotRange;
-(void)plotBoundsChanged;
-(void)plotSpaceChanged;
@end
/// @endcond
#pragma mark -
/**
* @brief A datasource class that automatically creates scatter plot data from a function.
**/
@implementation CPTFunctionDataSource
/** @property CPTDataSourceFunction dataSourceFunction
* @brief The function used to generate plot data.
**/
@synthesize dataSourceFunction;
/** @property __cpt_weak CPTPlot *dataPlot
* @brief The plot that will display the function values. Must be an instance of CPTScatterPlot.
**/
@synthesize dataPlot;
/** @property CGFloat resolution
* @brief The maximum number of pixels between data points on the plot. Default is @num{1.0}.
**/
@synthesize resolution;
/** @property CPTPlotRange *dataRange
* @brief The maximum range of x-values that will be plotted. If @nil (the default), the function will be plotted for all visible x-values.
**/
@synthesize dataRange;
@synthesize cachedStep;
@synthesize cachedCount;
@synthesize dataCount;
@synthesize cachedPlotRange;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Creates and returns a new CPTFunctionDataSource instance initialized with the provided function and plot.
* @param plot The plot that will display the function values.
* @param function The function used to generate plot data.
* @return A new CPTFunctionDataSource instance initialized with the provided function and plot.
**/
+(id)dataSourceForPlot:(CPTPlot *)plot withFunction:(CPTDataSourceFunction)function
{
return [[[self alloc] initForPlot:plot withFunction:function] autorelease];
}
/** @brief Initializes a newly allocated CPTFunctionDataSource object with the provided function and plot.
* @param plot The plot that will display the function values.
* @param function The function used to generate plot data.
* @return The initialized CPTFunctionDataSource object.
**/
-(id)initForPlot:(CPTPlot *)plot withFunction:(CPTDataSourceFunction)function
{
NSParameterAssert([plot isKindOfClass:[CPTScatterPlot class]]);
NSParameterAssert(function);
if ( (self = [super init]) ) {
dataPlot = plot;
dataSourceFunction = function;
resolution = CPTFloat(1.0);
cachedStep = 0.0;
dataCount = 0;
cachedCount = 0;
cachedPlotRange = nil;
dataRange = nil;
dataPlot.cachePrecision = CPTPlotCachePrecisionDouble;
dataPlot.dataSource = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(plotBoundsChanged)
name:CPTLayerBoundsDidChangeNotification
object:dataPlot];
[dataPlot addObserver:self
forKeyPath:@"plotSpace"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionInitial
context:CPTFunctionDataSourceKVOContext];
}
return self;
}
/// @cond
// function and plot are required; this will fail the assertions in -initForPlot:withFunction:
-(id)init
{
return [self initForPlot:nil withFunction:NULL];
}
-(void)dealloc
{
[cachedPlotRange release];
[dataRange release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
#if MAC_OS_X_VERSION_10_6 < MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_4_0 < __IPHONE_OS_VERSION_MAX_ALLOWED
[dataPlot removeObserver:self forKeyPath:@"plotSpace" context:CPTFunctionDataSourceKVOContext];
#else
[dataPlot removeObserver:self forKeyPath:@"plotSpace"];
#endif
[super dealloc];
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setResolution:(CGFloat)newResolution
{
NSParameterAssert( newResolution > CPTFloat(0.0) );
if ( newResolution != resolution ) {
resolution = newResolution;
self.cachedCount = 0;
self.cachedPlotRange = nil;
[self plotBoundsChanged];
}
}
-(void)setDataRange:(CPTPlotRange *)newRange
{
if ( newRange != dataRange ) {
[dataRange release];
dataRange = [newRange retain];
if ( ![dataRange containsRange:self.cachedPlotRange] ) {
self.cachedCount = 0;
self.cachedPlotRange = nil;
[self plotBoundsChanged];
}
}
}
/// @endcond
#pragma mark -
#pragma mark Notifications
/// @cond
/** @internal
* @brief Reloads the plot with more closely spaced data points when needed.
**/
-(void)plotBoundsChanged
{
CPTPlot *plot = self.dataPlot;
NSUInteger count = (NSUInteger)ceil(plot.bounds.size.width / self.resolution) + 1;
if ( count > self.cachedCount ) {
self.dataCount = count;
self.cachedCount = count;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)plot.plotSpace;
self.cachedStep = plotSpace.xRange.lengthDouble / count;
[plot reloadData];
}
}
/** @internal
* @brief Adds new data points as needed while scrolling.
**/
-(void)plotSpaceChanged
{
CPTPlot *plot = self.dataPlot;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)plot.plotSpace;
CPTMutablePlotRange *plotRange = [plotSpace.xRange mutableCopy];
[plotRange intersectionPlotRange:self.dataRange];
CPTMutablePlotRange *cachedRange = self.cachedPlotRange;
double step = self.cachedStep;
if ( [cachedRange containsRange:plotRange] ) {
// no new data needed
}
else if ( ![cachedRange intersectsRange:plotRange] || (step == 0.0) ) {
self.cachedCount = 0;
self.cachedPlotRange = plotRange;
[self plotBoundsChanged];
}
else {
if ( step > 0.0 ) {
double minLimit = plotRange.minLimitDouble;
if ( ![cachedRange containsDouble:minLimit] ) {
NSUInteger numPoints = (NSUInteger)ceil( (cachedRange.minLimitDouble - minLimit) / step );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.location = CPTDecimalSubtract(cachedRange.location, offset);
cachedRange.length = CPTDecimalAdd(cachedRange.length, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:0 numberOfRecords:numPoints];
}
double maxLimit = plotRange.maxLimitDouble;
if ( ![cachedRange containsDouble:maxLimit] ) {
NSUInteger numPoints = (NSUInteger)ceil( (maxLimit - cachedRange.maxLimitDouble) / step );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.length = CPTDecimalAdd(cachedRange.length, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:plot.cachedDataCount numberOfRecords:numPoints];
}
}
else {
double maxLimit = plotRange.maxLimitDouble;
if ( ![cachedRange containsDouble:maxLimit] ) {
NSUInteger numPoints = (NSUInteger)ceil( (cachedRange.maxLimitDouble - maxLimit) / step );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.location = CPTDecimalSubtract(cachedRange.location, offset);
cachedRange.length = CPTDecimalAdd(cachedRange.length, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:0 numberOfRecords:numPoints];
}
double minLimit = plotRange.minLimitDouble;
if ( ![cachedRange containsDouble:minLimit] ) {
NSUInteger numPoints = (NSUInteger)ceil( (minLimit - cachedRange.minLimitDouble) / step );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.length = CPTDecimalAdd(cachedRange.length, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:plot.cachedDataCount numberOfRecords:numPoints];
}
}
}
[plotRange release];
}
/// @endcond
#pragma mark -
#pragma mark KVO Methods
/// @cond
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ( (context == CPTFunctionDataSourceKVOContext) && [keyPath isEqualToString:@"plotSpace"] && [object isEqual:self.dataPlot] ) {
CPTPlotSpace *oldSpace = [change objectForKey:NSKeyValueChangeOldKey];
CPTPlotSpace *newSpace = [change objectForKey:NSKeyValueChangeNewKey];
if ( oldSpace ) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:CPTPlotSpaceCoordinateMappingDidChangeNotification
object:oldSpace];
}
if ( newSpace ) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(plotSpaceChanged)
name:CPTPlotSpaceCoordinateMappingDidChangeNotification
object:newSpace];
}
self.cachedPlotRange = nil;
[self plotSpaceChanged];
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
/// @endcond
#pragma mark -
#pragma mark CPTScatterPlotDataSource Methods
/// @cond
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
NSUInteger count = 0;
if ( [plot isEqual:self.dataPlot] ) {
count = self.dataCount;
}
return count;
}
-(CPTNumericData *)dataForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange
{
CPTNumericData *numericData = nil;
NSUInteger count = self.dataCount;
CPTPlotRange *xRange = self.cachedPlotRange;
if ( xRange && (count > 0) ) {
NSMutableData *data = [[NSMutableData alloc] initWithLength:indexRange.length * 2 * sizeof(double)];
double *xBytes = data.mutableBytes;
double *yBytes = data.mutableBytes + ( indexRange.length * sizeof(double) );
CPTDataSourceFunction function = self.dataSourceFunction;
double location = xRange.locationDouble;
double length = xRange.lengthDouble;
double denom = (double)( count - ( (count > 1) ? 1 : 0 ) );
NSUInteger lastIndex = NSMaxRange(indexRange);
for ( NSUInteger i = indexRange.location; i < lastIndex; i++ ) {
double x = location + ( (double)i / denom ) * length;
*xBytes++ = x;
*yBytes++ = function(x);
}
NSArray *shape = [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInteger:indexRange.length], [NSNumber numberWithUnsignedInteger:2], nil];
numericData = [CPTNumericData numericDataWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(double), CFByteOrderGetCurrent() )
shape:shape
dataOrder:CPTDataOrderColumnsFirst];
[data release];
}
return numericData;
}
/// @endcond
@end