2013-12-19 47 views
1

嘿即时通讯使用这种d3 tree.D3树 - 接近同一母公司的所有儿童

有没有一种可能,关闭所有其他子节点,当我一个节点上单击具有相同的父。我想应该是这样的,但我不知道对其进行修改:

// 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被点击和它的孩子(子主题4 ,副主题5,二级主题6)所示

  • 我点击TOPIC_1 - TOPIC_1的儿童开放

  • Topic_2 S的儿童大厅关闭

回答

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