gpt4 book ai didi

javascript - 对 D3.js : SVGAnimatedLengthList is not a function error 感到困惑

转载 作者:行者123 更新时间:2023-12-02 15:58:37 25 4
gpt4 key购买 nike

我正在尝试使用 D3.js 制作条形图对象,但我似乎可以让它工作。

我这样定义x-scale:

    var Barplot = function(){
this.x = d3.linear().....

...

然后我尝试使用这样的比例

    this.bar.append("text")
.attr("x", function(d){
return this.x(d) + 5;
}

但是,当我运行此命令时,我收到此错误 ->

    TypeError: SVGAnimatedLengthList is not a function (evaluating 'this.x(d)' ).

我不知道为什么,特别是因为如果我将 this.x 更改为 var x,它就会起作用。

最佳答案

this 指的是两个不同的事物。在属性函数内,this 引用当前 DOM 元素(请参阅 the documentation )。如果您确实需要引用外部 this,请将其保存在变量中(例如 var that = this),然后使用 that

关于javascript - 对 D3.js : SVGAnimatedLengthList is not a function error 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31386763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com