2

i am trying to have the text not exceed the size of the rect SVG however since it can not be added as a child to rect, i dont know what else i can do, sorry am kind of a newbie.

here is the code am working with.

var node = svg.append("g").selectAll(".node")
          .data(nodes)
        .enter().append("g")
          .attr("class", function(d){ return ["node ", d.name.replace(/ 
          /g,"-")].join(" "); })
          .attr("transform", function(d) {
              return "translate(" + d.x + "," + d.y + ")";
          });

      node.append("rect")
          .attr("height", sankey.nodeWidth())
          .attr("width", function(d) { return d.dy; })
          .style("fill", function(d) { return d.color = color(d.name.replace(/ .*/, "")); })
          .style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
        .append("title")
          .text(function(d) { return d.name + "\n" + format(d.value); });

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

Thank you :)

Mina
  • 75
  • 6
  • 2
    No. This has nothing to do with D3, this is a SVG specification: rect elements cannot be containers. What you have here is what we call the *XY problem*: instead of posting your attempted solution (which won't work), please post your actual problem, which is setting the text width according to the rect width. – Gerardo Furtado May 08 '18 at 23:15
  • @GerardoFurtado is there any kind of work around that you would suggest? – Mina May 08 '18 at 23:17
  • 1
    yes, several. Just edit your question better explaining your problem (not your attempted solution) and a ton of users will shortly post an answer. Actually, this is certainly a duplicate, please search this (using SO search or, even better, Google, which is better than SO search mechanism). – Gerardo Furtado May 08 '18 at 23:19

0 Answers0