Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to control the simulation speed of d3 force layout

I am following a d3 force laytout example like this.

I want to control the speed of the dots flying to the cluster. In other words, I want some dots take more time to get to their final positions, while some dots take less time.

I tried to add a timer function to control the time of each tick, but it did not work.

this.force = d3.layout.force()
.on("tick", setTimeout(tick(d), 50));

I need help for this.

like image 921
user3145427 Avatar asked Dec 30 '13 06:12

user3145427


People also ask

What is force simulation in D3?

# d3.forceSimulation([nodes]) · Source. Creates a new simulation with the specified array of nodes and no forces. If nodes is not specified, it defaults to the empty array. The simulator starts automatically; use simulation. on to listen for tick events as the simulation runs.

What is D3 force layout?

D3's force layout uses a physics based simulator for positioning visual elements. Forces can be set up between elements, for example: all elements can be configured to repel one another. elements can be attracted to center(s) of gravity. linked elements can be set a fixed distance apart (e.g. for network visualisation)

What is D3 physics?

Encora | September 15, 2021. Mapping the interaction between particles is a common problem in Physics, but it can be hard to visualize . D3 is a tool that can help solve this issue by simulating particles in a simple way.


1 Answers

Don't set a timer to call the tick function, this is done automatically by the force layout.

There are however a number of parameters you can set to modify the behaviour of the force layout. The ones most relevant to what you're trying to do are the following.

  1. .friction() corresponds to how quickly the velocity decays and therefore directly controls how fast nodes move. The default is 0.9, to make everything slower, set it to a lower value.
  2. .charge() controls how strong the attraction/repulsion between nodes is. This doesn't control the velocity directly, but affects it.

Of these parameters, only the latter can be set on a node-by-node basis. This makes achieving what you want a bit tricky, as you would have to carefully balance the forces. As a start, setting the charge of the nodes that you want to move slower closer to 0 should help.

There are a few other parameters of the force layout that I think would not be useful in your particular case (I'm thinking of .linkStrength() and .linkDistance()), but you may want to have a look nevertheless.

like image 126
Lars Kotthoff Avatar answered Sep 25 '22 16:09

Lars Kotthoff