forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTDataSourceTestCase.m
More file actions
136 lines (104 loc) · 3.56 KB
/
CPTDataSourceTestCase.m
File metadata and controls
136 lines (104 loc) · 3.56 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
#import "CPTDataSourceTestCase.h"
#import "CPTExceptions.h"
#import "CPTMutablePlotRange.h"
#import "CPTScatterPlot.h"
#import "CPTUtilities.h"
static const CGFloat CPTDataSourceTestCasePlotOffset = 0.5;
/// @cond
@interface CPTDataSourceTestCase()
-(CPTMutablePlotRange *)plotRangeForData:(CPTNumberArray)dataArray;
@end
/// @endcond
@implementation CPTDataSourceTestCase
@synthesize xData;
@synthesize yData;
@synthesize nRecords;
@dynamic xRange;
@dynamic yRange;
@synthesize plots;
-(void)setUp
{
//check CPTDataSource conformance
XCTAssertTrue([self conformsToProtocol:@protocol(CPTPlotDataSource)], @"CPTDataSourceTestCase should conform to <CPTPlotDataSource>");
}
-(void)tearDown
{
self.xData = nil;
self.yData = nil;
[[self plots] removeAllObjects];
}
-(void)buildData
{
NSUInteger recordCount = self.nRecords;
CPTMutableNumberArray arr = [NSMutableArray arrayWithCapacity:recordCount];
for ( NSUInteger i = 0; i < recordCount; i++ ) {
[arr insertObject:@(i) atIndex:i];
}
self.xData = arr;
arr = [NSMutableArray arrayWithCapacity:recordCount];
for ( NSUInteger i = 0; i < recordCount; i++ ) {
[arr insertObject:@( sin(2 * M_PI * (double)i / (double)recordCount) ) atIndex:i];
}
self.yData = arr;
}
-(void)addPlot:(CPTPlot *)newPlot
{
if ( nil == self.plots ) {
self.plots = [NSMutableArray array];
}
[[self plots] addObject:newPlot];
}
-(CPTPlotRange *)xRange
{
[self buildData];
return [self plotRangeForData:self.xData];
}
-(CPTPlotRange *)yRange
{
[self buildData];
CPTMutablePlotRange *range = [self plotRangeForData:self.yData];
if ( self.plots.count > 1 ) {
range.lengthDecimal = CPTDecimalAdd( range.lengthDecimal, CPTDecimalFromUnsignedInteger(self.plots.count) );
}
return range;
}
-(CPTMutablePlotRange *)plotRangeForData:(CPTNumberArray)dataArray
{
double min = [[dataArray valueForKeyPath:@"@min.doubleValue"] doubleValue];
double max = [[dataArray valueForKeyPath:@"@max.doubleValue"] doubleValue];
double range = max - min;
return [CPTMutablePlotRange plotRangeWithLocation:@(min - 0.05 * range)
length:@(range + 0.1 * range)];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return self.nRecords;
}
-(CPTNumberArray)numbersForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndexRange:(NSRange)indexRange
{
CPTNumberArray result;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
result = [[self xData] objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:indexRange]];
break;
case CPTScatterPlotFieldY:
result = [[self yData] objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:indexRange]];
if ( self.plots.count > 1 ) {
XCTAssertTrue([[self plots] containsObject:plot], @"Plot missing");
CPTMutableNumberArray shiftedResult = [NSMutableArray arrayWithCapacity:result.count];
for ( NSDecimalNumber *d in result ) {
[shiftedResult addObject:[d decimalNumberByAdding:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromDouble( CPTDataSourceTestCasePlotOffset * ([[self plots] indexOfObject:plot] + 1) )]]];
}
result = shiftedResult;
}
break;
default:
[NSException raise:CPTDataException format:@"Unexpected fieldEnum"];
}
return result;
}
@end