IAMTubby
IAMTubby

Reputation: 1667

Who defines the arguments in this javascript snippet?

I am just starting out with javascript and have a problem understanding this piece of (svg)code below which uses javascript for defining it's x co-ordinate and radius. I understand how data is bound etc. But my question is - For the function which takes two arguments : d and i, where is it defined that the first argument to the function is the dataset and the second is a counter for the circle, ie 0 for the first circle, 1 for the second and so on.

var dataset = [ 5, 10, 15, 20, 25 ];
var circles = svg.selectAll("circle")
                 .data(dataset)
                 .enter()
                 .append("circle");
circles.attr("cx", function(d, i) {
            return (i * 50) + 25;
        })
       .attr("cy", h/2)
       .attr("r", function(d) {
            return d;
       });

Thanks.

Upvotes: 0

Views: 30

Answers (1)

Robert Longson
Robert Longson

Reputation: 124059

This is d3 and so the d3 documentation defines what the function expects

If value is a constant, then all elements are given the same attribute value; otherwise, if value is a function, then the function is evaluated for each selected element (in order), being passed the current datum d and the current index i, with the this context as the current DOM element.

Upvotes: 2

Related Questions