d3 tree - 关闭同一父母的所有孩子

时间:2013-12-19 10:53:51

标签: d3.js tree children

嘿我正在使用这个d3 tree.

当我点击具有相同父节点的节点时,是否有可能关闭所有其他子节点。我认为它应该是这样的,但我不知道修改它:

// Transition exiting ndoes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);
  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

例如,如果您查看working example

  • 点击Topic_2并显示其子项(Subtopic 4,Subtopic 5,Subtopic 6)

  • 我点击Topic_1 - Topic_1的孩子打开

  • Topic_2的儿童将被关闭

1 个答案:

答案 0 :(得分:0)

This fiddle做你想要的。我刚做了以下改动:

// Toggle children
function toggle(d) {
    console.log(d);
  if (d.children) {
    d._children = d.children;
    d.children = null;
  }
  else {
    closeSiblings(d);
    d.children = d._children;
    d._children = null;
  }
}

function closeSiblings(d) {
    if (!d.parent) return; // root case
    d.parent.children.forEach(function(d1) {
        if (d1 === d || !d1.children) return;
        d1._children = d1.children;
        d1.children = null;
    });
}
相关问题