d3.js - D3 树方形节点不在所需位置

标签 d3.js treeview

我想使用 d3 创建一个 TreeView ,如下所示 http://bl.ocks.org/mbostock/4339083 , 但我想要的不是节点中的圆圈,而是正方形。我发现这篇文章给了我一个线索d3.js: modifyng links in a tree layout但没有解决我的问题。这是我的 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 .

关于d3.js - D3 树方形节点不在所需位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735995/

相关文章:

javascript - 在 D3.JS 中创建元素 block 的最佳方法是什么

python - _tkinter.TclError : Item 1 already exists with tkinter treeview

c# - 已处理的 RoutedEvent 继续冒泡树

jquery - 我如何使用 jquery 检查 ASP.NET TreeView 中的叶节点的开/关

javascript - 顺序图节点 d3js

d3.js - 在 D3 中绘制半径

javascript - 如何在 D3.js 中放大多边形

javascript - 使用 D3.js 进行数据绑定(bind)

Javascript 检查/取消选中 TreeView 的所有按钮

c# - TreeView 可用性差