How to place node title to the left or right of the node in d3 Sankey graph?

不想你离开。 提交于 2019-12-11 00:27:44

问题


Here is the example I'm referring to: http://bl.ocks.org/d3noob/c9b90689c1438f57d649

The second level nodes have their title to their right. How can I place those to the left of the nodes? Please help.


回答1:


This is how you make text to the nodes.

  node.append("text")
      .attr("x", -6)
      .attr("y", function(d) { return d.dy / 2; })
      .attr("dy", ".35em")
      .attr("text-anchor", "end")
      .attr("transform", null)
      .text(function(d) { return d.name; })
      .filter(function(d) { return d.x < width / 2; })
      .attr("x", 6 + sankey.nodeWidth())
      .attr("text-anchor", "start");

For making the text on the right always do :

  node.append("text")
      .attr("x", -6)
      .attr("y", function(d) { return d.dy / 2; })
      .attr("dy", ".35em")
      .attr("text-anchor", "end")
      .attr("transform", null)
      .text(function(d) { return d.name; })
    //.filter(function(d) { return d.x < width / 2; }) //COMMENT THIS LINE
      .attr("x", 6 + sankey.nodeWidth())
      .attr("text-anchor", "start");

Comment the line shown above so that it does not filter the nodes > half of width SVG.

working code here



来源:https://stackoverflow.com/questions/37960985/how-to-place-node-title-to-the-left-or-right-of-the-node-in-d3-sankey-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!