【问题标题】:d3.js - Tree Layout - How can I flip it?d3.js - 树布局 - 我如何翻转它?
【发布时间】:2013-03-27 23:12:52
【问题描述】:

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

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

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

我怎样才能做到这一点?

【问题讨论】:

标签: javascript json svg d3.js


【解决方案1】:
  • 将每个节点的偏移量更改为从右侧而不是从左侧偏移:

    // 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;

小提琴:http://jsfiddle.net/Ak5tP/1/embedded/result/

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 2013-03-02
    • 2012-09-17
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多