Last active
December 24, 2015 02:49
-
-
Save tomgullo/6732980 to your computer and use it in GitHub Desktop.
snippet to word wrap text in d3js treemap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
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) ); }); | |
} | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, thank you. Exactly what I was looking for!