简体   繁体   English

关于d3.layout.pack()的一些简单问题

[英]A few simple questions about d3.layout.pack()

Please take a look to this simple jsfiddle: http://jsfiddle.net/perikut/9qUVW/2/ (sorry if using Firefox, I don't know why it doesn't look good...) 请看看这个简单的jsfiddle: http//jsfiddle.net/perikut/9qUVW/2/ (对不起,如果使用Firefox,我不知道为什么它看起来不太好......)

在此输入图像描述

In our object can we use another word than 'children' to indicate where extract data from? 在我们的对象中,我们可以使用另一个词而不是'children'来表示从哪里提取数据? (all examples I see follow this data structure, see below). (我看到的所有示例都遵循此数据结构,请参阅下文)。 Where should we indicate that? 我们应该在哪里表明?

I consider my code quite deficient (see the jsfiddle), as I am forced to declare two times the 'group' parameter in order to show/hide childrens from a group/parent. 我认为我的代码非常缺乏(参见jsfiddle),因为我被迫宣布两次'group'参数,以显示/隐藏来自组/父级的子代。

There would be no way to directly select group1's children nodes and apply animations over? 没有办法直接选择group1的子节点并应用动画? I want a much more complex data structure in the future, so I need to know this kind of basics before. 我希望将来有一个更复杂的数据结构,所以我之前需要了解这种基础知识。

Current data structure: 目前的数据结构:

data = {
name:'root',
group:'no_group',
children:[
    {
        group: 'group1',
        children:[
            { group:'group1',name:'a1',value:10,color:'red' },
            { group:'group1',name:'a2',value:40,color:'lightcoral' }
        ]
    }
    , { .... } 

In D3's hierarchical layouts , all nodes are populated with a set of standard attributes , including a "parent" attribute. 在D3的分层布局中 ,所有节点都填充了一组标准属性 ,包括“父”属性。 So you can avoid specifying a "group" attribute, and use "parent" instead when selecting the children of a particular node: 因此,您可以避免指定“group”属性,并在选择特定节点的子节点时使用“parent”:

d3.selectAll("circle").filter(function(d) { return d.parent.name === "foo"; });

Alternatively, you can compare by object reference if you have a reference to the node object itself. 或者,如果您有对节点对象本身的引用,则可以按对象引用进行比较。

var parent = nodes.filter(function(d) { return d.name === "foo"; });
d3.selectAll("circle").filter(function(d) { return d.parent === parent; });

Here I'm assuming that each node has a "name" attribute. 这里我假设每个节点都有一个“name”属性。

You also mentioned using retrieving children from a different attribute. 您还提到了使用从不同属性中检索子项。 Yes, this can be achieved using the "children" accessor . 是的,这可以使用“儿童”访问器来实现。 Note that this will store a node's children in an attribute called "children" on that node, overwriting anything that may be there already. 请注意,这会将节点的子节点存储在该节点上名为“children”的属性中,从而覆盖可能存在的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM