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
Consider a .forces()
method that gets/sets the forces map
#43
Comments
Hmm. Well, simulation.forces would need to return a copy, and make a defensive copy, of the map to prevent the map from being modified without force.initialize being called. And using simulation.forces to set new forces is potentially slower than simulation.force if it causes some of existing forces to be re-initialized… Also it exposes the fact that the simulation uses d3.map under the hood, which I’m not wild about. A more conservative approach might be to just add a simulation.forces getter method which takes no arguments and returns an array of {key, value} objects representing the bound forces. It could be implemented as: forces: function() {
return forces.entries();
}, That seems like the most conservative approach as far as enabling inspection goes. The downside is that it’s not as easy to make bulk changes to the registered forces. |
@mbostock Thanks for the reply! <ForceLayout
nodes={[...]}
forces={{
link: forceLink(...),
center: forceCenter(...),
}}
/> and the component internally takes both nodes and forces and creates the simulation. (this is why I'd need the getter, to be able to know which of the forces passed in now are new.) const sim = forceSimulation()
.nodes([...])
.force('link', forceLink())
.force('center', forceCenter())
<ForceLayout
simulation={sim}
/> do you have any opinion either way? |
Closing due to inactivity. Note that another possibility is to create
then call simulation.forces will then hold the map of forces (see https://observablehq.com/d/d999cd71c09c86ef). |
While building a react component that renders a force layout chart I'm finding that my work would be easier if there was a method on the simulation that allowed me to get all forces on the simulation, so that I can compare to props passed to the component when the props change. It would also be useful to be able to set all the forces simultaneously, but the hard one to do outside of
d3-force
is getter, because without there's no way of knowing what forces are currently assigned to the simulation.Would you consider adding such a method?
I'd be happy to send a PR if yes.
Thanks either way,
Nuno
The text was updated successfully, but these errors were encountered: