I'm seeing some behavior in D3 that I'm not expecting, and I don't know how to get around it. With this block of code:
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function (d) { return d.FSname; })
.attr("radius", function (d) { return d.r;})
.call(wrap, function(d) {return d.r;})
//.call(wrap, 140)
;
Here is the wrap()
function:
function wrap(text, width) {
//reflows text to be within a pixel width
console.log("hit wrap(",text,width,this,")");
text.each(function () {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.0, // 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) {
if (line.length > 1) 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);
}
}
});
}
I want to pass the radius of the circle for a bubble chart into the wrap function, but what I get in the width
argument is the function itself, and not the resolved d.r
.
Is there a way of getting this anonymous function to resolve to a value before passing it into the .call()
?