Javascript .call()函数将函数作为参数而不是结果传递

Javascript .call()函数将函数作为参数而不是结果传递,javascript,d3.js,bubble-chart,Javascript,D3.js,Bubble Chart,我在D3中看到了一些我没有预料到的行为,我不知道如何避免它。使用此代码块: 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.

我在D3中看到了一些我没有预料到的行为,我不知道如何避免它。使用此代码块:

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)
;
以下是
wrap()
函数:

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);
            }
        }
    });
}
function wrap(text, widthFunc) {
  // ...
  text.each(function(d) {
    var width = widthFunc(d);
    // ...
  });
}
我想将气泡图的圆半径传递给wrap函数,但在
width
参数中得到的是函数本身,而不是解析的
d.r
。 有没有办法让这个匿名函数在传递到
.call()
之前解析为一个值

如果
wrap
返回一个数字(或一个表示数字的字符串,因为d3总是+值),它将正常工作。

您可以使用
.each()
而不是
.call()

或者,您可以更改
wrap()
的定义以应用该函数:

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);
            }
        }
    });
}
function wrap(text, widthFunc) {
  // ...
  text.each(function(d) {
    var width = widthFunc(d);
    // ...
  });
}

.call
用于更改函数的
,是否确定不只是要调用?@PaulS。这里的调用可以是来自对象的简单函数,而不是本机调用函数
wrap()
是MBostock的文本换行函数(请参见上面的编辑)
text
参数的值是多少?如果您能提供fiddletry示例,那就太好了。请看:第一个参数是selection,第二个参数是second,所以您作为第二个参数传递它不会调用functino。很抱歉,我不清楚。
.attr(“radius”…
行按预期工作。是
call()
函数将函数传递到
width
参数,而不是结果值。这样做了!
each()
就是要调用的函数。我从使用的
call()中提取此函数的示例)
但我猜这是因为宽度是恒定的。谢谢!函数的
wrap()
并不打算用函数调用。我倾向于后一种方法:修复函数,以便可以按照您想要的方式调用它。