Question

I'm implementing a bar graph and am having issues understanding the two method numberForPlot:field:recordIndex: and numberOfRecordsForPlot

I currently have

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return 4;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx {
    switch (idx) {
        case 0:
            return @1;
            break;
        case 1:
            return @2;
            break;
        case 2:
            return @3;
            break;
        case 3:
            return @4;
            break;
        default:
            return @0;
            break;
    }
}

which makes a graph as expected. When I change say the @4 to @5 it displays the last bar with an empty bar space next to it. This makes sense if I'm plotting both the x and y position for each of the 4 entries according to numberOfRecordsForPlot but when I log the information in numberForPlot it only has 0 & 1 for the fieldEnum.

I've looked at the doco and examples and is no clearer to me. Could someone please explain?

Was it helpful?

Solution

The main problem is that the fieldEnum of that delegate method is not doing what you think. It will either have the value of CPTBarPlotFieldBarLocation (the 'x' axis location) or CPTBarPlotFieldBarTip (the bar height), so these should be the cases used in the switch statement. The idx refers to the specific bar.

Here I have put the heights of the bars in a property of the data source object called plotData.

self.plotData = @[@(1), @(2), @(3), @(4)];

Then you can implement the delegate method like this,

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

    switch ( fieldEnum ) {
        case CPTBarPlotFieldBarLocation:
            return @(idx);
            break;

        case CPTBarPlotFieldBarTip:
            return [plotData objectAtIndex:idx];
            break;

        default:
            break;
    }

    return nil;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top