d3 / d3-force

Force-directed graph layout using velocity Verlet integration.
https://d3js.org/d3-force
ISC License
1.82k stars 377 forks source link

d3 v7 chained methods of forceLink "is not a fucntion" #215

Closed staysh closed 1 year ago

staysh commented 1 year ago

d3.forceLink().id().strength() results in an uncaught TypeError that it is not a function

d3.forceLink().id() //ok d3.forceLink().strength() //ok

Fil commented 1 year ago

works as expected; see https://github.com/d3/d3-force#link_id for details

mbostock commented 1 year ago

I think the confusion here is that you can only chain when you pass link.id an argument. If you donโ€™t pass an argument, then link.id is a getter, returning the current id. So:

const link = d3.forceLink();
console.log(link.id()); // returns the current id accessor
link.id((d) => d.id); // sets the id accessor and returns link

So, this is fine:

link.id((d) => d.id).strength(10); // ๐Ÿ‘

But this is nonsensical because link.id() without an argument returns the current id accessor, not the link force.

link.id().strength(); // ๐Ÿ‘Ž