javascript - d3.tree => 转换似乎不起作用

标签 javascript firebug d3.js

我使用 d3js 创建了一个树形布局图。节点可单击以切换显示子项。应该将子节点插入到预定义的位置,然后过渡到所需的位置。问题是插入坐标总是关闭的。使用 Firebug 进行调试时,它显示在添加新节点后,其坐标直接为 x = 51.42857142857142 和 y = 200.0,即使行
.attr("transform", "translate(90,100)") 应该改变它们(我在这里使用固定值,以进一步确定问题。

我的错误在哪里?

完整代码:

// Toggle children.
function toggle(d) {
   if (d.children) {
     d._children = d.children;
     d.children = null;
   } else {
     d.children = d._children;
     d._children = null;
  }
} 

function toggleAll(d) {
   if (d.children) {
      d.children.forEach(toggleAll);
      toggle(d);
   }
}

function update(source) {
   // Node movement delay
   var duration = d3.event && 1500;

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

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

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

  // Enter any new nodes at the parent's previous position.
   var nodeEnter = node.enter().append("svg:g")
    .attr("class", "node")
    .attr("transform", "translate(90,100)" )
    .on("click", function(d) { toggle(d); update(d); });

  nodeEnter.append("svg:circle")
    .attr("r", 1e-6);


  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
    .duration(duration)
    .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })

  nodeUpdate.select("circle")
    .attr("r", 4.5)
    .style("fill", function(d) { return d._children ? "red" : "#fff"; });



  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
     .duration(duration)
     .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
    .remove();

 // While moving the nodes, they need to shrink
 nodeExit.select("circle")
   .attr("r", 1e-6);


    // Stash the old positions for transition.
    nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
   });
}


var i = 0, root;
var radius = 960 / 2;
var tree = d3.layout.tree()
   .size([360, radius - 120]);
var diagonal = d3.svg.diagonal.radial()
   .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });

var vis = d3.select("#body").append("svg:svg")
   .attr("width", radius * 2)
   .attr("height", radius * 2 - 50)
   .append("g")
   .attr("transform", "translate(" + radius + "," + radius + ")");

d3.json("flare.json", function(json) {
  root = json;
  root.x0 = 0;
  root.y0 = 0;


  // Initialize the display to show a few nodes.
  root.children.forEach(toggleAll);
  toggle(root.children[1]);

  update(root);
});

最佳答案

您可以通过调整初始transform 来更改插入坐标,即更改

.attr("transform", "translate(90,100)")

例如,如果要在其父节点的位置插入新节点,可以通过将对父元素的引用传递给 update 函数并获取其 转换值:

var sourceNode = d3.select(parentElement);

// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("svg:g")
    .attr("class", "node")
    .attr("transform", parentElement ? sourceNode.attr("transform") : "translate(90,100)")
    .on("click", function (d) {
    toggle(d);
    update(d, this);
});

完整的 jsfiddle here .

关于javascript - d3.tree => 转换似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320786/

相关文章:

javascript - ternJS - 生成 JSON 类型定义文件

javascript - 下拉菜单根据选择显示不正确的图像

javascript - C 风格包含在 Firebug 或 Chrome 控制台中?

javascript - dc.js 热图取消选择颜色

javascript - 改变 D3 动画/过渡的方向?

javascript - 如何将下拉列表转换为在 HTML 和 PHP 中显示搜索匹配结果的输入标记?

javascript - 这是哪个模板lang `<?js`

javascript - 如何找出是什么 JavaScript 函数触发了 HTML 更改?

javascript - 如何通过变量在 JavaScript (Firebug) 中搜索变量?

javascript - 为什么 Internet Explorer 不显示 foreignObject?