0
votes

I've been able to get the axes for a standard core-plot graph to show up, but I'm having some trouble actually adding points to it.

I have these two methods declared in the .m file (found from another question on this site):

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;


-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:

So, at the top of that controller.m file, I have this:

NSArray *myDataArray;

Lastly, in the viewDidLoad method (still .m file), near the end (after creating the graph), I have an array with points set up.

When I build and run, XCode gives 0 errors or warnings, but the points do not show up on the graph.

Would appreciate any help, thanks!

1
Do you have the delegate protocols declared in the header file? For example, if you are using a bar graph, you should have CPBarPlotDelegate and CPPlotDataSource in the header. Otherwise, those methods above do not get called. Hopefully that's the problem. Otherwise, we'll have to do a little more digging - justin

1 Answers

2
votes

I thought this would be too confusing to read in the comments, so I'm moving to an answer.

In your numberForPlot method, try something like the following:

- (NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { 
    if (fieldEnum == CPScatterPlotFieldX) {
        // should return data for x axis point;
        return [[myDataArray objectAtIndex:index] objectAtIndex:index];
    }
    else {
        // y axis point;
        return [NSDecimalNumber numberWithInt:index];
    }
}

I apologize if it's pretty crude. I just rewrote a bit from a bar graph to scatter plot. But this should do the trick. I think the issue was that you were calling objectAtIndex:fieldEnum. This had me confused for a while also when I first started using CorePlot. Again, if this doesn't do the trick, just say so and we'll try something new. But I believe this should do it, or something very similar.

EDIT: In the example code provided with CorePlot, here is the code for the data itself:

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSDecimalNumber *num = nil;
    if ( [plot isKindOfClass:[CPPieChart class]] ) {
        if ( index >= [self.dataForChart count] ) return nil;

        if ( fieldEnum == CPPieChartFieldSliceWidth ) {
            num = [self.dataForChart objectAtIndex:index];
        }
        else {
            num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
        }
    }
    else if ( [plot isKindOfClass:[CPBarPlot class]] ) {
        switch ( fieldEnum ) {
        case CPBarPlotFieldBarLocation:
            if ( index == 4 ) {
                num = [NSDecimalNumber notANumber];
            }
            else {
                num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            }
            break;
        case CPBarPlotFieldBarTip:
            if ( index == 8 ) {
                num = [NSDecimalNumber notANumber];
            }
            else {
                num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index+1)*(index+1)];
                if ( [plot.identifier isEqual:@"Bar Plot 2"] ) {
                    num = [num decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"10"]];
                }
            }
            break;
        }
    }
    else {
        if ( index % 8 ) {
            num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
            // Green plot gets shifted above the blue
            if ( [(NSString *)plot.identifier isEqualToString:@"Green Plot"] ) {
                if ( fieldEnum == CPScatterPlotFieldY ) {
                    num = (NSDecimalNumber *)[NSDecimalNumber numberWithDouble:[num doubleValue] + 1.0];
                }
            }
        }
        else {
            num = [NSDecimalNumber notANumber];
        }
    }
    return num;
}

Sorry for including all of it, but I thought it might make it easier to look at all of the types to better differentiate how to approach the scatter plot. Creating their data, it looks like this:

NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 60; i++ ) {
    id x = [NSNumber numberWithFloat:1+i*0.05];
    id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
self.dataForPlot = contentArray;

So maybe if you try a format like that, it could work. If that doesn't do the trick, then we know it's an issue with the graph creation itself and not the data