Passing an object function to data () in d3

http://bl.ocks.org/mbostock/1134768

Hey guys, I'm pretty new to JavaScript, and basically study it to use d3 to render data. I am trying to understand what is happening in the code above, in particular in the fragment:

// Add a rect for each date var rect = cause.selectAll("rect") .data(Object) // THIS IS WEIRD TO ME.... .enter().append("svg:rect") .attr("x", function(d) { return x(dx); }) .attr("y", function(d) { return -y(d.y0) - y(dy); }) .attr("height", function(d) { return y(dy); }) .attr("width", x.rangeBand()); 

What does the constructor of an object do in .data() ? I think that data() will force the function to be evaluated, so is the object actually being created? Why insert a rectangle for each element of each array into causes ?

+4
source share
1 answer

See this answer .

It is used as an identification function - what was associated with the choice before remains connected. This pattern is usually needed because you must call .data() to get the choice to upgrade.

Personally, I really don't like messing up my code like this; I would rather do .data(function(d) { return d;}) , as it is obvious what I am doing there. YMMV.

+6
source

Source: https://habr.com/ru/post/1493076/


All Articles