-
Notifications
You must be signed in to change notification settings - Fork 610
/
DatePlot.m
277 lines (207 loc) · 8.68 KB
/
DatePlot.m
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
//
// DatePlot.m
// Plot Gallery-Mac
//
#import "DatePlot.h"
typedef NSDictionary<NSNumber *, NSNumber *> CPTPlotData;
typedef NSArray<CPTPlotData *> CPTPlotDataArray;
typedef NSMutableArray<CPTPlotData *> CPTMutablePlotDataArray;
static const NSUInteger kNumPoints = 10;
static const NSTimeInterval oneDay = 24 * 60 * 60;
@interface DatePlot()
@property (nonatomic, readwrite, strong, nonnull) CPTPlotDataArray *plotData;
@property (nonatomic, readwrite, strong, nullable) CPTPlotSpaceAnnotation *markerAnnotation;
@end
#pragma mark -
@implementation DatePlot
@synthesize plotData;
@synthesize markerAnnotation;
+(void)load
{
[super registerPlotItem:self];
}
-(nonnull instancetype)init
{
if ((self = [super init])) {
self.title = @"Date Plot";
self.section = kLinePlots;
}
return self;
}
-(void)generateData
{
if ( self.plotData.count == 0 ) {
// Add some data
CPTMutablePlotDataArray *newData = [NSMutableArray array];
for ( NSUInteger i = 0; i < kNumPoints; i++ ) {
NSTimeInterval xVal = oneDay * i;
double yVal = 1.2 * arc4random() / (double)UINT32_MAX + 1.2;
[newData addObject:
@{ @(CPTScatterPlotFieldX): @(xVal),
@(CPTScatterPlotFieldY): @(yVal) }
];
self.plotData = newData;
}
}
}
-(void)renderInGraphHostingView:(nonnull CPTGraphHostingView *)hostingView withTheme:(nullable CPTTheme *)theme animated:(BOOL __unused)animated
{
// If you make sure your dates are calculated at noon, you shouldn't have to
// worry about daylight savings. If you use midnight, you will have to adjust
// for daylight savings time.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.month = 10;
dateComponents.day = 29;
dateComponents.year = 2009;
dateComponents.hour = 12;
dateComponents.minute = 0;
dateComponents.second = 0;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *refDate = [gregorian dateFromComponents:dateComponents];
#if TARGET_OS_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:kCPTDarkGradientTheme]];
graph.plotAreaFrame.paddingLeft = 36.0;
graph.plotAreaFrame.paddingTop = 12.0;
graph.plotAreaFrame.paddingRight = 12.0;
graph.plotAreaFrame.paddingBottom = 12.0;
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@(xLow) length:@(oneDay * (kNumPoints - 1))];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@1.0 length:@2.0];
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = @(oneDay);
x.orthogonalPosition = @2.0;
x.minorTicksPerInterval = 0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
x.labelRotation = CPTFloat(M_PI_4);
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = @0.5;
y.minorTicksPerInterval = 5;
y.orthogonalPosition = @(oneDay);
CPTMutableLineStyle *blueLineStyle = [CPTMutableLineStyle lineStyle];
blueLineStyle.lineColor = [CPTColor blueColor];
blueLineStyle.lineWidth = 2.0;
CPTXYAxis *iAxis = [[CPTXYAxis alloc] initWithFrame:CGRectZero];
iAxis.title = nil;
iAxis.labelFormatter = nil;
iAxis.axisLineStyle = blueLineStyle;
iAxis.coordinate = CPTCoordinateY;
iAxis.plotSpace = graph.defaultPlotSpace;
iAxis.majorTickLineStyle = nil;
iAxis.minorTickLineStyle = nil;
iAxis.orthogonalPosition = @0.0;
iAxis.hidden = YES;
graph.axisSet.axes = @[x, y, iAxis];
// Create a plot that uses the data source method
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.identifier = @"Date Plot";
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 3.0;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor blackColor];
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";
hitAnnotationTextStyle.fontSize = self.titleSize * CPTFloat(0.5);
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:@"Annotation" style:hitAnnotationTextStyle];
textLayer.borderLineStyle = blueLineStyle;
textLayer.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
textLayer.cornerRadius = 3.0;
textLayer.paddingLeft = 2.0;
textLayer.paddingTop = 2.0;
textLayer.paddingRight = 2.0;
textLayer.paddingBottom = 2.0;
textLayer.hidden = YES;
CPTPlotSpaceAnnotation *annotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plotSpace anchorPlotPoint:@[@0, @0]];
annotation.contentLayer = textLayer;
[graph addAnnotation:annotation];
self.markerAnnotation = annotation;
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *__unused)plot
{
return self.plotData.count;
}
-(nullable id)numberForPlot:(nonnull CPTPlot *__unused)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
return self.plotData[index][@(fieldEnum)];
}
#pragma mark -
#pragma mark Plot Space Delegate Methods
-(CGPoint)plotSpace:(CPTPlotSpace *__unused)space willDisplaceBy:(CGPoint __unused)displacement
{
return CPTPointMake(0.0, 0.0);
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *__unused)newRange forCoordinate:(CPTCoordinate)coordinate
{
CPTPlotRange *updatedRange = nil;
CPTXYPlotSpace *xySpace = (CPTXYPlotSpace *)space;
switch ( coordinate ) {
case CPTCoordinateX:
updatedRange = xySpace.xRange;
break;
case CPTCoordinateY:
updatedRange = xySpace.yRange;
break;
default:
break;
}
return updatedRange;
}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(CPTNativeEvent *)event atPoint:(CGPoint __unused)point
{
CPTXYPlotSpace *xySpace = (CPTXYPlotSpace *)space;
CPTGraph *graph = space.graph;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTAxisArray *axes = axisSet.axes;
CPTXYAxis *iAxis = axes.lastObject;
CPTNumberArray *plotPoint = [space plotPointForEvent:event];
CPTPlotSpaceAnnotation *annotation = self.markerAnnotation;
CPTTextLayer *textLayer = (CPTTextLayer *)annotation.contentLayer;
NSNumber *xNumber = plotPoint[CPTCoordinateX];
if ( [xySpace.xRange containsNumber:xNumber] ) {
NSUInteger x = (NSUInteger)lround(xNumber.doubleValue / oneDay);
xNumber = @(x * oneDay);
NSString *dateValue = [axisSet.xAxis.labelFormatter stringForObjectValue:xNumber];
NSNumber *plotValue = self.plotData[x][@(CPTCoordinateY)];
textLayer.text = [NSString stringWithFormat:@"%@ → %@", dateValue, [NSString stringWithFormat:@"%1.3f", plotValue.doubleValue]];
textLayer.hidden = NO;
annotation.anchorPlotPoint = @[xNumber, xySpace.yRange.maxLimit];
iAxis.orthogonalPosition = xNumber;
iAxis.hidden = NO;
}
else {
textLayer.hidden = YES;
iAxis.hidden = YES;
}
return NO;
}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(CPTNativeEvent *)event atPoint:(CGPoint)point
{
return [self plotSpace:space shouldHandlePointingDeviceDownEvent:event atPoint:point];
}
-(BOOL)plotSpace:(CPTPlotSpace *__unused)space shouldHandlePointingDeviceUpEvent:(CPTNativeEvent *__unused)event atPoint:(CGPoint __unused)point
{
return NO;
}
@end