2013-01-03 52 views
0

如何爲我在d3.js中創建的圖形設置此樣本數據的格式?D3的格式數據

[ 
    [ 
     [ 
      "2", 
      "2013-01-02 00:00:00+00" 
     ], 
     [ 
      "3", 
      "2012-12-28 00:00:00+00" 
     ], 
     [ 
      "3", 
      "2012-12-27 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-12-26 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-12-24 00:00:00+00" 
     ], 
     [ 
      "4", 
      "2012-12-21 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-12-20 00:00:00+00" 
     ] 
    ], 
    [ 
     [ 
      "1", 
      "2012-12-27 00:00:00+00" 
     ], 
     [ 
      "2", 
      "2012-12-21 00:00:00+00" 
     ], 
     [ 
      "2", 
      "2012-12-18 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-12-17 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-11-27 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-11-03 00:00:00+00" 
     ], 
     [ 
      "1", 
      "2012-10-27 00:00:00+00" 
     ] 
    ] 
] 

目前,我的JavaScript控制檯給我d3.v3.js內Uncaught TypeError: Cannot use 'in' operator to search for '_dateElement' in NaN:1033當D3試圖解析這個數據

似乎D3並不想對付引號中的數字,沒有引號會導致無效的json,而我的ajax調用將不會返回,這意味着我將無法解析我的d3函數中的數組。

我相信d3可以解析和格式化json,但我處於僵局。如何格式化這些數據,日期對象是我的X軸的升序,Y軸是整數值的最大值

+0

你能簡單的通過'parseInt函數()'通過INT字符串? –

回答

2

您需要使用數據將輸入數組解析爲輸出數組以你想要的格式。正如C.Reed在評論中所說,使用parseInt將字符串轉換爲數字並使用d3.time.format將字符串轉換爲日期。 https://github.com/mbostock/d3/wiki/Time-Formatting

var date_in = "2013-01-02 00:00:00+00"; 
var format = d3.time.format("%Y-%m-%d 00:00:00+00"); 
var date_out = format.parse(date_in); 

(假定時間始終爲0)