Advertisement
Guest User

Core plot semi-log graph

a guest
Aug 8th, 2011
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
  2.     return 50;
  3. }
  4.  
  5. -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
  6.     double val = index/10.0;
  7.     //NSLog(@"index = %d, val = %f, val^2 = %f", index, val, val*val);
  8.     if (fieldEnum == CPTScatterPlotFieldX) {
  9.         return [NSNumber numberWithDouble:val];
  10.     } else {
  11.         if (plot.identifier == @"X Squared Plot") {
  12.             return [NSNumber numberWithDouble:exp(val)];
  13.         } else {
  14.             return [NSNumber numberWithDouble:val*val];
  15.         }
  16.     }
  17.    
  18. }
  19. #pragma mark - View lifecycle
  20.  
  21. -(void)setupGraphWithXRangeLocation:(float)xLocation XLength:(float)xLength YRangeLocation:(float)yLocation YLength:(float)yLength {
  22.  
  23.     graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
  24.    
  25.     CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
  26.     hostingView.hostedGraph = graph;
  27.     graph.paddingLeft = 20.0;
  28.     graph.paddingTop = 20.0;
  29.     graph.paddingRight = 20.0;
  30.     graph.paddingBottom = 20.0;
  31.    
  32.     CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
  33.     plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLocation) length:CPTDecimalFromFloat(xLength)];
  34.     plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yLocation) length:CPTDecimalFromFloat(yLength)];
  35.  
  36.     //plotSpace.yScaleType = CPTScaleTypeLinear;
  37.    
  38.     CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
  39.     lineStyle.lineColor = [CPTColor blackColor];
  40.     lineStyle.lineWidth = 2.0f;
  41.    
  42.     CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
  43.    
  44.     CPTXYAxis *xAxis = axisSet.xAxis;
  45.     xAxis.majorIntervalLength = CPTDecimalFromInt(5);
  46.     xAxis.minorTicksPerInterval = 4;
  47.     xAxis.majorTickLineStyle = lineStyle;
  48.     xAxis.minorTickLineStyle = lineStyle;
  49.     xAxis.axisLineStyle = lineStyle;
  50.     xAxis.minorTickLength = 5.0f;
  51.     xAxis.majorTickLength = 7.0f;
  52.     xAxis.axisTitle.offset = 3.0f;
  53.    
  54.     CPTXYAxis *yAxis = axisSet.yAxis;
  55.     yAxis.majorIntervalLength = CPTDecimalFromInt(5);
  56.     yAxis.minorTicksPerInterval = 4;
  57.     yAxis.majorTickLineStyle = lineStyle;
  58.     yAxis.minorTickLineStyle = lineStyle;
  59.     yAxis.axisLineStyle = lineStyle;
  60.     yAxis.minorTickLength = 5.0f;
  61.     yAxis.majorTickLength = 7.0f;
  62.     yAxis.axisTitle.offset = 3.0f; 
  63.    
  64.     CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease];
  65.     xSquaredPlot.identifier = @"X Squared Plot";
  66.    
  67.     CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
  68.     dataLineStyle.lineColor = [CPTColor redColor];
  69.     dataLineStyle.lineWidth = 1.0f;
  70.    
  71.     xSquaredPlot.dataLineStyle = dataLineStyle;
  72.     xSquaredPlot.dataSource =  self;
  73.     [graph addPlot:xSquaredPlot];
  74.     CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease];
  75.     xInversePlot.identifier = @"X Inverse Plot";
  76.    
  77.     CPTMutableLineStyle *dataLineStyle2 = [CPTMutableLineStyle lineStyle];
  78.     dataLineStyle2.lineColor = [CPTColor blueColor];
  79.     dataLineStyle2.lineWidth = 1.0f;
  80.    
  81.     xInversePlot.dataLineStyle = dataLineStyle2;
  82.     xInversePlot.dataSource = self;
  83.     [graph addPlot:xInversePlot];
  84.    
  85.     plotSpace.xScaleType = CPTScaleTypeLinear;
  86.     plotSpace.yScaleType = CPTScaleTypeLog;
  87. }
  88.  
  89. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  90. - (void)viewDidLoad
  91. {
  92.     [super viewDidLoad];   
  93.    
  94.     [self setupGraphWithXRangeLocation:-1
  95.                                XLength:6
  96.                         YRangeLocation:-1
  97.                                YLength:25];
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement