2016-09-20 10 views
0

私は以下の共有としてjsonを持っており、私は視覚化の前にデータをネストしようとしています。私はgovとnon-govの下でどのように各ベンダーが高低プロジェクトのために配布されているかを確認したい。[{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;}) 
         .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); 

ありがとう:yは、unoptimsed)バージョンは、それが行くような値をつかみます!

1

n = d3.nest().key(function(d){return d.vendor;}) 
    .key(function(d){return d.ptype;}) 
    .key(function(d){return d.critical;}) 
    .entries(j); 
  • のない私は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);

    EDIT:それは多分と私に起こりました可能なptypecriticalの値をハードコードしたくないので、次のようにします(ugl ..

    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

    関連する問題