1
votes

I am following the tutorial here about core plot here.... http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application

I am getting errors with the following lines of code

//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'
axisSet.xAxis.majorIntervalLength = [NSDecimalNumber decimalNumberWithString:@"5"];

// request for member 'axisLabelOffset' in something not a structure or union
axisSet.xAxis.axisLabelOffset = 3.0f;

//request for member 'bounds' in something not a structure or union
CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];

Here is my code now I am not getting any compiler errors anymore but its crashing and not loading the view please take a look if you can

  @implementation FirstCorePlotViewController

 (void)viewDidLoad 
{

      [super viewDidLoad];

      graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];

      CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
      hostingView.hostedLayer = graph;
      graph.paddingLeft = 20.0;
      graph.paddingTop = 20.0;
      graph.paddingRight = 20.0;
      graph.paddingBottom = 20.0;

        CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
     plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-6)
                                              length:CPDecimalFromFloat(12)];
     plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-5)
                                               length:CPDecimalFromFloat(30)];

      CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;

          CPLineStyle *lineStyle = [CPLineStyle lineStyle];
      lineStyle.lineColor = [CPColor blackColor];
          lineStyle.lineWidth = 2.0f;

      axisSet.xAxis.majorIntervalLength =CPDecimalFromString(@"5");
      axisSet.xAxis.minorTicksPerInterval = 4;
      axisSet.xAxis.majorTickLineStyle = lineStyle;
      axisSet.xAxis.minorTickLineStyle = lineStyle;
      axisSet.xAxis.axisLineStyle = lineStyle;
      axisSet.xAxis.minorTickLength = 5.0f;
      axisSet.xAxis.majorTickLength = 7.0f;
      axisSet.xAxis.labelOffset = 3.0f;

      axisSet.yAxis.majorIntervalLength = CPDecimalFromString(@"5");
      axisSet.yAxis.minorTicksPerInterval = 4;
      axisSet.yAxis.majorTickLineStyle = lineStyle;
      axisSet.yAxis.minorTickLineStyle = lineStyle;
      axisSet.yAxis.axisLineStyle = lineStyle;
      axisSet.yAxis.minorTickLength = 5.0f;
      axisSet.yAxis.majorTickLength = 7.0f;
      axisSet.yAxis.labelOffset = 3.0f;

     CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];


        xSquaredPlot.identifier = @"X Squared Plot";
    xSquaredPlot.dataLineStyle.lineWidth = 1.0f;
    xSquaredPlot.dataLineStyle.lineColor = [CPColor redColor];
    xSquaredPlot.dataSource = self;
    [graph addPlot:xSquaredPlot];

    CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
        greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
    //xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol;  

    CPScatterPlot *xInversePlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];    
    xInversePlot.identifier = @"X Inverse Plot";
    xInversePlot.dataLineStyle.lineWidth = 1.0f;
    xInversePlot.dataLineStyle.lineColor = [CPColor blueColor];
    xInversePlot.dataSource = self;
    [graph addPlot:xInversePlot];
}

-(NSUInteger)numberOfRecords 
 {

        return 51;
 }

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum  
           recordIndex:(NSUInteger)index
 {
        double val = (index/5.0)-5;
    if(fieldEnum == CPScatterPlotFieldX)
    { 

             return [NSNumber numberWithDouble:val]; 
     }
    else
    {
       if(plot.identifier == @"X Squared Plot")
       {
                   return [NSNumber numberWithDouble:val*val]; 
        }
       else
       {
                     return [NSNumber numberWithDouble:1/val];
       }
 }
}

 @end
4

4 Answers

1
votes

None of these errors are caused by #import problems. That tutorial is known to be somewhat out of date and some parts of the Core Plot framework have changed.

  • The majorIntervalLength property expects an NSDecimal, not NSDecimalNumer. Core Plot includes several utility functions that convert other types to NSDecimal such as CPDecimalFromString and CPDecimalFromDouble.

axisSet.xAxis.majorIntervalLength = CPDecimalFromString(@"5");

  • The axisLabelOffset property has been renamed to labelOffset.

  • The third error is caused by two things. Both UIView and CPLayer (the root class for all Core Plot layers) having an -initWithFrame: method. Because -alloc returns an id, the compiler doesn't know which -initWithFrame: to use and sometimes guesses wrong. You can fix it with a typecast. Also, plot spaces are not layers; just get the bounds of the graph.

CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
0
votes

// request for member 'axisLabelOffset' in something not a structure or union

... means that the complier doesn't recognize the name provided in the dot syntax as belonging to the object. Typos are a common cause of this error. Another is not properly importing the header for class preceding the dot.

//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'

This means that the property majorIntervalLength does not take a NSDecimalNumber.

I'm going to say that all your problems are caused by problems with #import statements. You not importing headers where you should be and the complier doesn't understand what symbol goes with which class.

0
votes

There is a divide by zero error in your -numberForPlot:field:recordIndex: method. When index == 25, the statement 1/val will blow up.

0
votes

Follow this Tute

This Ray's tutorial is really helpful.