views:

332

answers:

3

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

A: 

// 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.

TechZen
I imported this: in the .m file#import "FirstCorePlotViewController.h" which is the default. I know core plot it loaded correctly because if I take out all the exceptions the simulator shows me a blank graph. This is what I imported in my .h file #import <UIKit/UIKit.h>#import <CorePlot/CorePlot.h>Do I have to import more in the .m file?
Nick LaMarca
I dont have to add the protocal code somewhere do I?
Nick LaMarca
See Eric Skroch answer, he seems to to be familiar with the framework.
TechZen
A: 

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];
Eric Skroch
1. Can you give me a code snippet to convert this correctly...axisSet.xAxis.majorIntervalLength =[NSDecimalNumber decimalNumberWithString:@"5"];2. IS FIXED3. Your code still produces the same error.I just want to get a sample to run to I can see core plot in action and this is the only tutorial I can find. I im pretty sure I installed core-plot correctly and the CPLayerHostingView is working because if I take all the graphing code out I do see the x-y axis in the iphone simulator
Nick LaMarca
I edited my response to give an example for #1 and fix #3. Plot spaces are not CPLayers so they don't have bounds. You still probably need the typecast.
Eric Skroch
Eric thanks for your help on I am really interested in getting this to work and I am simply new to iphone development and suck right now. I see you contribute to core-plot on their site and I tried to email you. Anyway I added my .m file to this posted I am not getting any compile errors anymore but the app is crashingnow and not loading. Before I commented out all the plots and did see the xy axis so I know I installed core plot correctly in the project. Could you please help me get one of these xy graphs running. Thanks for all your help
Nick LaMarca
A: 

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

Eric Skroch
I added this to get it running... double val; if(val !=25) { val = (index/5.0)-5; }but its still not loading in the simulator
Nick LaMarca
This code is off the core-plot tutorial
Nick LaMarca