d3 / d3-selection

Transform the DOM by selecting elements and joining to data.
https://d3js.org/d3-selection
ISC License
558 stars 292 forks source link

Enable the use of d3.join() without implicit d3.order() call #299

Closed NeonMika closed 2 years ago

NeonMika commented 2 years ago

I switched one of my applications from the "old" enter()/merge() pattern to use the "new" join() method. I really like how concise data binding becomes using this method. Sadly, only after this move I found out that join() automatically performs order() after the merge of the enter and update selection.

Possible improvement: Since my application relies on preserving the document order, and data array reordering is not really feasable in my case, I would like to ask whether it is possible to add an additional (optional) parameter to the join() method that allows users to specify whether they want to automatically perform order() at the end of the join method. Probably somethink like an options parameter.

An example for this problem can be found here: https://stackoverflow.com/q/71179024/2938364

The problematic line is https://github.com/d3/d3-selection/blob/91245ee124ec4dd491e498ecbdc9679d75332b49/src/selection/join.js#L14

Fil commented 2 years ago

ordering is not an implicit byproduct of selection.join, but an explicit part of the feature as mentioned in the documentation and in the unit test: https://github.com/d3/d3-selection/commit/146af5da2ed1bba5246e1e6a4d930f8c3d0adc5c

Have you considered applying selection.sort after the join()?

NeonMika commented 2 years ago

Yes, I read the documentation (after searching for quite some time why the z-ordering problem occurs - first I assumed some problem with my drag code), and yes, probably I used the word "implicit" wrong in this context.

But I do no think that ordering (in join), sorting, and then ordering (in sort) again is the way how this problem should be tackeled.

I think there are use cases where it is not desired that join calls order at the end, and I just think that it would be nice if it was possible to call join with this setting (either by adding a parameter to join or by providing another method similar to join that does not order the selection in the end).

Otherwise, methods such as raise and lower lose their applicability in data-joined visualization, since they will be "overridden" on the next join call.

mbostock commented 2 years ago

selection.join is a convenience function. If you don’t want to order, you should call the underlying methods directly yourself. We shouldn’t add options to selection.join.