1

I want to make the links between nodes vary depending on the "distance" value I put in the links. This is surprisingly difficult - I've searched around and found no satisfying solution.

This is what I have.

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { 
        return d.id;
    }).distance(function(d) { 
        return d.distance;
    }).strength(1))
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(width / 2, height / 2));

Setting the distance value to fixed variable (e.g. distance(100)) works, but not if I use function.

Here is the complete snippet: http://plnkr.co/edit/xAFGFpIBIRxdyb1Y?open=lib%2Fscript.js

deathlock
  • 2,756
  • 5
  • 27
  • 48
  • Link distance can certainly be set by a function, however, constraints of geometry may mean that distances are not as specified, likewise, competing forces may result in lengths that are different than specified. Does [this](https://stackoverflow.com/q/38253560/7106086) help? – Andrew Reid Aug 24 '20 at 16:40
  • did you manage to find a solution to your own question? I am currently having the same issue. – blah Jan 27 '21 at 09:43

0 Answers0