Referencing this example from Mike Bostock, I would like to apply the same idea to a bar chart where the X and Y are switched. So the labels run up and down the Y axis.
In this fiddle: http://jsfiddle.net/2eLW6/ I simply copy the wrap function from Mike's example and apply it to my chart.
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
In Mike's example applying this to an x axis, words are only put on new lines if a certain line-length is exceeded. In my fiddle it placed every single word on a new line. Also, I wonder if there is a way to move the y attribute up a little bit if a new line has been created so that it still centers?
Can anyone help me translate that wrap() function for a y axis?
The wrap() function is defined on line 203, and called from line 135 in that fiddle.