What is a transcript in d3 for an identification function ("function (d) {return d;}")?

Looking through d3 documents, I see that this code (identity function) repeats everywhere:

function(d) { return d; } 

Is there a built-in way in d3 for this? I know that I can create my own no-op authentication function and use it everywhere, but it seems d3 should provide this.

+4
source share
2 answers

I used to see Mike make .data (Object), which seems to work

http://tributary.io/inlet/5842519

but I'm not sure why I no longer see him

 var svg = d3.select("svg") var data = [[10,20],[30,40]]; svg.selectAll("g") .data(data) .enter() .append("g") .attr("transform", function(d,i) { return "translate(" + [i * 100, 0] + ")"}) .selectAll("circle") //.data(function(d) { console.log(d); return d }) .data(Object) .enter() .append("circle") .attr({ cx: function(d,i) { return 100 + i * 40 }, cy: 100, r: function(d,i) { return d } }) 
+3
source

I was wondering why the d3.identity function d3.identity not part of the library, and could not find a reason not to have it.

In terms of performance, defining an identity function provides better performance than reusing the Object constructor. This is not a big deal if you reuse the same authentication function for different types. The following are performance tests .

So, in my case, I abuse D3 and add the function myself:

 d3.identity = function(d) { return d; } 

If you use underscore , you can also use _. identity .

Regarding the use of the Object constructor, I believe that this creates a new, unnecessary object every time it calls up waste memory and processor time, both for creating and for garbage collection. This can be optimized for immutable types, such as numbers at some time intervals.

EDIT Phrogz has a short article that shows a useful shortcut for reducing the amount of lambda when working with D3, which includes an identification function.

+2
source

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


All Articles