2011-09-21 61 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; 
    }