Reset zoom to initial auto based on # of nodes behavior
Is your feature request related to a problem? Please describe. I am trying to reuse a graph after clearing and loading different data. I was unable to find a way to restore the initial zoom behavior before reloading data in the case a user had started zooming.
Describe the solution you'd like A method to reset the zoom or a special value to give to the existing zoom setter.
Describe alternatives you've considered
No way to get at the lastSetZoom state so I was unable to make this work without making changes to the library itself.
Additional context I have a working patch but wanted to see if there is any interest before cleaning it up.
@infime thanks for reaching out.
You currently have two methods to programmatically change the zoom:
- .zoomToFit()
- .zoom(
)
Possibly the .zoomToFit(<ms>, <padding>) will satisfy your use case.
@vasturiano Thanks for the quick response. I saw and attempted using both of those methods but they do not achieve my use case of restoring the initial zoom behavior. I am streaming nodes incrementally to the graph so trying to choose the correct time to explicitly set the zoom isn't ideal.
@infime ok, you can then reproduce that zoom level calculation on your side, if you'd like to return to the exact initial settings.
The default zoom level is calculated here: https://github.com/vasturiano/force-graph/blob/master/src/force-graph.js#L440
ZOOM2NODES_FACTOR equals 4, so you can just do:
.zoom(4 / Math.cbrt(numNodes))