joshuapoehls
joshuapoehls

Reputation: 35217

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?

Upvotes: 9

Views: 4903

Answers (1)

Brant Olsen
Brant Olsen

Reputation: 5664

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.

Upvotes: 12

Related Questions