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

Adjust force directed 2d graph spacing #184

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/layout/forceDirected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { forceRadial, DagMode } from './forceUtils';
import { LayoutFactoryProps, LayoutStrategy } from './types';
import { buildNodeEdges } from './layoutUtils';
import { forceInABox } from './forceInABox';
import { FORCE_LAYOUTS } from './layoutProvider';

export interface ForceDirectedLayoutInputs extends LayoutFactoryProps {
/**
Expand Down Expand Up @@ -77,6 +78,11 @@ export interface ForceDirectedLayoutInputs extends LayoutFactoryProps {
* Used to compute the template force nodes size (Force template only)
*/
forceCharge?: number;

/**
* Used to determine the simulation forceX and forceY values
*/
forceLayout: (typeof FORCE_LAYOUTS)[number];
}

const TICK_COUNT = 100;
Expand All @@ -97,7 +103,8 @@ export function forceDirected({
forceCharge = -700,
getNodePosition,
drags,
clusterAttribute
clusterAttribute,
forceLayout
}: ForceDirectedLayoutInputs): LayoutStrategy {
const { nodes, edges } = buildNodeEdges(graph);

Expand All @@ -106,12 +113,22 @@ export function forceDirected({
const nodeStrengthAdjustment =
is2d && edges.length > 25 ? nodeStrength * 2 : nodeStrength;

let forceX;
let forceY;
if (forceLayout === 'forceDirected2d') {
forceX = d3ForceX();
forceY = d3ForceY();
} else {
forceX = d3ForceX(1200 / 2).strength(0.05);
forceY = d3ForceY(1200 / 2).strength(0.05);
}

// Create the simulation
const sim = d3ForceSimulation()
.force('link', d3ForceLink())
.force('charge', d3ForceManyBody().strength(nodeStrengthAdjustment))
.force('x', d3ForceX(1200 / 2).strength(0.05))
.force('y', d3ForceY(1200 / 2).strength(0.05))
.force('x', forceX)
.force('y', forceY)
.force('z', d3ForceZ())
// Handles nodes not overlapping each other ( most relevant in clustering )
.force(
Expand Down
23 changes: 15 additions & 8 deletions src/layout/layoutProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type LayoutOverrides = Partial<
| HierarchicalLayoutInputs
>;

const FORCE_LAYOUTS = [
export const FORCE_LAYOUTS = [
'forceDirected2d',
'treeTd2d',
'treeLr2d',
Expand All @@ -37,7 +37,8 @@ export function layoutProvider({
dimensions: 2,
nodeLevelRatio: nodeLevelRatio || 2,
nodeStrength: nodeStrength || -250,
linkDistance
linkDistance,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'treeTd2d') {
return forceDirected({
Expand All @@ -46,7 +47,8 @@ export function layoutProvider({
dimensions: 2,
nodeLevelRatio: nodeLevelRatio || 5,
nodeStrength: nodeStrength || -250,
linkDistance: linkDistance || 50
linkDistance: linkDistance || 50,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'treeLr2d') {
return forceDirected({
Expand All @@ -55,7 +57,8 @@ export function layoutProvider({
dimensions: 2,
nodeLevelRatio: nodeLevelRatio || 5,
nodeStrength: nodeStrength || -250,
linkDistance: linkDistance || 50
linkDistance: linkDistance || 50,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'radialOut2d') {
return forceDirected({
Expand All @@ -64,7 +67,8 @@ export function layoutProvider({
dimensions: 2,
nodeLevelRatio: nodeLevelRatio || 5,
nodeStrength: nodeStrength || -500,
linkDistance: linkDistance || 100
linkDistance: linkDistance || 100,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'treeTd3d') {
return forceDirected({
Expand All @@ -82,7 +86,8 @@ export function layoutProvider({
dimensions: 3,
nodeLevelRatio: nodeLevelRatio || 2,
nodeStrength: nodeStrength || -500,
linkDistance: linkDistance || 50
linkDistance: linkDistance || 50,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'radialOut3d') {
return forceDirected({
Expand All @@ -91,15 +96,17 @@ export function layoutProvider({
dimensions: 3,
nodeLevelRatio: nodeLevelRatio || 2,
nodeStrength: nodeStrength || -500,
linkDistance: linkDistance || 100
linkDistance: linkDistance || 100,
forceLayout: type
} as ForceDirectedLayoutInputs);
} else if (type === 'forceDirected3d') {
return forceDirected({
...rest,
dimensions: 3,
nodeLevelRatio: nodeLevelRatio || 2,
nodeStrength: nodeStrength || -250,
linkDistance
linkDistance,
forceLayout: type
} as ForceDirectedLayoutInputs);
}
} else if (type === 'circular2d') {
Expand Down
Loading