d33a
d33a

Reputation: 740

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

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.

Upvotes: 2

Views: 1369

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

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

Upvotes: 1

Related Questions