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

后端 未结 1 1997
眼角桃花
眼角桃花 2021-02-04 13:46

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

1条回答
  •  有刺的猬
    2021-02-04 14:39

    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.

    0 讨论(0)
提交回复
热议问题