increase force between nodes that are not linked

19 views Asked by At

I have a force-directed graph using d3.js, part of the code is like:

   simulation
        .force("center", d3.forceCenter(width / 2, height / 2))
        .force("nodes", d3.forceManyBody())
        .force(
            "links",
            d3
                .forceLink(links)
                .id(d => d.id)
                .distance(d => 5 * (d.source.size + d.target.size))
   
        )
        .on("tick", ticked);

this line determines the force between linked nodes:

 .distance(d => 5 * (d.source.size + d.target.size))

however, I would like to provide a force between unlinked nodes (ideally, the force would increase as the degree of freedom increases).

How can I accomplish this?

1

There are 1 answers

0
Alexander Mills On

these seem to do the trick:

  .force("charge", d3.forceManyBody().strength(-98))
  .force("collide", d3.forceCollide().radius(50))

how can I change these forces dynamically as the animation progresses?