2013-10-07 34 views
0

我有一個使用Core-plot(CPTBarPlot)的圖形,我想在X軸上顯示日子,每個條形圖的順序不同。我試圖設置CPTTimeFormatter,如下所示,其中x是我的x軸。CorePlot - CPTTimeFormatter爲我的條形圖的每個元素提供相同的時間

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ; 
    dateFormatter.dateStyle = kCFDateFormatterMediumStyle; 
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter]; 
    NSDate *refDate = [NSDate date]; 

    timeFormatter.referenceDate = refDate; 
    x.labelFormatter = timeFormatter; 

裏面numbersForPlot的我返回一組表示經過秒的數字,因爲我想有連續幾天它看起來像0, 86400, 172800, 259200, 345600, 432000, 等

我預計在這種情況下,X軸標籤是從refDate開始的日期的順序列表,並且在數字格式中增加秒數。相反,我所看到的是相同的日期重複一遍又一遍在x軸上的每個酒吧。任何人都可以向我解釋我做錯了什麼,以及我可以如何解決它?謝謝。

編輯:這是我的numbersForPlot函數看起來像我的數據源中:

-(NSArray *)numbersForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange 
{ 
    NSArray *nums = nil; 

    switch (fieldEnum) { 
     case CPTBarPlotFieldBarLocation: 
      nums = [NSMutableArray arrayWithCapacity:indexRange.length]; 
      for (NSUInteger i = indexRange.location; i < NSMaxRange(indexRange); i++) { 
       [(NSMutableArray *)nums addObject :[NSDecimalNumber numberWithUnsignedInteger:i*secondsPerDay]]; 
      } 
      NSLog(@"NUMS = %@",nums); 

      break; 

     case CPTBarPlotFieldBarTip: 
      nums = [stepsPerDay objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:indexRange]]; 
      break; 

     default: 
      break; 
    } 

    return nums; 
} 

相關部分是返回我相信這是正確的值的N排列的情況下CPTBarPlotFieldBarLocation ...

NUMS =( 0, 86400, 172800, 259200, 345600, 432000,)

回答

0

我想通了。我必須設置

x.majorIntervalLength = CPTDecimalFromInteger(secondsPerDay); 

此錯誤是,雖然困惑我,因爲一切都顯得完美減去X標籤重複,因此很難看到什麼是錯的。一旦我將間隔長度設置爲適當的值,X軸標籤就會正確顯示我想要的日期。

0

您的格式器代碼看起來正確。確保數據源爲相隔一天的值返回正確的x值,例如0,86400,172800等。

+0

這是我的numbersForPlot函數看起來像數據源裏面的內容...哎呀我不能發佈它在這個評論框,因爲它太長了。 – Jackson

+0

我已經更新了原始問題以顯示數據源。它看起來正確嗎? – Jackson

相關問題