Advertisement
Guest User

Untitled

a guest
Oct 25th, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. .h:
  2. //
  3. // CPTTestAppBarChartController.h
  4. // CPTTestApp-iPhone
  5. //
  6.  
  7. #import <UIKit/UIKit.h>
  8. #import "CorePlot-CocoaTouch.h"
  9.  
  10. @interface CPTTestAppBarChartController : UIViewController <CPTPlotDataSource>
  11. {
  12. @private
  13. CPTXYGraph *barChart;
  14. NSTimer *timer;
  15. }
  16.  
  17. @property(readwrite, retain, nonatomic) NSTimer *timer;
  18.  
  19. -(void)timerFired;
  20.  
  21. @end
  22.  
  23.  
  24. .m:
  25.  
  26. //
  27. // CPTTestAppBarChartController.m
  28. // CPTTestApp-iPhone
  29. //
  30.  
  31. #import "CPTTestAppBarChartController.h"
  32.  
  33. @implementation CPTTestAppBarChartController
  34.  
  35. @synthesize timer;
  36.  
  37. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  38. {
  39. return YES;
  40. }
  41.  
  42. #pragma mark -
  43. #pragma mark Initialization and teardown
  44. -(void) viewDidLoad{
  45. NSLog(@"Hello World!");
  46. [super viewDidLoad];
  47. }
  48. -(void)viewDidAppear:(BOOL)animated
  49. {
  50. [self timerFired];
  51. #ifdef MEMORY_TEST
  52. self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
  53. selector:@selector(timerFired) userInfo:nil repeats:YES];
  54. #endif
  55. }
  56.  
  57. -(void)timerFired
  58. {
  59. #ifdef MEMORY_TEST
  60. static NSUInteger counter = 0;
  61.  
  62. NSLog(@"\n----------------------------\ntimerFired: %lu", counter++);
  63. #endif
  64.  
  65. [barChart release];
  66.  
  67. // Create barChart from theme
  68. barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
  69. CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
  70. [barChart applyTheme:theme];
  71. CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
  72. hostingView.hostedGraph = barChart;
  73.  
  74. // Border
  75. barChart.plotAreaFrame.borderLineStyle = nil;
  76. barChart.plotAreaFrame.cornerRadius = 0.0f;
  77.  
  78. // Paddings
  79. barChart.paddingLeft = 0.0f;
  80. barChart.paddingRight = 0.0f;
  81. barChart.paddingTop = 0.0f;
  82. barChart.paddingBottom = 0.0f;
  83.  
  84. barChart.plotAreaFrame.paddingLeft = 70.0;
  85. barChart.plotAreaFrame.paddingTop = 20.0;
  86. barChart.plotAreaFrame.paddingRight = 20.0;
  87. barChart.plotAreaFrame.paddingBottom = 80.0;
  88.  
  89. // Graph title
  90. barChart.title = @"Graph Title\nLine 2";
  91. CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
  92. textStyle.color = [CPTColor grayColor];
  93. textStyle.fontSize = 16.0f;
  94. textStyle.textAlignment = CPTTextAlignmentCenter;
  95. barChart.titleTextStyle = textStyle;
  96. barChart.titleDisplacement = CGPointMake(0.0f, -20.0f);
  97. barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
  98.  
  99. // Add plot space for horizontal bar charts
  100. CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
  101. plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(300.0f)];
  102. plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(16.0f)];
  103.  
  104.  
  105. CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
  106. CPTXYAxis *x = axisSet.xAxis;
  107. x.axisLineStyle = nil;
  108. x.majorTickLineStyle = nil;
  109. x.minorTickLineStyle = nil;
  110. x.majorIntervalLength = CPTDecimalFromString(@"5");
  111. x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
  112. x.title = @"X Axis";
  113. x.titleLocation = CPTDecimalFromFloat(7.5f);
  114. x.titleOffset = 55.0f;
  115.  
  116. // Define some custom labels for the data elements
  117. x.labelRotation = M_PI/4;
  118. x.labelingPolicy = CPTAxisLabelingPolicyNone;
  119. NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
  120.  
  121. NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
  122. NSUInteger labelLocation = 0;
  123. NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
  124. for (NSNumber *tickLocation in customTickLocations) {
  125. CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
  126. newLabel.tickLocation = [tickLocation decimalValue];
  127. newLabel.offset = x.labelOffset + x.majorTickLength;
  128. newLabel.rotation = M_PI/4;
  129. [customLabels addObject:newLabel];
  130. [newLabel release];
  131. }
  132.  
  133. x.axisLabels = [NSSet setWithArray:customLabels];
  134.  
  135. CPTXYAxis *y = axisSet.yAxis;
  136. y.axisLineStyle = nil;
  137. y.majorTickLineStyle = nil;
  138. y.minorTickLineStyle = nil;
  139. y.majorIntervalLength = CPTDecimalFromString(@"50");
  140. y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
  141. y.title = @"Y Axis";
  142. y.titleOffset = 45.0f;
  143. y.titleLocation = CPTDecimalFromFloat(150.0f);
  144.  
  145. // First bar plot
  146. CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
  147. barPlot.baseValue = CPTDecimalFromString(@"0");
  148. barPlot.dataSource = self;
  149. barPlot.barOffset = CPTDecimalFromFloat(-0.25f);
  150. barPlot.identifier = @"Bar Plot 1";
  151. [barChart addPlot:barPlot toPlotSpace:plotSpace];
  152.  
  153. // Second bar plot
  154. barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
  155. barPlot.dataSource = self;
  156. barPlot.baseValue = CPTDecimalFromString(@"0");
  157. barPlot.barOffset = CPTDecimalFromFloat(0.25f);
  158. barPlot.barCornerRadius = 2.0f;
  159. barPlot.identifier = @"Bar Plot 2";
  160. [barChart addPlot:barPlot toPlotSpace:plotSpace];
  161. }
  162.  
  163. - (void)didReceiveMemoryWarning {
  164. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  165. // Release anything that's not essential, such as cached data
  166. }
  167.  
  168. #pragma mark -
  169. #pragma mark Plot Data Source Methods
  170.  
  171. -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
  172. return 16;
  173. }
  174.  
  175. -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
  176. {
  177. NSDecimalNumber *num = nil;
  178. if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
  179. switch ( fieldEnum ) {
  180. case CPTBarPlotFieldBarLocation:
  181. num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
  182. break;
  183. case CPTBarPlotFieldBarTip:
  184. num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index+1)*(index+1)];
  185. if ( [plot.identifier isEqual:@"Bar Plot 2"] )
  186. num = [num decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"10"]];
  187. break;
  188. }
  189. }
  190.  
  191. return num;
  192. }
  193.  
  194. -(CPTFill *) barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSNumber *)index;
  195. {
  196. return nil;
  197. }
  198.  
  199. @end
  200.  
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement