[英]D3. How do I act on the selected node and all parent nodes
I have a D3 tree diagram link this one... https://bl.ocks.org/mbostock/4339083 and can select a node fine. 我有一个D3树形图链接这一个... https://bl.ocks.org/mbostock/4339083并可以选择一个节点。 I want to be able to iterate over the parent nodes too, and set an attribute eg
d.MainBranch="Yes"
. 我也希望能够遍历父节点,并设置一个属性,例如
d.MainBranch="Yes"
。 The idea is I can right click, get a menu to pop up and then click "Mark as Main Branch". 我的想法是我可以右键单击,弹出菜单,然后单击“标记为主分支”。 I've done the right click to get a popup menu.
我已经完成了右键单击以获取一个弹出菜单。 and can set
d.MainBranch="Yes"
on the current node like this... 并可以像这样在当前节点上设置
d.MainBranch="Yes"
...
function marAsMainBranch(d) {
d.mainBranch="Yes";
update(d);
}
just lacking the ability to iterate over the parents :-( 只是缺乏迭代父母的能力:-(
Can anyone help? 有人可以帮忙吗?
You can do something like this to set parent data and call recursively till no parent (starting from the node which is acted) : 您可以执行以下操作来设置父数据并递归调用直到没有父节点(从被执行的节点开始) :
function setInfoParent(d){
d.mainBranch="Yes";
if(d.parent){
setInfoParent(d.parent);//call recursively itself till no parent.
}
console.log(d);
}
Then in click or right click action call setInfoParent
something like this: 然后在单击或右键单击操作中调用
setInfoParent
,如下所示:
function click(d) {
setInfoParent(d);//call to set all parents.
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.