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

the "resize" helper triggers twice when the returned chart depends on a generator/input #1563

Closed
ccattuto opened this issue Aug 9, 2024 · 5 comments · Fixed by #1564
Closed
Labels
bug Something isn’t working

Comments

@ccattuto
Copy link

ccattuto commented Aug 9, 2024

Consider the example below, where activityChart() returns a Plot.plot() object, and attRange is a generator/input. On instrumenting activityChart() and triggering one change of attRange it is observed that activityChart() gets called twice. This looks like an issue for cases when the plot is heavy to compute.

<div class="card">
  ${resize((width) => activityChart(data, attRange, {width}))}
</div>
@ccattuto ccattuto added the bug Something isn’t working label Aug 9, 2024
@mbostock
Copy link
Member

mbostock commented Aug 9, 2024

Yep, the problem is rendering the chart changes the height of the container, which then triggers the ResizeObserver. This is the expected behavior when you pass a render function that takes both width and height as two arguments, but we can optimize this to ignore changes in height when render only takes a single width argument.

@ccattuto
Copy link
Author

ccattuto commented Aug 9, 2024

thanks for the clarification!

@ccattuto
Copy link
Author

ccattuto commented Aug 9, 2024

PS @mbostock : in cases where we know that both width and height will not change as a consequence of changes in attRange, is there anything better one could do? I have an issue with a D3 graph layout inside an SVG element. I want the graph layout to be recomputed only when the element changes in size, not when a parameter that does not affect layout (case in point, attRange, changes). As it is now, a change in attRange will result in the whole graph layout needing to be recomputed, as the contained SVG element gets destroyed. Should I simply avoid using the resize helped in these more complex cases?

@mbostock
Copy link
Member

mbostock commented Aug 9, 2024

@ccattuto This would be better as a discussion question rather than tacking on to this (no longer related) issue. But it sounds like you want to avoid reacting to attRange changes. In that case you’d need to use mutable state rather than a generator to avoid reacting. But doing that may greatly increase the complexity of your code as you are now responsible for re-rendering the graph manually rather than as a pure function of state.

@ccattuto
Copy link
Author

ccattuto commented Aug 9, 2024

Thanks. I'll move this to a discussion, as it may be of interest to others.
#1567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn’t working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants