Adding svg text inside svg circle in d3js

前端 未结 1 842
伪装坚强ぢ
伪装坚强ぢ 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 node inside the node. You need to stack them one after the other. You can add both and to a 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)
提交回复
热议问题