Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell D3 to make the dendrogram vertical (top down) instead of left to right?

I'm looking at this example that uses cluster layout to assign the X and Y coordinates to nodes on a dendrogram. How can I tell cluster to layout vertically, top down, instead of the default left to right?

like image 222
joshuapoehls Avatar asked Jul 14 '12 01:07

joshuapoehls


1 Answers

For the example you link, just flip the use of the X and Y coordinates. This can be done by changing

var diagonal = d3.svg.diagonal()
  .projection(function(d) { return [d.y, d.x]; });

and

var node = vis.selectAll("g.node")
  .data(nodes)
 .enter().append("g")
   .attr("class", "node")
   .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

to

var diagonal = d3.svg.diagonal()
  // Flip the values here.
  .projection(function(d) { return [d.x, d.y]; });

and

var node = vis.selectAll("g.node")
   .data(nodes)
 .enter().append("g")
  .attr("class", "node")
  // Flip the values here.
  .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })

Here is a JSFiddle showing the above changes in action.

like image 145
Brant Olsen Avatar answered Nov 17 '22 01:11

Brant Olsen