4.5 KiB
d3-spring-model
This module implements Chalmers' 1996 Neighbour and Sampling algorithm for drawing the force-directed layouts. It is a linear time algorithm that uses stochastic sampling to find the best neighbours for high-dimensional data and creates the layout in 2 dimensions.
Neighbour and Sampling algorithm is useful for producing visualizations that show relationships between the data. For instance:
Authors
Remigijus Bartasius and Matthew Chalmers
Reference
- Chalmers, Matthew. "A linear iteration time layout algorithm for visualising high-dimensional data." Proceedings of the 7th conference on Visualization'96. IEEE Computer Society Press, 1996.
Usage
Download the latest release and load one of Javascript file alongside D3 4.0.
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="d3-spring-model.min.js"></script>
<script>
var simulation = d3.forceSimulation(nodes);
</script>
API Reference
NeighbourSampling
The Neighbour and Sampling algorithm tries to group the nodes based on the distance between them. If the nodes have a low distance, then the force attracts them to each other. If the nodes have a high distance, then the repulsive force pushes them further apart from each other.
In order for it to work properly, a distance function should be specified.
# d3.forceNeighbourSampling() <>
Initializes the Neighbour and Sampling algorithm with default parameters.
# neighbourSampling.id([id]) <>
If id is specified, sets the node id accessor to the specified function and returns this force. If id is not specified, returns the current node id accessor, which defaults to the numeric node.index:
function id(d) {
return d.index;
}
The id accessor is invoked for each node whenever the force is initialized, as when the nodes change, being passed the node and its zero-based index.
# neighbourSampling.distance([distance]) <>
If distance is specified, sets the distance accessor to the specified number or function, re-evaluates the distance accessor for each link, and returns this force. If distance is not specified, returns the current distance accessor, which defaults to:
function distance() {
return 300;
}
# neighbourSampling.neighbourSize([neighbourSize]) <>
If neighbourSize is specified, sets the neighbour set size to the specified number and returns this force. If neighbourSize is not specified, returns the current value, which defaults to 6.
# neighbourSampling.sampleSize([sampleSize]) <>
If sampleSize is specified, sets the sample set size to the specified number and returns this force. If sampleSize is not specified, returns the current value, which defaults to 3.
# neighbourSampling.stress() <>
Returns the stress of the layout.
# neighbourSampling.velocity() <>
Returns the average velocity of the iteration.