-
Notifications
You must be signed in to change notification settings - Fork 596
Expand file tree
/
Copy pathController.m
More file actions
111 lines (85 loc) · 3.73 KB
/
Copy pathController.m
File metadata and controls
111 lines (85 loc) · 3.73 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
#import "Controller.h"
#import <CorePlot/CorePlot.h>
@interface Controller()
@property (nonatomic, readwrite, strong, nullable) IBOutlet CPTGraphHostingView *hostView;
@property (nonatomic, readwrite, strong, nonnull) CPTXYGraph *graph;
@property (nonatomic, readwrite, strong, nonnull) NSArray<NSDictionary *> *plotData;
@end
#pragma mark -
@implementation Controller
@synthesize hostView;
@synthesize graph;
@synthesize plotData;
-(void)awakeFromNib
{
[super awakeFromNib];
// 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.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *refDate = [formatter dateFromString:@"12:00 Oct 29, 2009"];
NSTimeInterval oneDay = 24 * 60 * 60;
// Create graph from theme
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[newGraph applyTheme:theme];
self.graph = newGraph;
self.hostView.hostedGraph = newGraph;
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph.defaultPlotSpace;
NSTimeInterval xLow = 0.0;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@(xLow) length:@(oneDay * 3.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@1.0 length:@3.0];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = @(oneDay);
x.orthogonalPosition = @2.0;
x.minorTicksPerInterval = 3;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *myDateFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
myDateFormatter.referenceDate = refDate;
x.labelFormatter = myDateFormatter;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.timeStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *myTimeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:timeFormatter];
myTimeFormatter.referenceDate = refDate;
x.minorTickLabelFormatter = myTimeFormatter;
// x.minorTickLabelRotation = M_PI_2;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = @0.5;
y.minorTicksPerInterval = 5;
y.orthogonalPosition = @(0.5 * oneDay);
// 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.;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[newGraph addPlot:dataSourceLinePlot];
// Add some data
NSMutableArray<NSDictionary *> *newData = [NSMutableArray array];
for ( NSUInteger i = 0; i < 7; i++ ) {
NSTimeInterval xVal = oneDay * i * 0.5;
double yVal = 1.2 * arc4random() / (double)UINT32_MAX + 1.2;
[newData addObject:
@{ @(CPTScatterPlotFieldX): @(xVal),
@(CPTScatterPlotFieldY): @(yVal) }
];
}
self.plotData = newData;
}
#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)];
}
@end