prrecomputing the Length of text

774 views
Skip to first unread message

SHEFALI

unread,
May 30, 2012, 3:26:10 AM5/30/12
to d3-js
Hello all,
can any one suggest how to precompute the size of text and accordingly
draw an ellipse around it.


Following is the snippet from the code
new_g.append("svg:ellipse")
.attr("cx", bbox.x+25)
.attr("cy", bbox.y+13)
.call(force.drag)
.attr("rx",function(d) {var ttx=d.screen_name ; return
(ttx.length + 40)})
.attr("ry",13)
.style("fill-opacity", ".2")
.style("stroke", "#666")
.style("stroke-width", "1.5px")
.style("fill", function color(d) { if (d.expanded=="true")
return "red"; else return "none"});

The problem encountered here is that for long text, it does not fit
within the ellipse.please help

Ian Johnson

unread,
May 30, 2012, 10:55:36 AM5/30/12
to d3...@googlegroups.com
Hi Shefali,

There are two methods you can use to get the length of some text, my coworker Nils has prepared an example for understanding fonts in SVG (using d3) and you can see both of them here:


One of the methods is getComputedTextLength and the other is getBBox, both are functions you can call on a DOM node like:

var width = text.node().getBBox().width;

--
Ian Johnson

Mike Bostock

unread,
May 30, 2012, 11:30:34 AM5/30/12
to d3...@googlegroups.com
Here's another example of getComputedTextLength():

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

Mike
Reply all
Reply to author
Forward
0 new messages