我有一個JSON如下共享,我試圖在可視化之前嵌套數據。我想繪製在政府和非政府下每個供應商是如何分配高和低的項目,所以試圖創建一個JSON對象,如[{v1: {Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, {v2:{Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, ...]
D3嵌套和對象遍歷
我能夠嵌套數據,但無法獲得相應的計數。任何指導表示讚賞。道歉,如果問題構造是模糊的。
[
{
"vendor": "V1",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V2",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V3",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V8",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V9",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V1",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V2",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V3",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V8",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V9",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V1",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V2",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V3",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V8",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V9",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Gov",
"critical": "low"
}
]
沒有彙總
n = d3.nest().key(function(d){return d.vendor;}) .key(function(d){return d.ptype;}) .key(function(d){return d.critical;}) .entries(j);
與彙總
n = d3.nest().key(function(d){return d.vendor;}) .key(function(d){return d.ptype;}) .key(function(d){return d.critical;}) .rollup(function(leaf){ return[ {key:'GH', value:leaf[0].values.length} ,{key:'GL',value:leaf[1].values.length} ]}) .entries(j);
謝謝nnnnnn!我正在尋找更多的D3方式.. :) – user6384905