D3嵌套和对象遍历 [英] D3 nesting and object traversal

查看:771
本文介绍了D3嵌套和对象遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json在下面共享,我试图嵌套数据之前可视化。我希望在gov和non-gov下绘制每个供应商如何分配高低项目,因此试图创建一个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} ,...]



我可以嵌套数据,但无法获取相应的计数。任何指导是赞赏。

  [
{
vendor:V1
ptype:Gov,
critical:high
},
{
vendor:V2,
ptype:Gov,
critical:low
},
{
vendor:V3,
ptype Gov,
critical:high
},
{
vendor:V4,
ptype: ,
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 b $ b},
{
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
},
{
: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 $ bcritical:high
},
{
vendor:V5,
ptype:Non-Gov,
critical:high
},
{
vendor:V6,
ptype:Gov,
critical high
},
{
vendor:V7,
ptype:Gov,
critical:high b $ b},
{
vendor:V8,
ptype:Non-Gov,
critical:low
},
{
vendor:V9,
ptype:Gov,
critical:high
} b $ b {
vendor:V10,
ptype:Gov,
critical:low
}
] b $ b




  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}
    ]})



解决方案

  var nested_data = d3.nest()
.key 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
.entries(j);

谢谢!


i have a json as shared below and I am trying to nest the data prior to visualization. I wish to plot under gov and non-gov how every vendor is distributed for high and low projects so trying to create a json object like [{v1: {Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, {v2:{Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, ...]

I am able to nest the data but unable to get the respective counts. Any guidance is appreciated. Apologies if the question construct is vague.

    [
  {
    "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. without rollup

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

  2. with rollup

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

解决方案

i could do it this way..

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

Thanks!

这篇关于D3嵌套和对象遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆