Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Last active December 24, 2015 02:49
Show Gist options
  • Save tomgullo/6732980 to your computer and use it in GitHub Desktop.
Save tomgullo/6732980 to your computer and use it in GitHub Desktop.
snippet to word wrap text in d3js treemap
....
text = cell.append("text")
.attr("x", function(d) { return d.dx / 2; })
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style("opacity", function(d) { d.w = this.getComputedTextLength(); return d.dx > d.w ? 1 : 0; })
.text(function(d) { return d.name });
var temp_text = text.text();
text.text('');
var words = temp_text.split(' ');
for (var i = 0; i < words.length; i++) {
if (i == 0) {
text.append('tspan').text(function(d) { return d.name.split(' ')[i]; } ).attr('x', function(d) { return (d.dx)/2; });
} else {
text.append('tspan').text(function(d) {
return d.name.split(' ')[i]; }
).attr('x', function(d) { return (d.dx ) / 2; }).attr('y', function(d) { return (d.dy/2 + (i * 25) ); });
}
}
...
@macliems
Copy link

macliems commented Jan 2, 2015

Ah, thank you. Exactly what I was looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment