wordwrap = (width) ->this.each() ->text = d3.selectthiswords = text.text().split(/\s+/).reverse()line = []line_height = text.node().getBBox().height# can get the actual font height, but on chrome it seems like getBBox().height# is more accurate#line_height = parseInt window.getComputedStyle(this).fontSize, 10tspan = text.textnull.append"tspan"whilewords.length>0word = words.pop()line.pushwordtspan.textline.join" "iftspan.node().getComputedTextLength()>=widthline.pop()tspan.textline.join" "line = [word]tspan = text.append"tspan".attr"dy",line_height+"px".attr"dx",-tspan.node().getComputedTextLength()+"px".textwordd3.selection.prototype.wordwrap = wordwrap
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.