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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With