# d3-neighbour-sampling

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:

![fsmvis data set](https://raw.githubusercontent.com/sReeper/d3-neighbour-sampling/master/img/chalmers-fsmvis.PNG)![Poker Hands data set](https://raw.githubusercontent.com/sReeper/d3-neighbour-sampling/master/img/chalmers-poker.PNG)

### Authors
Remigijus Bartasius and Matthew Chalmers

### Reference
- Chalmers, Matthew. ["A linear iteration time layout algorithm for visualising high-dimensional data."](http://dl.acm.org/citation.cfm?id=245035) Proceedings of the 7th conference on Visualization'96. IEEE Computer Society Press, 1996.

## Installing

Download the [latest release](https://github.com/sReeper/d3-neighbour-sampling/releases/latest).

## 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.

<a name="forceNeighbourSampling" href="#forceNeighbourSampling">#</a> d3.<b>forceNeighbourSampling</b>() [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js "Source")

Initializes the Neighbour and Sampling algorithm with default parameters.

<a name="neighbourSampling_id" href="#neighbourSampling_id">#</a> <i>neighbourSampling</i>.<b>id</b>([<i>id</i>]) [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L218 "Source")

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:

```js
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.

<a name="neighbourSampling_distance" href="#neighbourSampling_distance">#</a> <i>neighbourSampling</i>.<b>distance</b>([<i>distance</i>]) [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L230 "Source")

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:

```js
function distance() {
  return 300;
}
```

<a name="neighbourSampling_neighbourSize" href="#neighbourSampling_neighbourSize">#</a> <i>neighbourSampling</i>.<b>neighbourSize</b>([<i>neighbourSize</i>]) [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L222 "Source")

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.

<a name="neighbourSampling_sampleSize" href="#neighbourSampling_sampleSize">#</a> <i>neighbourSampling</i>.<b>sampleSize</b>([<i>sampleSize</i>]) [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L226 "Source")

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.

<a name="neighbourSampling_stress" href="#neighbourSampling_stress">#</a> <i>neighbourSampling</i>.<b>stress</b>() [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L234 "Source")

Returns the stress of the layout.

<a name="neighbourSampling_velocity" href="#neighbourSampling_velocity">#</a> <i>neighbourSampling</i>.<b>velocity</b>() [<>](https://github.com/sReeper/d3-neighbour-sampling/blob/master/src/neighbourSampling.js#L238 "Source")

Returns the average velocity of the iteration.