Fit Text into SVG Element (Using D3/JS)

前端 未结 5 1338
-上瘾入骨i
-上瘾入骨i 2020-12-06 01:53

I want to write text inside a rectangle I create as follows:

body = d3.select(\'body\')

svg = body.append(\'svg\').attr(\'height\', 600).attr(\'width\', 200         


        
5条回答
  •  旧巷少年郎
    2020-12-06 02:07

    Another approach, when trying to fit a straight line of text into an svg element, could use the strategy found in http://bl.ocks.org/mbostock/1846692:

    node.append("text")
      .text(function(d) { return d.name; })
      .style("font-size", function(d) { return Math.min(2 * d.r, (2 * d.r - 8) / this.getComputedTextLength() * 24) + "px"; })
      .attr("dy", ".35em");
    

提交回复
热议问题