What does binding to Object do in D3.js

本小妞迷上赌 提交于 2020-01-03 18:00:08

问题


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?


回答1:


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; });


来源:https://stackoverflow.com/questions/16555857/what-does-binding-to-object-do-in-d3-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!