gpt4 book ai didi

javascript - d3.js - 树布局 - 如何翻转它?

转载 作者:行者123 更新时间:2023-11-29 16:16:02 25 4
gpt4 key购买 nike

我将此示例用于 D3.js 树布局。

http://mbostock.github.com/d3/talk/20111018/tree.html

我需要翻转它,所以根节点在右手边,链接......等等。

我怎样才能做到这一点?

最佳答案

  • 将每个节点的偏移量改为从右边偏移而不是从左边偏移:

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

变成:

    // Normalize for fixed-depth from right.
nodes.forEach(function(d) { d.y = w - (d.depth * 180); });
  • 将标签更改为在相对的两侧

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

变成:

    nodeEnter.append("svg:text")
.attr("x", function(d) { return d.children || d._children ? 10 : -10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "start" : "end"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
  • 将根节点的原始位置放在右侧,而不是左侧,这样第一次转换就不会很奇怪:

    root = json;
    root.x0 = h / 2;
    root.y0 = 0;

变成:

    root = json;
root.x0 = h / 2;
root.y0 = w;

fiddle :http://jsfiddle.net/Ak5tP/1/embedded/result/

关于javascript - d3.js - 树布局 - 如何翻转它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com