且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

避免在d3.js中的树布局中的节点重叠

更新时间:2023-01-26 08:39:27

问题。这是我如何解决它。我有宽度分配给每个节点,现在的高度对于所有节点是相同的(基本上节点的高度小于nodeHeight,垂直居中):

I had the same problem. This is how I solved it. I have width assigned to each node, height for now is the same for all nodes (basically nodes with smaller height than nodeHeight, get centered vertically):

var tree = d3.layout.tree().nodeSize([1, nodeHeight])
           .separation(function(a, b) {
               var width = a.width + b.width,
                   distance = width / 2 + 16; // horizontal distance between nodes = 16
                   return distance;
           }),
    nodes = tree.nodes(data),
    links = tree.links(nodes);

希望这有帮助。