Wrap text within circle

前端 未结 4 1267
故里飘歌
故里飘歌 2020-12-16 00:59

I\'m using d3 to draw a UML diagram and would like to wrap text within the shapes drawn with d3. I\'ve gotten as far as the code below and can\'t find a solution to make the

4条回答
  •  孤城傲影
    2020-12-16 01:09

    SVG doesn't provide text wrapping, but using foreignObject you can achieve a similar effect. Assuming that radius is the radius of the circle, we can compute the dimensions of a box that will fit inside the circle:

    var side = 2 * radius * Math.cos(Math.PI / 4),
        dx = radius - side / 2;
    
    var g = svg.append('g')
        .attr('transform', 'translate(' + [dx, dx] + ')');
    
    g.append("foreignObject")
        .attr("width", side)
        .attr("height", side)
        .append("xhtml:body")
        .html("Lorem ipsum dolor sit amet, ...");
    

    The group should be displaced a small amount to have the text centered. I know that this is not exactly what is asked, but it can be helpful. I wrote a small fiddle. The result will look like this:

    enter image description here

提交回复
热议问题