0
votes

I am new to CorePlot and having a little trouble understanding "data source". I am sure that I have most likely misinterpreted some of the functions. Right now, I am trying to display the 2 json data I have already gotten and saved into _soldarray and _datearray.

example of the data received (as it is on nsLog):

_soldarray : {0, 0, 0, "62.69", "48.3", 81,}
_datearray : {("02/07/12", "02/10/12", "02/14/12", "02/11/12", "02/10/12", "02/12/12"}

and I have

CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [_soldarray count];
}

-(NSArray *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSUInteger)index {

switch (fieldEnum) {
    case CPTScatterPlotFieldX:

        for (int i =0 ; 50;i++){
            return [_datearray objectAtIndex:i];
        }
        break;

    case CPTScatterPlotFieldY:
        for (int j =0 ; 50;j++){
            return [_soldarray objectAtIndex:j];
        }

        break;
}
return 0;

}

and as for configuration:

CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
// 2 - Create the three plots
CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
aaplPlot.dataSource = self;
aaplPlot.identifier = _soldarray;
CPTColor *aaplColor = [CPTColor redColor];
[graph addPlot:aaplPlot toPlotSpace:plotSpace];

I tried changing datasource to the array but that does not work as well. Can someone point me to the right directions. I am having trouble understanding how plot.datasource and how -(NSArray *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSUInteger) index works. Thanks in advance!

1

1 Answers

0
votes

-numbersForPlot:field:recordIndexRange: expects the datasource to return an array of values for the given field while -numberForPlot:field:recordIndex: returns one value at a time for the given index.

Assuming that the values in the arrays are NSNumber objects (or something else that responds to -decimalValue or -doubleValue like NSString), you can implement this either way.

-(NSNumber *)numberForPlot:(CPTPlot *)plot
                     field:(NSUInteger)fieldEnum
               recordIndex:(NSUInteger)idx
{
    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            return [_datearray objectAtIndex:idx];
            break;

        case CPTScatterPlotFieldY:
            return [_soldarray objectAtIndex:idx];
            break;
    }
    return nil;
}

or

-(NSArray *)numbersForPlot:(CPTPlot *)plot
                     field:(NSUInteger)fieldEnum
          recordIndexRange:(NSUInteger)indexRange
{
    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            return [_datearray subarrayWithRange:indexRange];
            break;

        case CPTScatterPlotFieldY:
            return [_soldarray subarrayWithRange:indexRange];
            break;
    }
    return nil;
}