11#import " Controller.h"
22#import < CorePlot/CorePlot.h>
33
4+ @interface Controller ()
5+
6+ @property (nonatomic , readwrite , strong ) IBOutlet CPTGraphHostingView *hostView;
7+ @property (nonatomic , readwrite , strong ) CPTXYGraph *graph;
8+ @property (nonatomic , readwrite , strong ) NSArray *plotData;
9+
10+ @end
11+
12+ #pragma mark -
13+
414@implementation Controller
515
16+ @synthesize hostView;
17+ @synthesize graph;
18+ @synthesize plotData;
19+
620-(void )awakeFromNib
721{
822 [super awakeFromNib ];
@@ -15,21 +29,23 @@ -(void)awakeFromNib
1529 NSTimeInterval oneDay = 24 * 60 * 60 ;
1630
1731 // Create graph from theme
18- graph = [[CPTXYGraph alloc ] initWithFrame: CGRectZero];
19- CPTTheme *theme = [CPTTheme themeNamed: kCPTDarkGradientTheme ];
20- [graph applyTheme: theme];
21- hostView.hostedGraph = graph;
32+ CPTXYGraph *newGraph = [[CPTXYGraph alloc ] initWithFrame: CGRectZero];
33+ CPTTheme *theme = [CPTTheme themeNamed: kCPTDarkGradientTheme ];
34+ [newGraph applyTheme: theme];
35+ self.graph = newGraph;
36+
37+ self.hostView .hostedGraph = newGraph;
2238
2339 // Setup scatter plot space
24- CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph .defaultPlotSpace ;
40+ CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph .defaultPlotSpace ;
2541 NSTimeInterval xLow = 0.0 ;
2642 plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble (xLow) length: CPTDecimalFromDouble (oneDay * 5.0 )];
2743 plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble (1.0 ) length: CPTDecimalFromDouble (3.0 )];
2844
2945 // Axes
30- CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph .axisSet ;
46+ CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph .axisSet ;
3147 CPTXYAxis *x = axisSet.xAxis ;
32- x.majorIntervalLength = CPTDecimalFromFloat (oneDay);
48+ x.majorIntervalLength = CPTDecimalFromDouble (oneDay);
3349 x.orthogonalCoordinateDecimal = CPTDecimalFromDouble (2.0 );
3450 x.minorTicksPerInterval = 0 ;
3551 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc ] init ];
@@ -41,7 +57,7 @@ -(void)awakeFromNib
4157 CPTXYAxis *y = axisSet.yAxis ;
4258 y.majorIntervalLength = CPTDecimalFromDouble (0.5 );
4359 y.minorTicksPerInterval = 5 ;
44- y.orthogonalCoordinateDecimal = CPTDecimalFromFloat (oneDay);
60+ y.orthogonalCoordinateDecimal = CPTDecimalFromDouble (oneDay);
4561
4662 // Create a plot that uses the data source method
4763 CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc ] init ];
@@ -53,32 +69,32 @@ -(void)awakeFromNib
5369 dataSourceLinePlot.dataLineStyle = lineStyle;
5470
5571 dataSourceLinePlot.dataSource = self;
56- [graph addPlot: dataSourceLinePlot];
72+ [newGraph addPlot: dataSourceLinePlot];
5773
5874 // Add some data
5975 NSMutableArray *newData = [NSMutableArray array ];
6076 for ( NSUInteger i = 0 ; i < 5 ; i++ ) {
61- NSTimeInterval x = oneDay * i;
62- NSNumber *y = @( 1.2 * rand () / (double )RAND_MAX + 1.2 ) ;
77+ NSTimeInterval xVal = oneDay * i;
78+ double yVal = 1.2 * rand () / (double )RAND_MAX + 1.2 ;
6379 [newData addObject:
64- @{ @(CPTScatterPlotFieldX): @(x ),
65- @(CPTScatterPlotFieldY): y }
80+ @{ @(CPTScatterPlotFieldX): @(xVal ),
81+ @(CPTScatterPlotFieldY): @(yVal) }
6682 ];
6783 }
68- plotData = newData;
84+ self. plotData = newData;
6985}
7086
7187#pragma mark -
7288#pragma mark Plot Data Source Methods
7389
7490-(NSUInteger )numberOfRecordsForPlot : (CPTPlot *)plot
7591{
76- return plotData.count ;
92+ return self. plotData .count ;
7793}
7894
7995-(id )numberForPlot : (CPTPlot *)plot field : (NSUInteger )fieldEnum recordIndex : (NSUInteger )index
8096{
81- return plotData[index][@(fieldEnum)];
97+ return self. plotData [index][@(fieldEnum)];
8298}
8399
84100@end
0 commit comments