Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does binding to Object do in D3.js

I am trying to understand the D3.js code for this example and am confused by this code:

var circle = interpolation.selectAll("circle")
    .data(Object);
circle.enter().append("circle")
    .attr("r", 4)
    .attr("fill","yellow");
circle
    .attr("cx", function y(d) { console.log(d.attr("class")); return d.x; })
    .attr("cy", function(d) { return d.y; });

What does the second line of this code actually do? What data does it bind to?

like image 913
BruceHill Avatar asked May 15 '13 02:05

BruceHill


1 Answers

The data bound in the element above that is given by the function getLevels(d, t), where d is a number of range 2 - 4 and t is a number derived from the current time.

This only ever returns an array of arrays. Because an array is already of type Object, Calling Object() on an Array returns the original array.. Therefore, from what I can see, the author is simply using Object as a kind of identity function, similar to:

var identity = function(d){
  return d;
}

var circle = interpolation.selectAll("circle")
    .data(identity);
circle.enter().append("circle")
    .attr("r", 4)
    .attr("fill","yellow");
circle
    .attr("cx", function y(d) { console.log(d.attr("class")); return d.x; })
    .attr("cy", function(d) { return d.y; });
like image 76
minikomi Avatar answered Oct 31 '22 21:10

minikomi