forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTPlotDocument.m
More file actions
469 lines (361 loc) · 17.7 KB
/
CPTPlotDocument.m
File metadata and controls
469 lines (361 loc) · 17.7 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#import "CPTPlotDocument.h"
#import "NSString+ParseCSV.h"
@interface CPTPlotDocument()
@property (nonatomic, readwrite, strong) IBOutlet CPTGraphHostingView *graphView;
@property (nonatomic, readwrite, strong) CPTXYGraph *graph;
@property (nonatomic, readwrite, assign) double minimumValueForXAxis;
@property (nonatomic, readwrite, assign) double maximumValueForXAxis;
@property (nonatomic, readwrite, assign) double minimumValueForYAxis;
@property (nonatomic, readwrite, assign) double maximumValueForYAxis;
@property (nonatomic, readwrite, assign) double majorIntervalLengthForX;
@property (nonatomic, readwrite, assign) double majorIntervalLengthForY;
@property (nonatomic, readwrite, strong) NSArray *dataPoints;
@property (nonatomic, readwrite, strong) CPTPlotSpaceAnnotation *zoomAnnotation;
@property (nonatomic, readwrite, assign) CGPoint dragStart;
@property (nonatomic, readwrite, assign) CGPoint dragEnd;
@end
#pragma mark -
@implementation CPTPlotDocument
@synthesize graphView;
@synthesize graph;
@synthesize minimumValueForXAxis;
@synthesize maximumValueForXAxis;
@synthesize minimumValueForYAxis;
@synthesize maximumValueForYAxis;
@synthesize majorIntervalLengthForX;
@synthesize majorIntervalLengthForY;
@synthesize dataPoints;
@synthesize zoomAnnotation;
@synthesize dragStart;
@synthesize dragEnd;
//#define USE_NSDECIMAL
-(id)init
{
self = [super init];
if ( self ) {
dataPoints = [[NSMutableArray alloc] init];
zoomAnnotation = nil;
dragStart = CGPointZero;
dragEnd = CGPointZero;
}
return self;
}
-(NSString *)windowNibName
{
return @"CPTPlotDocument";
}
-(void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
// Create graph from theme
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[newGraph applyTheme:theme];
self.graph = newGraph;
self.graphView.hostedGraph = newGraph;
newGraph.paddingLeft = 0.0;
newGraph.paddingTop = 0.0;
newGraph.paddingRight = 0.0;
newGraph.paddingBottom = 0.0;
newGraph.plotAreaFrame.paddingLeft = 55.0;
newGraph.plotAreaFrame.paddingTop = 40.0;
newGraph.plotAreaFrame.paddingRight = 40.0;
newGraph.plotAreaFrame.paddingBottom = 35.0;
newGraph.plotAreaFrame.plotArea.fill = newGraph.plotAreaFrame.fill;
newGraph.plotAreaFrame.fill = nil;
newGraph.plotAreaFrame.borderLineStyle = nil;
newGraph.plotAreaFrame.cornerRadius = 0.0;
newGraph.plotAreaFrame.masksToBorder = NO;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.minimumValueForXAxis)
length:CPTDecimalFromDouble(ceil( (self.maximumValueForXAxis - self.minimumValueForXAxis) / self.majorIntervalLengthForX ) * self.majorIntervalLengthForX)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.minimumValueForYAxis)
length:CPTDecimalFromDouble(ceil( (self.maximumValueForYAxis - self.minimumValueForYAxis) / self.majorIntervalLengthForY ) * self.majorIntervalLengthForY)];
// this allows the plot to respond to mouse events
[plotSpace setDelegate:self];
[plotSpace setAllowsUserInteraction:YES];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.minorTicksPerInterval = 9;
x.majorIntervalLength = CPTDecimalFromDouble(self.majorIntervalLengthForX);
x.labelOffset = 5.0;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
CPTXYAxis *y = axisSet.yAxis;
y.minorTicksPerInterval = 9;
y.majorIntervalLength = CPTDecimalFromDouble(self.majorIntervalLengthForY);
y.labelOffset = 5.0;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Create the main plot for the delimited data
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
dataSourceLinePlot.identifier = @"Data Source Plot";
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor whiteColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[newGraph addPlot:dataSourceLinePlot];
}
#pragma mark -
#pragma mark Data loading methods
-(NSData *)dataOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
-(BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
if ( [typeName isEqualToString:@"CSVDocument"] ) {
double minX = MAXFLOAT;
double maxX = -MAXFLOAT;
double minY = MAXFLOAT;
double maxY = -MAXFLOAT;
NSMutableArray *newData = [[NSMutableArray alloc] init];
NSString *fileContents = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// Parse CSV
NSUInteger length = fileContents.length;
NSUInteger lineStart = 0, lineEnd = 0, contentsEnd = 0;
NSRange currentRange;
// Read headers from the first line of the file
[fileContents getParagraphStart:&lineStart end:&lineEnd contentsEnd:&contentsEnd forRange:NSMakeRange(lineEnd, 0)];
// currentRange = NSMakeRange(lineStart, contentsEnd - lineStart);
// NSArray *columnHeaders = [[fileContents substringWithRange:currentRange] arrayByParsingCSVLine];
// NSLog([columnHeaders objectAtIndex:0]);
while ( lineEnd < length ) {
[fileContents getParagraphStart:&lineStart end:&lineEnd contentsEnd:&contentsEnd forRange:NSMakeRange(lineEnd, 0)];
currentRange = NSMakeRange(lineStart, contentsEnd - lineStart);
NSArray *columnValues = [[fileContents substringWithRange:currentRange] arrayByParsingCSVLine];
double xValue = [columnValues[0] doubleValue];
double yValue = [columnValues[1] doubleValue];
if ( xValue < minX ) {
minX = xValue;
}
if ( xValue > maxX ) {
maxX = xValue;
}
if ( yValue < minY ) {
minY = yValue;
}
if ( yValue > maxY ) {
maxY = yValue;
}
#ifdef USE_NSDECIMAL
[newData addObject:@{ @"x": [NSDecimalNumber decimalNumberWithString:columnValues[0]],
@"y": [NSDecimalNumber decimalNumberWithString:columnValues[1]] }
];
#else
[newData addObject:@{ @"x": @(xValue),
@"y": @(yValue) }
];
#endif
}
self.dataPoints = newData;
double intervalX = (maxX - minX) / 5.0;
if ( intervalX > 0.0 ) {
intervalX = pow( 10.0, ceil( log10(intervalX) ) );
}
self.majorIntervalLengthForX = intervalX;
double intervalY = (maxY - minY) / 10.0;
if ( intervalY > 0.0 ) {
intervalY = pow( 10.0, ceil( log10(intervalY) ) );
}
self.majorIntervalLengthForY = intervalY;
minX = floor(minX / intervalX) * intervalX;
minY = floor(minY / intervalY) * intervalY;
self.minimumValueForXAxis = minX;
self.maximumValueForXAxis = maxX;
self.minimumValueForYAxis = minY;
self.maximumValueForYAxis = maxY;
}
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
#pragma mark -
#pragma mark Zoom Methods
-(IBAction)zoomIn
{
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
CPTPlotArea *plotArea = self.graph.plotAreaFrame.plotArea;
// convert the dragStart and dragEnd values to plot coordinates
CGPoint dragStartInPlotArea = [self.graph convertPoint:self.dragStart toLayer:plotArea];
CGPoint dragEndInPlotArea = [self.graph convertPoint:self.dragEnd toLayer:plotArea];
double start[2], end[2];
// obtain the datapoints for the drag start and end
[plotSpace doublePrecisionPlotPoint:start numberOfCoordinates:2 forPlotAreaViewPoint:dragStartInPlotArea];
[plotSpace doublePrecisionPlotPoint:end numberOfCoordinates:2 forPlotAreaViewPoint:dragEndInPlotArea];
// recalculate the min and max values
self.minimumValueForXAxis = MIN(start[CPTCoordinateX], end[CPTCoordinateX]);
self.maximumValueForXAxis = MAX(start[CPTCoordinateX], end[CPTCoordinateX]);
self.minimumValueForYAxis = MIN(start[CPTCoordinateY], end[CPTCoordinateY]);
self.maximumValueForYAxis = MAX(start[CPTCoordinateY], end[CPTCoordinateY]);
// now adjust the plot range and axes
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.minimumValueForXAxis)
length:CPTDecimalFromDouble(self.maximumValueForXAxis - self.minimumValueForXAxis)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.minimumValueForYAxis)
length:CPTDecimalFromDouble(self.maximumValueForYAxis - self.minimumValueForYAxis)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
}
-(IBAction)zoomOut
{
double minX = MAXFLOAT;
double maxX = -MAXFLOAT;
double minY = MAXFLOAT;
double maxY = -MAXFLOAT;
// get the ful range min and max values
for ( NSDictionary *xyValues in self.dataPoints ) {
double xVal = [xyValues[@"x"] doubleValue];
minX = fmin(xVal, minX);
maxX = fmax(xVal, maxX);
double yVal = [xyValues[@"y"] doubleValue];
minY = fmin(yVal, minY);
maxY = fmax(yVal, maxY);
}
double intervalX = self.majorIntervalLengthForX;
double intervalY = self.majorIntervalLengthForY;
minX = floor(minX / intervalX) * intervalX;
minY = floor(minY / intervalY) * intervalY;
self.minimumValueForXAxis = minX;
self.maximumValueForXAxis = maxX;
self.minimumValueForYAxis = minY;
self.maximumValueForYAxis = maxY;
// now adjust the plot range and axes
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(minX)
length:CPTDecimalFromDouble(ceil( (maxX - minX) / intervalX ) * intervalX)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(minY)
length:CPTDecimalFromDouble(ceil( (maxY - minY) / intervalY ) * intervalY)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
}
#pragma mark -
#pragma mark PDF / image export
-(IBAction)exportToPDF:(id)sender
{
NSSavePanel *pdfSavingDialog = [NSSavePanel savePanel];
[pdfSavingDialog setAllowedFileTypes:@[@"pdf"]];
if ( [pdfSavingDialog runModal] == NSOKButton ) {
NSData *dataForPDF = [self.graph dataForPDFRepresentationOfLayer];
[dataForPDF writeToURL:[pdfSavingDialog URL] atomically:NO];
}
}
-(IBAction)exportToPNG:(id)sender
{
NSSavePanel *pngSavingDialog = [NSSavePanel savePanel];
[pngSavingDialog setAllowedFileTypes:@[@"png"]];
if ( [pngSavingDialog runModal] == NSOKButton ) {
NSImage *image = [self.graph imageOfLayer];
NSData *tiffData = [image TIFFRepresentation];
NSBitmapImageRep *tiffRep = [NSBitmapImageRep imageRepWithData:tiffData];
NSData *pngData = [tiffRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToURL:[pngSavingDialog URL] atomically:NO];
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return self.dataPoints.count;
}
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
return self.dataPoints[index][key];
}
#pragma mark -
#pragma mark Plot Space Delegate Methods
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)interactionPoint
{
CPTPlotSpaceAnnotation *annotation = self.zoomAnnotation;
if ( annotation ) {
CPTPlotArea *plotArea = self.graph.plotAreaFrame.plotArea;
CGRect plotBounds = plotArea.bounds;
// convert the dragStart and dragEnd values to plot coordinates
CGPoint dragStartInPlotArea = [self.graph convertPoint:self.dragStart toLayer:plotArea];
CGPoint dragEndInPlotArea = [self.graph convertPoint:interactionPoint toLayer:plotArea];
// create the dragrect from dragStart to the current location
CGFloat endX = MAX( MIN( dragEndInPlotArea.x, CGRectGetMaxX(plotBounds) ), CGRectGetMinX(plotBounds) );
CGFloat endY = MAX( MIN( dragEndInPlotArea.y, CGRectGetMaxY(plotBounds) ), CGRectGetMinY(plotBounds) );
CGRect borderRect = CGRectMake( dragStartInPlotArea.x, dragStartInPlotArea.y,
(endX - dragStartInPlotArea.x),
(endY - dragStartInPlotArea.y) );
annotation.contentAnchorPoint = CGPointMake(dragEndInPlotArea.x >= dragStartInPlotArea.x ? 0.0 : 1.0,
dragEndInPlotArea.y >= dragStartInPlotArea.y ? 0.0 : 1.0);
annotation.contentLayer.frame = borderRect;
}
return NO;
}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)interactionPoint
{
if ( !self.zoomAnnotation ) {
self.dragStart = interactionPoint;
CPTPlotArea *plotArea = self.graph.plotAreaFrame.plotArea;
CGPoint dragStartInPlotArea = [self.graph convertPoint:self.dragStart toLayer:plotArea];
if ( CGRectContainsPoint(plotArea.bounds, dragStartInPlotArea) ) {
// create the zoom rectangle
// first a bordered layer to draw the zoomrect
CPTBorderedLayer *zoomRectangleLayer = [[CPTBorderedLayer alloc] initWithFrame:CGRectNull];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor darkGrayColor];
lineStyle.lineWidth = 1.0;
zoomRectangleLayer.borderLineStyle = lineStyle;
CPTColor *transparentFillColor = [[CPTColor blueColor] colorWithAlphaComponent:0.2];
zoomRectangleLayer.fill = [CPTFill fillWithColor:transparentFillColor];
double start[2];
[self.graph.defaultPlotSpace doublePrecisionPlotPoint:start numberOfCoordinates:2 forPlotAreaViewPoint:dragStartInPlotArea];
NSArray *anchorPoint = @[@(start[CPTCoordinateX]),
@(start[CPTCoordinateY])];
// now create the annotation
CPTPlotSpaceAnnotation *annotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
annotation.contentLayer = zoomRectangleLayer;
self.zoomAnnotation = annotation;
[self.graph.plotAreaFrame.plotArea addAnnotation:annotation];
}
}
return NO;
}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceUpEvent:(id)event atPoint:(CGPoint)interactionPoint
{
CPTPlotSpaceAnnotation *annotation = self.zoomAnnotation;
if ( annotation ) {
self.dragEnd = interactionPoint;
// double-click to completely zoom out
if ( [event clickCount] == 2 ) {
CPTPlotArea *plotArea = self.graph.plotAreaFrame.plotArea;
CGPoint dragEndInPlotArea = [self.graph convertPoint:interactionPoint toLayer:plotArea];
if ( CGRectContainsPoint(plotArea.bounds, dragEndInPlotArea) ) {
[self zoomOut];
}
}
else if ( !CGPointEqualToPoint(self.dragStart, self.dragEnd) ) {
// no accidental drag, so zoom in
[self zoomIn];
}
// and we're done with the drag
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.zoomAnnotation = nil;
self.dragStart = CGPointZero;
self.dragEnd = CGPointZero;
}
return NO;
}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceCancelledEvent:(id)event atPoint:(CGPoint)interactionPoint
{
CPTPlotSpaceAnnotation *annotation = self.zoomAnnotation;
if ( annotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.zoomAnnotation = nil;
self.dragStart = CGPointZero;
self.dragEnd = CGPointZero;
}
return NO;
}
@end