2016-09-20 61 views
0

我有一個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" 
    } 
] 
  1. 沒有彙總

    n = d3.nest().key(function(d){return d.vendor;}) 
        .key(function(d){return d.ptype;}) 
        .key(function(d){return d.critical;}) 
        .entries(j); 
    
  2. 與彙總

    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); 
    

回答

1

我能做到這樣..

var nested_data = d3.nest() 
.key(function(d) { return d.vendor; }).sortKeys(d3.ascending) 
.key(function(d) { return d.ptype; }).sortKeys(function(d) { return d;}) 
.key(function(d) { return d.critical; }).sortKeys(function(d) { return d;}) 
.rollup(function(leaves) { return leaves.length; }) 
.entries(j); 

謝謝!

1

我不知道D3,但使用香草JS你可以按如下轉換數據:

var input = [ { "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" } ]; 
 

 
var working = input.reduce(function(p, c) { 
 
    var v = p[c.vendor]; 
 
    if (!v) v = p[c.vendor] = {Gov: {high: 0, low: 0}, "Non-Gov": {high: 0, low: 0}}; 
 
    v[c.ptype][c.critical]++; 
 
    return p; 
 
    }, {}); 
 

 
var output = Object.keys(working).map(function(v) { 
 
    var o = {}; 
 
    o[v] = working[v]; 
 
    return o; 
 
    }); 
 

 
console.log(output);

或者,如果你可以使用ES6語法可以使.map()部分短了很多:

var input = [ { "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" } ]; 
 

 
var working = input.reduce((p, c) => { 
 
    var v = p[c.vendor]; 
 
    if (!v) v = p[c.vendor] = {Gov: {high: 0, low: 0}, "Non-Gov": {high: 0, low: 0}}; 
 
    v[c.ptype][c.critical]++; 
 
    return p; 
 
    }, {}); 
 

 
var output = Object.keys(working).map(v => ({ [v]: working[v] })); 
 

 
console.log(output);

編輯:發生在我身上,也許您不希望對可能的ptypecritical值進行硬編碼,所以...以下(ugl Y,因爲它去unoptimsed)版本抓起值:

var input = [ { "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" } ]; 
 

 
var working = input.reduce((p, c) => { 
 
    var v = p.vendors[c.vendor]; 
 
    if (!v) v = p.vendors[c.vendor] = {}; 
 
    if (!v[c.ptype]) v[c.ptype] = {}; 
 
    if (!v[c.ptype][c.critical]) v[c.ptype][c.critical] = 1; 
 
    else v[c.ptype][c.critical]++; 
 

 
    p.ptypes[c.ptype] = true; 
 
    p.criticals[c.critical] = true; 
 

 
    return p; 
 
    }, {vendors:{}, ptypes:{}, criticals:{}}); 
 

 
var output = Object.keys(working.vendors).map(v => { 
 
    var c = working.vendors[v]; 
 
    Object.keys(working.ptypes).forEach(p => { 
 
     if (!c[p]) c[p] = {}; 
 
     Object.keys(working.criticals).forEach(q => c[p][q] || (c[p][q] = 0)); 
 
    }); 
 
    return { [v]: c }; 
 
    }); 
 

 
console.log(output);

+0

謝謝nnnnnn!我正在尋找更多的D3方式.. :) – user6384905