Wrap text within circle

前端 未结 4 1275
故里飘歌
故里飘歌 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:10

    It's not ideal, but @Pablo.Navarro's answer led me to the following.

    var svg =  d3.select('#svg')
      .append('svg')
        .attr('width', 500)
        .attr('height', 200);
    
    var radius = 60,
        x      = 150,
        y      = 100,
        side   = 2 * radius * Math.cos(Math.PI / 4),
        dx     = radius - side / 2;
    
    var global = svg.append('g')
      .attr('transform', 'translate(' + [ dx, dx ] + ')');
    
    global.append('circle')
      .attr('cx', x)
      .attr('cy', y)
      .attr('r', radius);
    
    global.append('foreignObject')
      .attr('x', x - (side/2))
      .attr('y', y - (side/2))
      .attr('width', side)
      .attr('height', side)
      .attr('color', 'red')
      .append('xhtml:p')
        .text('Text meant to fit within circle')
        .attr('style', 'text-align:center;padding:2px;margin:2px;');
    

    Result

    result

提交回复
热议问题