2017-11-07 3 views
3

これは私が意図するよりも複雑に聞こえるかもしれませんが、D3 V3からD3 V4に切り替わります。私の他のグラフやグラフはすべて本当に簡単に転送されていますが、この特定のツリー(ツリー)はd3.layout.treeからすべての種類の悲しみを変更し、単に.children()関数を使用してノード/ d3.hierarchy()を使用して新しい方法に古い方法。私は無数のチュートリアルで見たように設定しました。D3 V3からD3 V4ツリー配列が階層化を使用して誤って並べ替えされました

私は2種類のプランカを設定しています。 この1つはD3 V3であり、正しい配列順序で正しく表示/動作している: https://plnkr.co/edit/qXDWDGzhNIM2tTOoAXRa?p=preview

How the Tree Should Look

Correct Data Order

var tree = d3.layout.tree() 
       .nodeSize([0, 75]) 
       .children(function(d) { 
        return d.subsidiaries; 
       }); 

      var line = d3.svg.line().interpolate('step') 
       .x(function(d) { 
        return d.x; 
       }) 
       .y(function(d) { 
        return d.y - 12; 
       }); 

      // Define 'div' for tooltips 
      var div = d3.select('#tree') 
       // Declare the tooltip div 
       .append('div') 
       // Apply the 'tooltip' class 
       .attr('class', 'tooltip') 
       .style('opacity', 0); 

      var svg = d3.select('#tree').append('svg') 
       .attr('width', width + margin.left + margin.right) 
       .append('g') 
       .attr('transform', `translate(${ margin.left },${ margin.top })`); 

      function lineData(d) { 
       var points = [{ 
        x: d.source.y, 
        y: d.source.x 
       }, { 
        x: d.target.y, 
        y: d.target.x 
       }]; 

       return line(points); 
      } 

      root = treeData; 
      root.x0 = 0; 
      root.y0 = 0; 

      update(root); 

      function update(source) { 
       // Compute the flattened node list. 
       var nodes = tree.nodes(root); 
       console.log(nodes); 

       var height = Math.max(500, nodes.length * barHeight + margin.top + margin.bottom); 

       d3.select('svg').transition() 
        .duration(duration) 
        .attr('height', height); 

       d3.select(window.frameElement).transition() 
        .duration(duration) 
        .style('height', `${ height }px`); 

       // Compute the layout. 
       nodes.forEach(function(n, i) { 
        n.x = i * barHeight; 
       }); 

       // Update the nodes. 
       var node = svg.selectAll('g.node') 
        .data(nodes, function(d) { 
         return d.id || (d.id = ++i); 
        }); 

この第二つD3 V4であり、それが明らかです配列の順序が非常に間違っていることを伝えます。オブジェクトの多くは、彼らがすることになっているところから、リストを上に移動しているので、ここに見られるように、それは奇妙なCOLLIDED効果を引き起こしている: https://plnkr.co/edit/PGPw92URj24yDMrPenwE?p=preview

How the Tree Looks

Different Data Structure and Object Order

function _drawTree(treeData) { 
      // This solves the maximum call stack issue with d3 layout function 
      const margin = { 
        top: 55, 
        right: 20, 
        bottom: 30, 
        left: 30 
       }, 
       width = 960 - margin.left - margin.right, 
       barHeight = 65, 
       barWidth = width * 0.35, 
       duration = 400, 
       treeMap = d3.tree() 
        .nodeSize([0, 75]); 

      let i = 0, 
       root; 

      var line = d3.line() 
       .x(d => d.x) 
       .y(d => d.y - 12) 
       .curve(d3.curveStep); 

      // Define 'div' for tooltips 
      var div = d3.select('#tree') 
       // Declare the tooltip div 
       .append('div') 
       // Apply the 'tooltip' class 
       .attr('class', 'tooltip') 
       .style('opacity', 0); 

      var svg = d3.select('#tree').append('svg') 
       .attr('width', width + margin.left + margin.right) 
       .append('g') 
       .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); 

      function lineData(d) { 
       var points = [{ 
        x: d.source.y, 
        y: d.source.x 
       }, { 
        x: d.target.y, 
        y: d.target.x 
       }]; 

       return line(points); 
      } 

      root = d3.hierarchy(treeData, d => { 
       return d.subsidiaries; 
       }); 
      root.x0 = 0; 
      root.y0 = 0; 

      update(root); 

      function update(source) { 
       var tree = treeMap(root); 

       var nodes = tree.descendants(root); 

       console.log(nodes); 

       // Compute the flattened node list. 
       var height = Math.max(500, nodes.length * barHeight + margin.top + margin.bottom); 

       d3.select('svg').transition() 
        .duration(duration) 
        .attr('height', height); 

       d3.select(window.frameElement).transition() 
        .duration(duration) 
        .style('height', height + 'px'); 

       // Compute the layout. 
       nodes.forEach((n, i) => { 
        n.x = i * barHeight; 
       }); 

私はしました必要な変数をログに記録し、開発コンソールをポップアップして表示することができます。

これが十分な情報であるかどうかわからない場合は、もっと必要な場合はお知らせください。助けてくれてありがとう!!!

答えて

0

答えは実際には比較的簡単だったことが判明しました。私は各ノードの代わりにforEachを使用していました。ノードをレイアウトする前に。この解決策はGitHub Issueです。

彼らは、このexampleにリンクし、正しい修正はこれです:

私もアクションで正しいコードと私のPlunkerを更新しました
// Compute the "layout". TODO https://github.com/d3/d3-hierarchy/issues/67 
var index = -1; 
root.eachBefore(function(n) { 
    n.x = ++index * barHeight; 
    n.y = n.depth * 20; 
}); 

...

関連する問題