Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selection.appendAll #301

Open
mbostock opened this issue Mar 10, 2022 · 3 comments
Open

selection.appendAll #301

mbostock opened this issue Mar 10, 2022 · 3 comments

Comments

@mbostock
Copy link
Member

What if this

svg.append("g")
  .appendAll("rect", data)

were shorthand for this

svg.append("g")
  .selectAll()
  .data(data)
  .enter()
  .append("rect")

The latter is a common special case of the data-join, and it seems worthwhile to support it explicitly? Some optimizations:

  • You don’t need a selector (pass undefined to selection.selectAll, which is already optimized)
  • You don’t need a key function (because the initial selection is necessarily empty)
  • You only need the enter selection (because the initial selection is necessarily empty)
  • You don’t need to call selection.order (because the initial selection is necessarily empty)
@Fil
Copy link
Member

Fil commented Mar 10, 2022

Besides optimization, the benefit (as I understand it) would be a more straightforward expression of intent in that quite common case; a drawback might be that this new method doesn't allow the user to move from "create" to the "update" pattern if that's what they need.

@curran
Copy link
Contributor

curran commented Mar 10, 2022

this new method doesn't allow the user to move from "create" to the "update" pattern if that's what they need.

That's a great point. Migrating code that is designed to be run only once to code that can be run multiple times is one of the biggest pain points of D3 (in my experience at least, this is what students especially struggle with the most).

Maybe the "update" pattern version of the shorthand could look something like this:

one(svg, "g")
  .joinAll("rect", data)

shorthand for

svg
  .selectAll("g")
  .data([null])
  .join("g")
  .selectAll()
  .data(data)
  .join("rect")
@curran curran mentioned this issue Mar 10, 2022
@mbostock
Copy link
Member Author

Yes, that’s historically why D3 hasn’t included a special API for this case and encourages you to use selection.join instead. But I think it’s totally reasonable to special-case the enter selection, as people already do when they chain enter-append instead of using selection.join.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants