66
77@interface MainViewController ()
88
9- @property (nonatomic , strong ) CPTXYGraph *graph;
10- @property (nonatomic , strong ) APYahooDataPuller *datapuller;
9+ @property (nonatomic , readwrite , strong ) CPTXYGraph *graph;
10+ @property (nonatomic , readwrite , strong ) APYahooDataPuller *datapuller;
1111
1212@end
1313
@@ -28,22 +28,25 @@ -(void)setView:(UIView *)aView
2828
2929-(void )viewDidLoad
3030{
31- graph = [[CPTXYGraph alloc ] initWithFrame: CGRectZero];
32- CPTTheme *theme = [CPTTheme themeNamed: kCPTStocksTheme ];
33- [graph applyTheme: theme];
34- graph.frame = self.view .bounds ;
35- graph.paddingRight = 50.0 ;
36- graph.paddingLeft = 50.0 ;
37- graph.plotAreaFrame .masksToBorder = NO ;
38- graph.plotAreaFrame .cornerRadius = 0.0 ;
31+ CPTXYGraph *newGraph = [[CPTXYGraph alloc ] initWithFrame: self .view.bounds];
32+ CPTTheme *theme = [CPTTheme themeNamed: kCPTStocksTheme ];
33+
34+ [newGraph applyTheme: theme];
35+ self.graph = newGraph;
36+
37+ newGraph.paddingRight = 50.0 ;
38+ newGraph.paddingLeft = 50.0 ;
39+ newGraph.plotAreaFrame .masksToBorder = NO ;
40+ newGraph.plotAreaFrame .cornerRadius = 0.0 ;
41+
3942 CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle ];
40- borderLineStyle.lineColor = [CPTColor whiteColor ];
41- borderLineStyle.lineWidth = 2.0 ;
42- graph .plotAreaFrame .borderLineStyle = borderLineStyle;
43- self.graphHost .hostedGraph = graph ;
43+ borderLineStyle.lineColor = [CPTColor whiteColor ];
44+ borderLineStyle.lineWidth = 2.0 ;
45+ newGraph .plotAreaFrame .borderLineStyle = borderLineStyle;
46+ self.graphHost .hostedGraph = newGraph ;
4447
4548 // Axes
46- CPTXYAxisSet *xyAxisSet = (id )graph .axisSet ;
49+ CPTXYAxisSet *xyAxisSet = (CPTXYAxisSet *)newGraph .axisSet ;
4750 CPTXYAxis *xAxis = xyAxisSet.xAxis ;
4851 CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy ];
4952 lineStyle.lineCap = kCGLineCapButt ;
@@ -54,21 +57,21 @@ -(void)viewDidLoad
5457 yAxis.axisLineStyle = nil ;
5558
5659 // Line plot with gradient fill
57- CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc ] initWithFrame: graph .bounds];
60+ CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc ] initWithFrame: newGraph .bounds];
5861 dataSourceLinePlot.identifier = @" Data Source Plot" ;
5962 dataSourceLinePlot.dataLineStyle = nil ;
6063 dataSourceLinePlot.dataSource = self;
6164 dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
62- [graph addPlot: dataSourceLinePlot];
65+ [newGraph addPlot: dataSourceLinePlot];
6366
64- CPTColor *areaColor = [CPTColor colorWithComponentRed: 1.0 green: 1.0 blue: 1.0 alpha: 0.6 ];
67+ CPTColor *areaColor = [CPTColor colorWithComponentRed: CPTFloat ( 1.0 ) green: CPTFloat ( 1.0 ) blue: CPTFloat ( 1.0 ) alpha: CPTFloat ( 0.6 ) ];
6568 CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor: areaColor endingColor: [CPTColor clearColor ]];
6669 areaGradient.angle = -90.0 ;
6770 CPTFill *areaGradientFill = [CPTFill fillWithGradient: areaGradient];
6871 dataSourceLinePlot.areaFill = areaGradientFill;
6972 dataSourceLinePlot.areaBaseValue = CPTDecimalFromDouble (200.0 );
7073
71- areaColor = [CPTColor colorWithComponentRed: 0.0 green: 1.0 blue: 0.0 alpha: 0.6 ];
74+ areaColor = [CPTColor colorWithComponentRed: CPTFloat ( 0.0 ) green: CPTFloat ( 1.0 ) blue: CPTFloat ( 0.0 ) alpha: CPTFloat ( 0.6 ) ];
7275 areaGradient = [CPTGradient gradientWithBeginningColor: [CPTColor clearColor ] endingColor: areaColor];
7376 areaGradient.angle = -90.0 ;
7477 areaGradientFill = [CPTFill fillWithGradient: areaGradient];
@@ -79,7 +82,7 @@ -(void)viewDidLoad
7982 CPTMutableLineStyle *whiteLineStyle = [CPTMutableLineStyle lineStyle ];
8083 whiteLineStyle.lineColor = [CPTColor whiteColor ];
8184 whiteLineStyle.lineWidth = 1.0 ;
82- CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc ] initWithFrame: graph .bounds];
85+ CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc ] initWithFrame: newGraph .bounds];
8386 ohlcPlot.identifier = @" OHLC" ;
8487 ohlcPlot.lineStyle = whiteLineStyle;
8588 CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle ];
@@ -91,12 +94,12 @@ -(void)viewDidLoad
9194 ohlcPlot.dataSource = self;
9295 ohlcPlot.plotStyle = CPTTradingRangePlotStyleOHLC;
9396 ohlcPlot.cachePrecision = CPTPlotCachePrecisionDecimal;
94- [graph addPlot: ohlcPlot];
97+ [newGraph addPlot: ohlcPlot];
9598
9699 // Add plot space for bar chart
97100 CPTXYPlotSpace *volumePlotSpace = [[CPTXYPlotSpace alloc ] init ];
98101 volumePlotSpace.identifier = @" Volume Plot Space" ;
99- [graph addPlotSpace: volumePlotSpace];
102+ [newGraph addPlotSpace: volumePlotSpace];
100103
101104 // Volume plot
102105 CPTBarPlot *volumePlot = [CPTBarPlot tubularBarPlotWithColor: [CPTColor blackColor ] horizontalBars: NO ];
@@ -110,7 +113,7 @@ -(void)viewDidLoad
110113 volumePlot.barWidth = CPTDecimalFromFloat (1 .0f );
111114 volumePlot.identifier = @" Volume Plot" ;
112115 volumePlot.cachePrecision = CPTPlotCachePrecisionDouble;
113- [graph addPlot: volumePlot toPlotSpace: volumePlotSpace];
116+ [newGraph addPlot: volumePlot toPlotSpace: volumePlotSpace];
114117
115118 // Data puller
116119 NSDate *start = [NSDate dateWithTimeIntervalSinceNow: -60.0 * 60.0 * 24.0 * 7.0 * 12.0 ]; // 12 weeks ago
@@ -124,7 +127,7 @@ -(void)viewDidLoad
124127
125128-(id )initWithNibName : (NSString *)nibNameOrNil bundle : (NSBundle *)nibBundleOrNil
126129{
127- if ( self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil] ) {
130+ if ( ( self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) ) {
128131 }
129132 return self;
130133}
@@ -600,8 +603,10 @@ -(void)dataPullerDidFinishFetch:(APYahooDataPuller *)dp
600603 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph .defaultPlotSpace ;
601604 CPTXYPlotSpace *volumePlotSpace = (CPTXYPlotSpace *)[self .graph plotSpaceWithIdentifier: @" Volume Plot Space" ];
602605
603- NSDecimalNumber *high = [datapuller overallHigh ];
604- NSDecimalNumber *low = [datapuller overallLow ];
606+ APYahooDataPuller *thePuller = self.datapuller ;
607+
608+ NSDecimalNumber *high = thePuller.overallHigh ;
609+ NSDecimalNumber *low = thePuller.overallLow ;
605610 NSDecimalNumber *length = [high decimalNumberBySubtracting: low];
606611
607612 NSLog (@" high = %@ , low = %@ , length = %@ " , high, low, length);
@@ -613,18 +618,18 @@ -(void)dataPullerDidFinishFetch:(APYahooDataPuller *)dp
613618 NSDecimalNumber *lowDisplayLocation = [low decimalNumberBySubtracting: lengthDisplacementValue];
614619 NSDecimalNumber *lengthDisplayLocation = [length decimalNumberByAdding: lengthDisplacementValue];
615620
616- plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromFloat (0 .0f ) length: CPTDecimalFromUnsignedInteger (datapuller .financialData.count + 1 )];
617- plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: [ lowDisplayLocation decimalValue ] length: [ lengthDisplayLocation decimalValue ] ];
621+ plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble (0.0 ) length: CPTDecimalFromUnsignedInteger (thePuller .financialData.count + 1 )];
622+ plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: lowDisplayLocation. decimalValue length: lengthDisplayLocation. decimalValue];
618623
619- CPTScatterPlot *linePlot = (CPTScatterPlot *)[graph plotWithIdentifier: @" Data Source Plot" ];
624+ CPTScatterPlot *linePlot = (CPTScatterPlot *)[self . graph plotWithIdentifier: @" Data Source Plot" ];
620625 linePlot.areaBaseValue = [high decimalValue ];
621626 linePlot.areaBaseValue2 = [low decimalValue ];
622627
623628 // Axes
624- CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet ;
629+ CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self. graph .axisSet ;
625630
626- NSDecimalNumber *overallVolumeHigh = [datapuller overallVolumeHigh ] ;
627- NSDecimalNumber *overallVolumeLow = [datapuller overallVolumeLow ] ;
631+ NSDecimalNumber *overallVolumeHigh = thePuller. overallVolumeHigh ;
632+ NSDecimalNumber *overallVolumeLow = thePuller. overallVolumeLow ;
628633 NSDecimalNumber *volumeLength = [overallVolumeHigh decimalNumberBySubtracting: overallVolumeLow];
629634
630635 // make the length aka height for y 3 times more so that we get a 1/3 area covered by volume
@@ -636,7 +641,7 @@ -(void)dataPullerDidFinishFetch:(APYahooDataPuller *)dp
636641 NSDecimalNumber *volumeLowDisplayLocation = overallVolumeLow;
637642 NSDecimalNumber *volumeLengthDisplayLocation = [volumeLength decimalNumberByAdding: volumeLengthDisplacementValue];
638643
639- volumePlotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromFloat (0 .0f ) length: CPTDecimalFromUnsignedInteger (datapuller .financialData.count + 1 )];
644+ volumePlotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble (0.0 ) length: CPTDecimalFromUnsignedInteger (thePuller .financialData.count + 1 )];
640645// volumePlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:[volumeLowDisplayLocation decimalValue] length:[volumeLengthDisplayLocation decimalValue]];
641646
642647 if ( animationOperation ) {
@@ -659,19 +664,7 @@ -(void)dataPullerDidFinishFetch:(APYahooDataPuller *)dp
659664
660665 axisSet.yAxis .labelExclusionRanges = exclusionRanges;
661666
662- [graph reloadData ];
663- }
664-
665- -(APYahooDataPuller *)datapuller
666- {
667- return datapuller;
668- }
669-
670- -(void )setDatapuller : (APYahooDataPuller *)aDatapuller
671- {
672- if ( datapuller != aDatapuller ) {
673- datapuller = aDatapuller;
674- }
667+ [self .graph reloadData ];
675668}
676669
677670@end
0 commit comments