D3树方节点不在所需位置

时间:2015-01-02 00:20:02

标签: d3.js treeview

我想使用像这样http://bl.ocks.org/mbostock/4339083的d3创建树视图, 但我不想在节点中使用圆圈,而是想要正方形。我发现这篇帖子给了我一个线索d3.js: modifyng links in a tree layout,但没有解决我的问题。这是我的小提琴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);

}  

我无法意识到代码中的错误在哪里?

1 个答案:

答案 0 :(得分:2)

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

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