2013-12-19 48 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; 
    }); 
}