读取d3文本元素的宽度

Bre*_*ett 33 d3.js

我想<text>在创建后读取在d3中创建的元素的宽度.

我试过了

svg.selectAll("text")
    .each(function () {
        console.log('text', d3.select(this).style("width"));
    }); // 'auto'
Run Code Online (Sandbox Code Playgroud)

svg.selectAll("text")
    .each(function () {
        console.log('text', $(this).css("width"));
    }); // '0px'
Run Code Online (Sandbox Code Playgroud)

提前致谢

Rac*_*len 56

在选择方法(如attr)中:

   this.getComputedTextLength(). 
Run Code Online (Sandbox Code Playgroud)

您可以说,在选择一个元素时

   selection.node().getComputedTextLength(). 
Run Code Online (Sandbox Code Playgroud)

您还可以使用getBBox作为宽度和高度