forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.m
More file actions
102 lines (79 loc) · 3.44 KB
/
Controller.m
File metadata and controls
102 lines (79 loc) · 3.44 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
#import "Controller.h"
#import <CorePlot/CorePlot.h>
@interface Controller()
@property (nonatomic, readwrite, strong) IBOutlet CPTGraphHostingView *hostView;
@property (nonatomic, readwrite, strong) CPTXYGraph *graph;
@property (nonatomic, readwrite, strong) NSArray *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:CPTDecimalFromDouble(xLow) length:CPTDecimalFromDouble(oneDay * 5.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.0) length:CPTDecimalFromDouble(3.0)];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromDouble(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(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;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromDouble(0.5);
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(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.0;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[newGraph addPlot:dataSourceLinePlot];
// Add some data
NSMutableArray *newData = [NSMutableArray array];
for ( NSUInteger i = 0; i < 5; i++ ) {
NSTimeInterval xVal = oneDay * i;
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:(CPTPlot *)plot
{
return self.plotData.count;
}
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
return self.plotData[index][@(fieldEnum)];
}
@end