我有一个svg元素rect同样也包含一个文本元素 . 我使用translate函数来显示文本是否在rect元素中 . 我想相应于文本的宽度更改矩形的宽度 .

如何在d3中的svg中实现这一点?任何帮助都非常感谢 .

nodeEnter.filter(function(d: any) { return d.type != 'node--button'})
                  .append("rect")
                  .attr("rx", "15")
                  .attr("ry", "20")
                  .attr("x", "-100")
                  .attr("y", "-20")
                  .attr("width", "200")
                  .attr("height", "40")
                  .style("fill", function(d: any) { return d._children ? "lightsteelblue" : "#fff"; })
                  .on("click", this.click);

nodeEnter.filter(function(d: any) { return d.type != 'node--button'})
                  .append("text")
                  .attr("x", "-60")
                  .attr("y", "-10")
                  .attr("dy", ".35em")
                  .attr("text-anchor", function(d: any) { return d.children || d._children ? "center" : "center"; })
                  .text(function(d: any) { return d.name; })
                  .style("fill-opacity", 1e-6);