javascript - D3嵌套和对象遍历

标签 javascript d3.js nested

我有一个如下共享的 json,我试图在可视化之前嵌套数据。我希望在政府和非政府下绘制每个 vendor 如何分配高低项目的图,因此尝试创建一个像 [{v1: {Gov: {high:3, low:2}, {Non -政府:{高:12,低:1}}},{v2:{政府:{高:3,低:2},{非政府:{高:12,低:1}}},.. .]

我可以嵌套数据,但无法获取相应的计数。任何指导表示赞赏。如果问题结构含糊,我们深表歉意。

    [
  {
    "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);
    

最佳答案

我不知道 D3,但是使用 vanilla 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);

编辑:我想到也许您不想对可能的 ptyperitic 值进行硬编码,所以......以下(丑陋,未优化)版本会随时获取值:

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

关于javascript - D3嵌套和对象遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585265/

相关文章:

list - Erlang映射嵌套列表

javascript - 在nodejs中使用wait.for暂停非阻塞文件读取

d3.js - d3 js 节点作为图像

javascript - 如何格式化用户输入以允许 xx.xx 格式

javascript - 垂直树结构方向

javascript - D3 从flare.csv 中过滤树状图的根数据

java - 使RelativeLayout可滚动

elasticsearch - 嵌套聚合Elasticsearch

javascript - JavaScript 的非贪婪正则表达式

javascript - 如何在加载页面和提交表单时提交表单?