Adding svg text inside svg circle in d3js

前端 未结 1 838
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 09:33

I am trying to add text into a circle made in SVG with d3.js

The text is not appearing inside the circle. When I inspect the element I can see

相关标签:
1条回答
  • 2021-01-26 09:59

    You cannot place the <text> node inside the <circle></circle> node. You need to stack them one after the other. You can add both <circle> and <text> to a <g> element. Try this snippet

    function CreateNode(nodeId,label,className,nodeType)
    {   
      var node = d3.select("svg").append('g');
    
      node.append("circle")
       .attr("cx", CalculateX(nodeType))
       .attr("cy", CalculateY(nodeType))
       .attr("r",40)
       .style("fill","none")
       .style("stroke","#ccc")
       .attr("nodeId",nodeId)
       .attr("class",className);
    
       node.append("text")
        .attr("x", CalculateX(nodeType))             
        .attr("y", CalculateY(nodeType))
        .attr("text-anchor", "middle")  
        .style("font-size", "14px")
        .attr('fill','#cccccc')
        .text(label);
    
        return node;
    
    }
    
    0 讨论(0)
提交回复
热议问题