forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTFunctionDataSource.m
More file actions
437 lines (334 loc) · 13.8 KB
/
CPTFunctionDataSource.m
File metadata and controls
437 lines (334 loc) · 13.8 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#import "CPTFunctionDataSource.h"
#import "CPTExceptions.h"
#import "CPTMutablePlotRange.h"
#import "CPTNumericData.h"
#import "CPTScatterPlot.h"
#import "CPTUtilities.h"
#import "CPTXYPlotSpace.h"
#import "tgmath.h"
/// @cond
static void *CPTFunctionDataSourceKVOContext = (void *)&CPTFunctionDataSourceKVOContext;
@interface CPTFunctionDataSource()
@property (nonatomic, readwrite) CPTPlot *dataPlot;
@property (nonatomic, readwrite) double cachedStep;
@property (nonatomic, readwrite) NSUInteger dataCount;
@property (nonatomic, readwrite) NSUInteger cachedCount;
@property (nonatomic, readwrite, strong) CPTMutablePlotRange *cachedPlotRange;
-(instancetype)initForPlot:(CPTPlot *)plot NS_DESIGNATED_INITIALIZER;
-(void)plotBoundsChanged;
-(void)plotSpaceChanged;
@end
/// @endcond
#pragma mark -
/**
* @brief A datasource class that automatically creates scatter plot data from a function or Objective-C block.
**/
@implementation CPTFunctionDataSource
/** @property CPTDataSourceFunction dataSourceFunction
* @brief The function used to generate plot data.
**/
@synthesize dataSourceFunction;
/** @property CPTDataSourceBlock dataSourceBlock
* @brief The Objective-C block used to generate plot data.
**/
@synthesize dataSourceBlock;
/** @property 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.
**/
+(instancetype)dataSourceForPlot:(CPTPlot *)plot withFunction:(CPTDataSourceFunction)function
{
return [[self alloc] initForPlot:plot withFunction:function];
}
/** @brief Creates and returns a new CPTFunctionDataSource instance initialized with the provided block and plot.
* @param plot The plot that will display the function values.
* @param block The Objective-C block used to generate plot data.
* @return A new CPTFunctionDataSource instance initialized with the provided block and plot.
**/
+(instancetype)dataSourceForPlot:(CPTPlot *)plot withBlock:(CPTDataSourceBlock)block
{
return [[self alloc] initForPlot:plot withBlock:block];
}
/** @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.
**/
-(instancetype)initForPlot:(CPTPlot *)plot withFunction:(CPTDataSourceFunction)function
{
NSParameterAssert(function);
if ( (self = [self initForPlot:plot]) ) {
dataSourceFunction = function;
plot.dataSource = self;
}
return self;
}
/** @brief Initializes a newly allocated CPTFunctionDataSource object with the provided block and plot.
* @param plot The plot that will display the function values.
* @param block The Objective-C block used to generate plot data.
* @return The initialized CPTFunctionDataSource object.
**/
-(instancetype)initForPlot:(CPTPlot *)plot withBlock:(CPTDataSourceBlock)block
{
NSParameterAssert(block);
if ( (self = [self initForPlot:plot]) ) {
dataSourceBlock = block;
plot.dataSource = self;
}
return self;
}
/// @cond
-(instancetype)initForPlot:(CPTPlot *)plot
{
NSParameterAssert([plot isKindOfClass:[CPTScatterPlot class]]);
if ( (self = [super init]) ) {
dataPlot = plot;
dataSourceFunction = NULL;
dataSourceBlock = nil;
resolution = CPTFloat(1.0);
cachedStep = 0.0;
dataCount = 0;
cachedCount = 0;
cachedPlotRange = nil;
dataRange = nil;
plot.cachePrecision = CPTPlotCachePrecisionDouble;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(plotBoundsChanged)
name:CPTLayerBoundsDidChangeNotification
object:plot];
[plot addObserver:self
forKeyPath:@"plotSpace"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionInitial
context:CPTFunctionDataSourceKVOContext];
}
return self;
}
// function and plot are required; this will fail the assertions in -initForPlot:withFunction:
-(instancetype)init
{
[NSException raise:CPTException format:@"%@ must be initialized with a function or a block.", NSStringFromClass([self class])];
return [self initForPlot:[CPTScatterPlot layer] withFunction:sin];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[dataPlot removeObserver:self forKeyPath:@"plotSpace" context:CPTFunctionDataSourceKVOContext];
}
/// @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 = newRange;
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;
if ( plot ) {
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)plot.plotSpace;
if ( plotSpace ) {
CGFloat width = plot.bounds.size.width;
if ( width > CPTFloat(0.0) ) {
NSUInteger count = (NSUInteger)lrint( ceil(width / self.resolution) ) + 1;
if ( count > self.cachedCount ) {
self.dataCount = count;
self.cachedCount = count;
self.cachedStep = plotSpace.xRange.lengthDouble / count;
[plot reloadData];
}
}
else {
self.dataCount = 0;
self.cachedCount = 0;
self.cachedStep = 0.0;
}
}
}
}
/** @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)lrint( ( ceil( (cachedRange.minLimitDouble - minLimit) / step ) ) );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.locationDecimal = CPTDecimalSubtract(cachedRange.locationDecimal, offset);
cachedRange.lengthDecimal = CPTDecimalAdd(cachedRange.lengthDecimal, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:0 numberOfRecords:numPoints];
}
double maxLimit = plotRange.maxLimitDouble;
if ( ![cachedRange containsDouble:maxLimit] ) {
NSUInteger numPoints = (NSUInteger)lrint( ceil( (maxLimit - cachedRange.maxLimitDouble) / step ) );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.lengthDecimal = CPTDecimalAdd(cachedRange.lengthDecimal, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:plot.cachedDataCount numberOfRecords:numPoints];
}
}
else {
double maxLimit = plotRange.maxLimitDouble;
if ( ![cachedRange containsDouble:maxLimit] ) {
NSUInteger numPoints = (NSUInteger)lrint( ceil( (cachedRange.maxLimitDouble - maxLimit) / step ) );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.locationDecimal = CPTDecimalSubtract(cachedRange.locationDecimal, offset);
cachedRange.lengthDecimal = CPTDecimalAdd(cachedRange.lengthDecimal, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:0 numberOfRecords:numPoints];
}
double minLimit = plotRange.minLimitDouble;
if ( ![cachedRange containsDouble:minLimit] ) {
NSUInteger numPoints = (NSUInteger)lrint( ceil( (minLimit - cachedRange.minLimitDouble) / step ) );
NSDecimal offset = CPTDecimalFromDouble(step * numPoints);
cachedRange.lengthDecimal = CPTDecimalAdd(cachedRange.lengthDecimal, offset);
self.dataCount += numPoints;
[plot insertDataAtIndex:plot.cachedDataCount numberOfRecords:numPoints];
}
}
}
}
/// @endcond
#pragma mark -
#pragma mark KVO Methods
/// @cond
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, CPTPlotSpace *> *)change context:(void *)context
{
if ( (context == CPTFunctionDataSourceKVOContext) && [keyPath isEqualToString:@"plotSpace"] && [object isEqual:self.dataPlot] ) {
CPTPlotSpace *oldSpace = change[NSKeyValueChangeOldKey];
CPTPlotSpace *newSpace = change[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;
if ( count > 0 ) {
CPTPlotRange *xRange = self.cachedPlotRange;
if ( !xRange ) {
[self plotSpaceChanged];
xRange = self.cachedPlotRange;
}
NSMutableData *data = [[NSMutableData alloc] initWithLength:indexRange.length * 2 * sizeof(double)];
double *xBytes = data.mutableBytes;
double *yBytes = data.mutableBytes + ( indexRange.length * sizeof(double) );
double location = xRange.locationDouble;
double length = xRange.lengthDouble;
double denom = (double)( count - ( (count > 1) ? 1 : 0 ) );
NSUInteger lastIndex = NSMaxRange(indexRange);
CPTDataSourceFunction function = self.dataSourceFunction;
if ( function ) {
for ( NSUInteger i = indexRange.location; i < lastIndex; i++ ) {
double x = location + ( (double)i / denom ) * length;
*xBytes++ = x;
*yBytes++ = function(x);
}
}
else {
CPTDataSourceBlock functionBlock = self.dataSourceBlock;
if ( functionBlock ) {
for ( NSUInteger i = indexRange.location; i < lastIndex; i++ ) {
double x = location + ( (double)i / denom ) * length;
*xBytes++ = x;
*yBytes++ = functionBlock(x);
}
}
}
numericData = [CPTNumericData numericDataWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(double), CFByteOrderGetCurrent() )
shape:@[@(indexRange.length), @2]
dataOrder:CPTDataOrderColumnsFirst];
}
return numericData;
}
/// @endcond
@end