2011-09-21 26 views
0

我想知道我需要如何准备数据,以便为核心图做好准备。在y轴每年 核心 - 如何打包数据

  • DAYOFYEAR

    • 一行在x轴

    我的x轴具有366点(一年的每一天)。目前我有一本看起来像这样的字典

    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; 
        }; 
    

    我认为core-plot需要一个数组不是吗?你如何打包这是最有效的?

  • 回答

    0

    其实我已经改变了结构这一点。 以年份为关键字的数字字典,以及每个包含dayofyear和value的点的数组。

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

    这样的实现是很容易

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

    数据结构的选择完全取决于您。你已经有了字典中的数据,所以保持这一点。实现你的数据源下面的方法:

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

    假设你有每年为一个单独的情节,使用plot参数来选择从数据字典中适当一年字典。使用fieldEnum参数可确定图是否要求x或y值,并使用参数index来决定要返回的列表中的哪个值。

    例如(假设所有的字典值被存储为NSNumber的对象和您使用的是散点图):

    -(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; 
    }