2016-05-13 65 views
1

我有數據。比方說:檢查D3中所有數據點之間的所有值

date,comments 
19-Apr-2016,Today was great. 
21-Apr-2016,Today was not so great. 
1-May-2016,Interesting things happened! 

(所有時間被解析爲當然的日期)

我想通過從2016年4月19日,每天2016年5月1日,讓一個元素爲每天,然後在存在相關數據的地方注入數據。例如:

19-Apr-2016 - Today was great. 
20-Apr-2016 - <No entry> 
21-Apr-2016 - Today was not so great. 
22-Apr-2016 - <No entry> 
... 
1-May-2016 - Interesting things happened! 

這是當前的代碼。它的前和每一個數據點創建了一個元素:

preview.selectAll("svg") 
    .append("g") 
    .data(sums) 
    .enter() 
    .append("rect") 
    // Drawing code etc. Setting height, class, color, w/e 

我如何遍歷所有天,無論是否有他們的數據?

+0

有點需要向我們展示更多的代碼,最好是您的數據... – thatOneGuy

回答

2

這是我將如何處理這個問題的僞代碼。

想象一下你的資金的對象是由你

19-Apr-2016 - Today was great. 20-Apr-2016 - <No entry> 21-Apr-2016 - Today was not so great. 22-Apr-2016 - <No entry> ... 1-May-2016 - Interesting things happened!

現在在我的函數,其中我畫我會做這樣的

var start = new Date(2016, 3, 19);//19-Apr-2016 
var end = new Date(2016, 4, 1);//1-May-2016 
for (var d = start; d <= end; d.setDate(d.getDate() + 1)) { 
    //for each date append a group 
    var grp = preview.selectAll("svg").append("g") 
    var myData = //get the data from sums array for which the date is d(in iterator) 
    if(myData){ 
    grp.append("rect").style("stroke", 1) ... 
    }else { 
    //date not in the sums array. 
    } 

} 

希望這有助於存儲表示的數據的數組!

0

D3的內置nest可能是最簡單的方法。

不要直接綁定sums。數據首先按nest按日期分組,然後根據該分組繪製圖表。

+0

嘿。我編輯了一下我的問題。也許它現在更有意義。 – cwj