在D3.js中添加带有嵌套条目的属性 [英] Add attribute with nested entries in D3.js

查看:117
本文介绍了在D3.js中添加带有嵌套条目的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将d3.nest()用于按行值分组

I have use d3.nest() for group by row values

代码就是这样

function createNestingFunction(propertyName){
  return function(d){ 
        return d[propertyName];
     };
}

var levels = ['first', 'second', 'third']

var nest = d3.nest();
for (var i = 0; i < levels.length; i++) {
    nest = nest.key( createNestingFunction(levels[i]) );

}

var root = {
    "key":"root", 
    "values": nest.entries(data) //compute the nest
}

因此,如果有两个键['locationId','userID']

so result will be like this if there are two key ['locationId','userID']

[
 {
    "key":"12345646", <=== this group by first key locationID
    "values": [
      { 
        "key":"asdbnnmb12", <==== this group by second key userId
         "values":[]
      }
     ]
},
{
    "key":"12345we6", <=== this group by first key locationId
    "values": [
      { 
        "key":"hjhggg33", <==== this group by second key userId
         "values":[]
      }
     ]
}

]

现在我怎么只能用钥匙识别?关键是价值 所以我期望的是这样的结果

now how can i identify then only with key ? key is value so what I expect is result like this

[
 {
    "key":"12345646", <=== this group by first key locationID
    "keyArrt":"locationID",
    "values": [
      { 
        "key":"asdbnnmb12", <==== this group by second key userId 
        "keyArrt":"userId",
         "values":[]
      }
     ]
}...

]

所以我可以按级别识别组,因为我必须根据分组级别重定向页面,并为此确定当前级别.

So i can identify the group by level because I have to redirect page based on grouped level and for that i dded to identify current level.

我如何添加此键或其他具有键和值的attr,以便在groupby级别上可以识别它们

how can i add this key or any other attr with key and values so on groupby level i can identify them

更多:示例 只是对您具有社交网络数据进行成像,并且您想要构建层次结构,例如第一层是位置,而不是用户,而是他们的相关帖子(实际上是真实的行数据)

More: example Just imaging you have social network data and you wanted to build hierarchy like this first level is location and than user and than their related post ( which is actually real row data )

因此,您将为每个级别的位置和用户添加两个键.它将很好地工作,但是在获取数据后,您将在递归或简单循环中的某个地方使用此数据,如何确定当前节点是按用户级别还是按位置定位它只会具有实际上不是值的键,而不是attr的键,例如位置说USA,FRANCE的键将显示为USA和FRANCE的键,因此按用户级别分组的键将是用户名,例如AMIT或任何其他名称. .所以基于密钥,我们无法确定该分组的依据..因此,我想要的是,如果密钥是位置值,而不是像密钥attr那样再添加一个attr以及我们仅显示位置"的位置,这样我们就可以识别该对象具有基于位置或用户分组的值.

So you will add two key for each level location and user .. it will work well but after getting data you are using this data somewhere in recursive or simple loop how will you identify that current node is user level goupr by or location it will only have key which is actually value not attr, key like location say USA,FRANCE than that level will be show key as USA and FRANCE, so same on user level group by key will be user name like AMIT or any other name .. so based on key we can't decide which group by is this .. so that what i wanted that if key is location value than just add one more attr like key attr and where we just show "location" so after that we can identify that this object have values group-by based on location or user..

推荐答案

由于.entries(data)返回一个数组,您可以使用连续的map方法添加键attrs.

since .entries(data) returns an array you can use successive map methods to add keys attrs.

.map(function(d){
    var j = 0;
    d.keyAttr = levels[j];

    d.values.map(function(e){
     e.keyAttr = levels[++j];
     return e;
    });
    return d;
  });

这里是一个矮人,它显示了如何处理玩具数据.

Here is a plunker that shows this working with toy data.

这篇关于在D3.js中添加带有嵌套条目的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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