Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link distance force #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ForceGraph.md
Expand Up @@ -35,6 +35,7 @@ An object of options to pass into d3-force.
* *boolean* animate
* *number* height
* *number* width
* *function|number* distance (see [d3-force distance docs](https://github.com/d3/d3-force#link_distance))
* *object* strength (see strengths in the [d3-force documentation](https://github.com/d3/d3-force#collide_strength))
* *function|number* strength.charge
* *function|number* strength.collide
Expand Down
5 changes: 5 additions & 0 deletions src/utils/d3-force.js
Expand Up @@ -136,6 +136,7 @@ function applyLinkForce(simulation, {
data: { nodes, links },
linkAttrs = [],
nodeAttrs = [],
distance,
}) {
// setup the link force if it isn't already set up
if (!simulation.force('link')) {
Expand All @@ -158,6 +159,9 @@ function applyLinkForce(simulation, {
const newLinksSet = new Set(links.map(linkId));
if (!setsEqual(prevLinksSet, newLinksSet)) {
simulation.shouldRun = true;
if (distance) {
simulation.force('link').distance(distance);
}
simulation.force('link').links(
pick(links, 'source', 'target', 'value', ...linkAttrs)
);
Expand Down Expand Up @@ -245,6 +249,7 @@ export function createSimulation(options) {
* @param {object} options.data
* @param {array} options.data.nodes
* @param {array} options.data.links
* @param {function|number} [options.distance]
* @param {object} [options.strength]
* @param {function|number} [options.strength.charge]
* @param {function|number} [options.strength.collide]
Expand Down