Yucheng Lin
Yucheng Lin

Reputation: 11

Confused about D3.js : SVGAnimatedLengthList is not a function error

I am trying to make a bar plot object using D3.js but I can seem to get it to work.

I defined the x-scale like this:

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

    ...

And then I try to use the scale like this

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

However when I run this I get this error ->

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

and i have no idea why especially since it will work if I change this.x to var x.

Upvotes: 0

Views: 129

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

this refers to two different things. Inside the attribute function, this refers to the current DOM element (see the documentation). If you really need to refer to the outer this, save it in a variable (e.g. var that = this) and then use that.

Upvotes: 2

Related Questions