简体   繁体   中英

d3.js - Tree Layout - How can I flip it?

I'm using this example for a D3.js tree layout.

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

I need to flip it, so the root node is on the right hand side, and the links... etc.

How can I achieve this?

  • Change the offset of each node to be from the right rather than offset from the left:

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

Becomes:

    // Normalize for fixed-depth from right.
    nodes.forEach(function(d) { d.y = w - (d.depth * 180); });
  • Change the labels to be on the opposite sides

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

Becomes:

    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);
  • Make original position of the root node on the right hand side, rather than the left so first transition isn't weird:

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

Becomes:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM