getBBox() returns 0 for append svg in memory element instead of append in body.

3,127 views
Skip to first unread message

Mohammad Faizal

unread,
Jun 21, 2013, 10:39:16 AM6/21/13
to d3...@googlegroups.com
var w = 240;
var h = 50;
var span =document.createElement("span");
var svg = d3.select(span).append("svg").attr("width",w).attr("height",h);
var text = svg.append("text").attr("x",w-25).attr("y",h-20)
           .text(dataset[1] + " ( " + dataset[2] + "% " + ")")
           .style("fill",text_color)
           .attr("text-anchor","end")
           .attr("font-size",font_size)
           .attr("font-family",font_family);

        var bbox = text.node().getBBox();
        console.log('bbox');
        console.log(bbox);


It returns 0 for bbox object.

Ian Johnson

unread,
Jun 21, 2013, 1:22:13 PM6/21/13
to d3...@googlegroups.com
that is expected, it hasn't been put in the DOM yet so no calculations have been done for it. If you want to measure it you can either append it somewhere out of sight, or just append it where it needs to go and then adjust other things based on the measurements once they are there.


--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Ian Johnson - 周彦

roland

unread,
Jun 23, 2013, 7:14:56 AM6/23/13
to d3...@googlegroups.com
It's exactly as Ian/enjalot says. All you need to is:

var span_element = document.createElement("span");
document.body.appendChild(span_element);

I've put amended code in a small jsfiddle. See: 

You might also want to look at: http://bl.ocks.org/mbostock/1160929.
R

Paul Katsen

unread,
Jun 5, 2014, 4:46:56 AM6/5/14
to d3...@googlegroups.com
any way to compute without actually appending to dom?

Victor Powell

unread,
Jun 5, 2014, 12:25:34 PM6/5/14
to d3...@googlegroups.com
you could append it, run your calculates, then immediately remove it. because JS runs in the UI thread, the user will never see the element in the DOM.
Reply all
Reply to author
Forward
0 new messages