Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svg getComputedTextLength function always returning zero

Tags:

svg

d3.js

Hello I tried to integrate this d3 zoomable treemap from

http://mbostock.github.io/d3/talk/20111018/treemap.html

but the getComputedTextLength() always returns 0. I also tried getBBox(), but it returns an object with all properties with zero.

    .text(function(d) { return d.name; })
    .style("opacity", function(d) {
         d.w = this.getComputedTextLength(); 
         return d.dx > d.w ? 1 : 0;
    });

Do I need to put my text wrapped in a tspan?
What other reasons could generate this?

like image 590
dotmindlabs Avatar asked Jun 12 '13 19:06

dotmindlabs


1 Answers

I realize that this is over a year late, but I ran into the same issue recently and this may help someone. this.getComputedTextLength() returns 0 if the text element hasn't been rendered yet or if the container or any of its parent containers have display set to none at the time the text element is being rendered.

like image 52
OrionMelt Avatar answered Oct 22 '22 18:10

OrionMelt