最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

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?

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?

Share Improve this question edited Jul 22, 2012 at 0:01 Brant Olsen 5,6645 gold badges39 silver badges53 bronze badges asked Jul 14, 2012 at 1:12 joshuapoehlsjoshuapoehls 35.4k11 gold badges53 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far