Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read width of d3 text element

Tags:

d3.js

I would like to read the width of <text> elements, created in d3, after they're created.

I have tried

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

and

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

Thanks in advance

like image 352
Brett Avatar asked Jan 28 '13 19:01

Brett


1 Answers

In a selection method (such as attr) :

   this.getComputedTextLength(). 

In a selection of one element, you can say

   selection.node().getComputedTextLength(). 

You can also use getBBox for the width and height

like image 93
Rachel Gallen Avatar answered Nov 18 '22 01:11

Rachel Gallen