I'm trying to wrap text by building up a text string, and using getComputedTextLength to find out when the text goes beyond the allowed width. However, I can't find a simple way to incrementally build up the text which will work with getComputedTextLength.
The general idea is:
str = svgDocument.createTextNode(myText[word]); // first word on new line
word++;
obj = text.cloneNode(true); // new text element for this line
obj.appendChild(str);
svgDocument.documentElement.appendChild(obj); // reqd for getComputedTextLength?
for( ; word < myText.length; word++) {
next_width = obj.getComputedTextLength(); // get current line width
if(next_width >= extent)
break;
str += " "; // add next word to the line
str += myText[word];
...
}
Can anyone tell me how to get this to work? Presumably str is copied rather than referenced in obj, but I've also tried putting obj.removeChild(str) and obj.appendChild(str) in the loop, but the appendChild crashes. I've also tried various combinations of moving around the documentElement.appendChild, and removing obj and re-appending it, and so on.
a wrapper function for overflowing text:
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (width - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
usage:
text.append('tspan').text(function(d) { return d.name; }).each(wrap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With