D3 树方形节点不在所需位置

D3 tree square node not in desired position

我想像这样使用 d3 创建树视图 http://bl.ocks.org/mbostock/4339083, 但不是节点中的圆圈,我想要正方形。我发现这个 post 给了我一个线索 d3.js: modifyng links in a tree layout but not solved my issue. This is my fiddle http://jsfiddle.net/yp7o8wbm/

如您所见,所有节点的位置都不正确。

这是js代码:

var margin = {top: 20, right: 120, bottom: 20, left: 120},
 width = 960 - margin.right - margin.left,
 height = 500 - margin.top - margin.bottom;


var rectSize = 40;

var i = 0;

var tree = d3.layout.tree().size([height, width]);

var diagonal = d3.svg.diagonal()
                                .source(function(d) { return {"x":d.source.x, "y":(d.source.y+rectSize)}; })            
                                   .target(function(d) { return {"x":(d.target.x), "y":d.target.y}; })
                                .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
 .attr("width", width + margin.right + margin.left)
 .attr("height", height + margin.top + margin.bottom)
 .append("g")
 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

root = treeData[0];

update(root);

function update(source) {

          // Compute the new tree layout.
          var nodes = tree.nodes(root).reverse(),
           links = tree.links(nodes);

          // Normalize for fixed-depth.
          nodes.forEach(function(d) { d.y = d.depth * 180; });

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

          // Enter the nodes.
          var nodeEnter = node.enter().append("g")
           .attr("class", "node")
           .attr("transform", function(d) { 
            return "translate(" + d.y + "," + d.x + ")"; });




        nodeEnter.append("rect")

          .attr("x", function(d) { return d.x ; })
          .attr("y", function(d) { return d.y ; })
          .attr("width", rectSize)
          .attr("height", rectSize);


          nodeEnter.append("text")
           .attr("x", function(d) { 
            return d.children || d._children ? -13 : 13; })
           .attr("dy", ".35em")
           .attr("text-anchor", function(d) { 
            return d.children || d._children ? "end" : "start"; })
           .text(function(d) { return d.name; })
           .style("fill-opacity", 1);

          // Declare the links…
          var link = svg.selectAll("path.link")
           .data(links, function(d) { return d.target.id; });

          // Enter the links.
          link.enter().insert("path", "g")
           .attr("class", "link")
           .attr("d", diagonal);

}  

我不知道我的代码哪里出错了?

您以不同的方式两次设置位置:

var nodeEnter = node.enter().append("g")
  .attr("class", "node")
  .attr("transform", function(d) { 
     return "translate(" + d.y + "," + d.x + ")"; //<-- setting it on the parent using a "translate"
   });

nodeEnter.append("rect")
  .attr("x", function(d) { return d.x ; }) //<-- setting it on the rect using a "x" attribute

改为这样做:

nodeEnter.append("rect")
   .attr("x", 0) //<-- x is taken care of by translate
   .attr("y", -rectSize/2) //<-- just use y to center the rect
   .attr("width", rectSize)
   .attr("height", rectSize);

已更新 fiddle