d3 / d3-selection

Transform the DOM by selecting elements and joining to data.

Home Page:https://d3js.org/d3-selection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stash the key used by the previous data-join

mbostock opened this issue · comments

One of the surprising things about the data join is that you have to recompute a new key for the existing elements. It’d be nice if we stashed the key (if one exists) as node.__key__, similar to how we stash data as node.__data__. That way you’d only need to compute the key for the data, not for the existing elements. This key would only be stashed in the cases where you pass a key function to selection.data.

On a similar topic, is there an intentional decision behind selection.data() not accepting an object as a key?

When I did:

var clocks = [{position: {x:0,y:0}, minutes: 0, hours:0},...]
selection.data(clocks, d=>d.position)...

I expected the data object to bind to the position object. It however did not. Instead I had to serialise position to a string manually.

https://observablehq.com/d/b0cbdcac16896daf

@mjmdavis Keys are required to be strings. https://github.com/d3/d3-selection#selection_data

A key function may be specified to control which datum is assigned to which element, replacing the default join-by-index, by computing a string identifier for each datum and element.