我正在嘗試改編Mike Bostock的泳道日曆http://bl.ocks.org/bunkat/1962173,並用JSON文件替換隨機數據生成函數。d3.js用異步函數替換random()數據函數
d3.json("random.json", function(data) {
// console.log(d3.entries(data));
//var data = randomData()
var lanes = data.lanes;
var items = data.items;
var now = new Date();
.....
});
在數據文件看起來像隨機()函數的輸出:
{"lanes":[{"id":0,"label":"lane 0"},{"id":1,"label":"lane 1"},{"id":2,"label":"lane 2"},{"id":3,"label":"lane 3"},{"id":4,"label":"lane 4"}],"items":[{"id":0,"lane":0,"start":"2012-01-21T18:00:00.000Z","end":"2012-01-26T21:00:00.000Z","class":"past","desc":"This is a description."},{"id":1,"lane":0,"start":"2012-01-28T18:00:00.000Z","end":"2012-02-02T21:00:00.000Z","class":"past","desc":"This is a description."},{"id":2,"lane":0,"start":"2012-02-05T13:00:00.000Z","end":"2012-02-10T18:00:00.000Z","class":"past","desc":"This is a description."},{"id":3,"lane":0,"start":"2012-02-13T15:00:00.000Z","end":"2012-02-15T13:00:00.000Z","class":"past","desc":"This is a description."},{"id":4,"lane":0,"start":"2012-02-17T16:00:00.000Z","end":"2012-02-22T13:00:00.000Z","class":"past","desc":"This is a description."},{"id":5,"lane":0,"start":"2012-02-23T13:00:00.000Z","end":"2012-02-23T21:00:00.000Z","class":"past","desc":"This is a description."},{"id":6,"lane":0,"start":"2012-02-24T18:00:00.000Z","end":"2012-03-01T14:00:00.000Z","class":"past","desc":"This is a description."}
我得到的錯誤是:
TypeError: n.getFullYear is not a function
這是great-我要看看我是否可以在陣列中替換日期的項目,而不是後期處理它們。 – user1515373 2015-02-06 14:29:29
代替迭代項目,我用 替換了所有對'd.start'和'd.end'的引用d.start = new Date(d.start); 012.ec =新日期(d.end); 完美的作品! – user1515373 2015-02-06 14:49:43