0
votes

I was wondering how I need to prepare the data so it is ready for core-plot.

  • one line per year
  • dayOfYear in the x-axis
  • value on the y-axis

My x-axis has 366 points (Each day of the year). At the moment I have a dictionary that looks like this

 2009 (year) =     {
        151 (dayofyear) = 5 (value);
        192 = 25;
        206 = 5;
        234 = 20;
        235 = 20;
        255 = 20;
        262 = 10;
        276 = 10;
        290 = 10;
        298 = 7;
        310 = 1;
        338 = 3;
        354 = 5;
        362 = 5;
    };
    2010 =     {
        114 = 7;
        119 = 3;
        144 = 7;
        17 = 5;
        187 = 10;
        198 = 7;
        205 = 10;
        212 = 10;
        213 = 20;
        215 = 5;
        247 = 10;
        248 = 10;
        256 = 10;
        262 = 7;
        264 = 10;
        277 = 10;
        282 = 3;
        284 = 7;
        47 = 5;
        75 = 7;
        99 = 7;
    };
    2011 =     {
        260 = 10;
    };

I think core-plot needs an array doesn't it? How would you pack this to be the most efficient?

2

2 Answers

1
votes

The choice of data structure is completely up to you. You already have the data in a dictionary, so keep that. Implement the following method in your datasource:

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

Assuming you have a separate plot for each year, use the plot parameter to select the appropriate year dictionary from the data dictionary. Use the fieldEnum parameter to determine whether the plot is asking for the x or y value and the index parameter to decide which value in the list to return.

For instance (assuming all values in the dictionaries are stored as NSNumber objects and you're using a scatter plot):

-(NSNumber *)numberForPlot:(CPTPlot *)plot
                     field:(NSUInteger)fieldEnum
               recordIndex:(NSUInteger)index
{
    NSDictionary *year = // retrieve the year dictionary based on the plot parameter

    NSDictionary *yearData = [year objectAtIndex:index];

    NSNumber *num = nil;

    switch ( fieldEnum ) {
        case CPTScatterPlotFieldX:
            num = [yearData objectForKey:@"dayofyear"];
            break;

        case CPTScatterPlotFieldY:
            num = [yearData objectForKey:@"value"];
            break;

        default:
            break;
    }

    return num;
}
0
votes

I have actually changed the structure to this. A dictionary with the year as the key and an array for each point containing the dayofyear and the value.

2009 =     (
                (354,5),
                (338,3),
                (234,20),
                (298,7),
                (192,25)
)

That way the implementation was very easy

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    return [[[self.data objectForKey:plot.identifier] objectAtIndex: index] objectAtIndex: fieldEnum];
}