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