2012-04-11 23 views
3

按照不同的教程,我已經使用Core-Plot成功創建了散點圖。現在,在X軸上設置日期我正在獲取完全錯誤的日期。 我的圖形的來源是一個包含字典的數組。在字典的每個記錄中,字典的第一個鍵是一個unix時間戳,第二個鍵是要繪製的值。它看起來像這樣:Core-Plot:x軸上的錯誤日期

(
      { 
     0 = 1334152500; 
     1 = 0; 
    }, 
      { 
     0 = 1334152800; 
     1 = 0; 
    }, 
      { 
     0 = 1334153100; 
     1 = 0; 
    }, 

AFAIK Core-Plot需要一個開始日期和一個步驟。在這種情況下,開始日期爲1334152500(格林威治標準時間2012年4月11日13:55:00),步驟爲300秒。每隔300秒出現一個新值。 現在代碼我用來繪製X軸:

// this string contains the first UNIX Timestamp registered 
NSString *tstamp_start = [NSString stringWithFormat:@"%@", [[[self.graphData objectForKey:@"1"] objectAtIndex:0] objectForKey:@"0"]]; 
NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:[tstamp_start doubleValue]]; 

// definition of the graph skipped... 

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet; 
// tInterval is previous set to 300 - float. 
// here I should set the Interval between every value on graph... 
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(tInterval); 
axisSet.xAxis.axisLineStyle = axisLineStyle; 
axisSet.xAxis.majorTickLineStyle = axisLineStyle; 
axisSet.xAxis.minorTickLineStyle = axisLineStyle; 
axisSet.xAxis.labelTextStyle = textStyle; 
axisSet.xAxis.labelOffset = 3.0f; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mma"]; 
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter]; 
timeFormatter.referenceDate = refDate; 
axisSet.xAxis.labelFormatter = timeFormatter; 
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic; 
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yAxisMin); 
(...) 

線被正確地示出,但在X軸我讀值/如標籤:'22/07/2054上午6點35分」。 2054年?在不考慮時區(本地+2,unix +0)和CPTAxisLabelingPolicyAutomatic的情況下,至少我應該在13:55到現在的當天讀取日期。我錯過了什麼?

非常感謝!

西蒙

回答

5

referenceDate是偏移格式化當施加到每個數據點。因此,第一個數據點的值正在翻倍。您需要調整參考日期或將數據值更改爲從0開始。

NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0.0]; 
+0

謝謝,這絕對是我的錯誤:) – Simon 2012-04-12 19:16:24