[英]D3 bind data of nested JSON document
我有以下格式的MongoDB數據。 我想創建一個這樣的數據的多線圖,類似於: http : //bl.ocks.org/mbostock/3884955,X軸上的日期,Y軸上的持續時間和每個作業都是自己的分開的。
我對數據的嵌套特性感到很難 - 我以前做過一些簡單的D3圖表,但我不確定如何綁定和訪問這些數據。 我在Stackoverflow上看過其他幾個類似的例子(使用嵌套函數等),但我還沒有完全理解它,我不確定這是否是最好的解決方案。 任何關於最佳方法的指針都將非常感激。
{
"result" : [
{
"_id" : {
"month" : 1,
"day" : 20,
"year" : 2014,
"job" : "ABC"
},
"build_duration" : 432565
},
{
"_id" : {
"month" : 1,
"day" : 17,
"year" : 2014,
"job" : "ABC"
},
"build_duration" : 543256
},
{
"_id" : {
"month" : 1,
"day" : 17,
"year" : 2014,
"job" : "DEF"
},
"build_duration" : 87634
},
{
"_id" : {
"month" : 1,
"day" : 20,
"year" : 2014,
"job" : "DEF"
},
"build_duration" : 456230
}
],
"ok" : 1
}
這是我要采取的方法。 根據作業ID嵌套數據,然后繪制嵌套數據。 嵌套的代碼很簡單,只需要
var nested = d3.nest().key(function(d) { return d._id.job; })
.entries(data.result);
然后你可以像這樣繪制它(假設這里省略了適當的變量定義)。
svg.selectAll("path").data(nested)
.enter().append("path")
.style("stroke", function(d) { return color(d.key); })
.attr("d", function(d) { return line(d.values); });
在這里完成示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.