self sorting nodes in d3 fdg IV
Force Directed Graph with self sorting nodes
The nodes arrange themselves by size, with the larger nodes migrating toward the center.
Features
- Metrics display and inputs
The metrics panel across the top of the svg element gives a live display of the layout state. The inputs on the left allow for the number of nodes and the force gravity and the friction to be adjusted live. The current alpha
value for the layout is displayed along with instantaneous and averaged tick time and the average calculation rate of the layout. Changing any of the inputs re-starts the layout.
- Accelerated annealing
The annealing calc is done every tick but, until alpha drops below 0.05, the viz is only updated every nth tick (n is currently 4). This delivers significant reductions in the time to reach equilibrium (roughly a factor of 2).
- Force dynamics
The force dynamics are a function of alpha, with two phases. The initial phase has zero charge, low gravity and low damping. This is designed to maximise mixing and sorting. The second phase has a higher gravity and a large, negative charge and much higher damping, this is designed to clean up and stabilise the presentation of the nodes.
- Controlling shape
If the nodes are all created on [0,0], then a circular shape results but it tends to be asymmetrical, with most of the smaller nodes thrown to one side. This is due to extreme sensitivity to the distance from the center of the gravity field.
By creating the nodes on two points (randomly biased between the two) not only is the shape controlled but the efficiency of the sorting is markedly improved.
- Collisions between nodes
Based on this example but enhanced to sort the radial position of the nodes based on size, with larger nodes closer to the center. Every collision is used as an opportunity to correct the relative positions. If they are out of position then the radial ordinates of the colliding nodes (in polar coordinates) are swapped. The sorting efficiency is therefore reliant on good mixing in the collisions. In order to maximise the mixing, the nodes are all created at the same point in the center of the graph. When the nodes are swapped, the velocity of the bigger node is preserved while the smaller node is accelerated. Thus, the sorting efficiency is enhanced because the smaller nodes are flung out from the collision point. The mass is calculated assuming the nodes are spheres, using r3, and the rebounds calculated according to relative "mass".
d3 features used
- d3.layout.force
- Ordinal Scales
- d3.format
- d3.range
- d3.geom.quadtree