2013-12-20 25 views
3

在下面的示例中:http://bl.ocks.org/mbostock/4063570您有一個樹狀圖,其中所有鏈接全部顯示在右側,是否有方法將其分成兩半,另一部分顯示在右側和另一部分在左邊?樹狀圖D3在兩側顯示鏈接

我幾乎能夠做到這一點,但它是不正確的:http://jsfiddle.net/8EM4s/3/。我想用2個獨立的對角線繪製左,右連桿的工作,但仍然計算Y值一樣有直接海誓山盟下:

var diagonalLeft = d3.svg.diagonal() 
     .projection(function(d) { return [-d.y, d.x]; }); 

var diagonalRight = d3.svg.diagonal() 
     .projection(function(d) { return [d.y, d.x]; }); 
+0

不知道你在找什麼? A [二分圖](https://github.com/bitliner/d3-bipartite-graph)? –

+0

像這樣的東西http://intelligentheritage.files.wordpress.com/2011/11/mind-map.png?w=609&h=326 –

+0

然後[這個問題](http://stackoverflow.com/questions/10232850/ js-library-for-creating-a-mindmap-like-interface)可能會有所幫助。 –

回答

4

你可以在這裏找到我的解決方案: http://jsfiddle.net/5TK3d/

 var numItems = Math.round(root.children.length /2); 
     var nodePositionDictionary = {}; 
     var rootLeft = {children:[], name:root.name}; 
     var rootRight = {children:[], name:root.name}; 
     for(var i=0;i<root.children.length;i++) 
     { 
      var node = root.children[i]; 
      if(!(i < numItems)) 
      { 
       rootRight.children.push($.extend({}, node)); 
      }else{ 
       rootLeft.children.push($.extend({}, node)); 
      } 
     }  
     var nodesRight = cluster.nodes(rootRight); 
     for (var i = 0; i < nodesRight.length; i++) { 
      var node = nodesRight[i]; 
      node.right = true; 
      nodePositionDictionary[node.name + (node.parent?node.parent.name:"")] = node; 
     }; 
     var nodesLeft = cluster.nodes(rootLeft); 
     for (var i = 0; i < nodesLeft.length; i++) { 
      var node = nodesLeft[i]; 
      node.right = false; 
      nodePositionDictionary[node.name + (node.parent?node.parent.name:"")] = node; 
     }; 

     // manually create nodes with their positions instead of doing cluster.nodes because 
     // i want them to be drawn left and right 
     var nodes = []; 
     updateNodePositions(root); 
     function updateNodePositions(n) 
     { 
      var nodePosition = nodePositionDictionary[n.name + (n.parent?n.parent.name:"")]; 

      if(nodePosition){ 
       n.x = nodePosition.x; 
       n.y = nodePosition.y; 
       n.depth = nodePosition.depth; 
       nodes.push(n); 
      } 

      for(var i=0; i< n.children.length;i++) 
      { 
       var node = n.children[i]; 
       node.parent = n; 

       nodes.push(node); 

       var childNodePosition = nodePositionDictionary[node.name + (node.parent?node.parent.name:"")]; 
       if(childNodePosition){ 
            node.x = childNodePosition.x; 
            node.y = childNodePosition.y; 
            node.depth = childNodePosition.depth; 
            node.right = childNodePosition.right; 
       } 

       if(node.children){ 
        updateNodePositions(node); 
       } 
      } 

     } 
+0

這個解決方案看起來不錯,但我不知道如何繪製直線來連接節點。你有什麼主意嗎?已經嘗試了一些像http://stackoverflow.com/questions/17787287/d3-js-indented-tree-with-straight-links,但真的弄髒了它。 – row1

+1

像這樣的東西? http://jsfiddle.net/F8BH9/2/ –

+0

看起來很棒,謝謝。 – row1