Force graph layout initialisation


I am a little bit lost in the way to initialize a graph layout.
I have forked the Sticky Force Layout / D3 | Observable
and computed my own version.

The problem is that the behaviour is not the same at the initialization and when I rerun the cell where the graph layout is produced.
The visibility of labels of the connected nodes are not set properly when a click event is used on a node. And the coAuthors variable is empty.
Once rerun the cell with the svg.nodes(), the behaviour is correct (labels of connected nodes are visible), the coAuthors array set correctly.

I must also admit that the
yield svg.nodes();is not clear.

Another point when you load the notebook for the first time is that the variable coAuthors is an empy array whereas it should set properly.
That what is heppening when I run the cell.

So problem of initilization.

Let me know what I have missed.

I have captured what is shown when I first load the notebook, then what happens when I run the cell that defines the variable coAuthors.
Once run, the array is no more empty (what I expect). Why it is empty when I load the notebook the first time ?
This is what I do not understand.

At first load:

After having run the cell:

Do I have to used a promise to delay the evaluation of the cell ?

Any lights welcomed.

I don’t quite understand the first issue you are facing.

But after playing with your notebook a bit, the second issue where you don’t see new coAuthors until rerunning cell manually, is because the cell depends on json.links, which doesn’t get updated – the objects inside the array does, but not the array itself.

Without diving deeper, I think you potentially can fix this by:

  1. at where you update objects in json.links, return a new array for json.links, instead of mutating/pushing/poping objects from the array itself. Like instead of json.links.push(<new obj>), do json.links = [...json.links, <new obj>]

  2. at a variable that mutates with your change to objects in json.links, and then include that variable in the coAuthors cell, too, such that the object change triggers the coAuthors cell change, too.

I was also able to get the coAuthors cell updating at page load by extracting links to its own variable.

So it does look a object/array reference updating + cell dependency issue.

1 Like

Many thanks.
Indeed an object/array reference updating + cell dependency issue.

Using a mutable on links as you have proposed solve the initialization issue.

Not for the opacity on labels.

Here is what I get at first click on a node:


Then after having run the cell with the graph.


Same issue as with the links ?

I think I have solved the issue by using the mutable variable inside the cell that defines the graph.

1 Like