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

New rectangle-shaped force. #1690

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 3 additions & 7 deletions src/graphVisualization/visualization/ForceSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ import {
forceCollide,
forceLink,
forceManyBody,
forceSimulation,
forceX,
forceY
forceSimulation
} from 'd3-force'

import {
DEFAULT_ALPHA,
DEFAULT_ALPHA_MIN,
FORCE_CENTER_X,
FORCE_CENTER_Y,
FORCE_CHARGE,
FORCE_COLLIDE_RADIUS,
FORCE_LINK_DISTANCE,
Expand All @@ -44,6 +40,7 @@ import { GraphModel } from '../models/Graph'
import { NodeModel } from '../models/Node'
import { RelationshipModel } from '../models/Relationship'
import circularLayout from '../utils/circularLayout'
import { rectangleForce } from './rectangleForce'

const oneRelationshipPerPairOfNodes = (graph: GraphModel) =>
Array.from(graph.groupedRelationships()).map(pair => pair.relationships[0])
Expand All @@ -56,8 +53,7 @@ export class ForceSimulation {
this.simulation = forceSimulation<NodeModel, RelationshipModel>()
.velocityDecay(VELOCITY_DECAY)
.force('charge', forceManyBody().strength(FORCE_CHARGE))
.force('centerX', forceX(0).strength(FORCE_CENTER_X))
.force('centerY', forceY(0).strength(FORCE_CENTER_Y))
.force('gravity', rectangleForce())
.on('tick', () => {
this.simulation.tick(TICKS_PER_RENDER)
render()
Expand Down
45 changes: 45 additions & 0 deletions src/graphVisualization/visualization/rectangleForce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const aspectRatio = 3
const gravityStrength = 0.05

const sq = (d: number) => d * d
const integral = (a: number, b: number) =>
(b * Math.log(sq(a) + sq(b))) / 2 + a * Math.atan(b / a)

export const rectangleForce = () => {
let nodes: any

const force = (alpha: number) => {
const targetRatio =
aspectRatio < 0 ? 1 / (1 - aspectRatio) : aspectRatio + 1
const strength = gravityStrength * alpha

for (const node of nodes) {
const actualRatio = Math.abs(node.x / node.y)
const width = actualRatio > targetRatio ? node.x : node.y * targetRatio
const height = actualRatio < targetRatio ? node.y : node.x / targetRatio
const maxX = width - node.x
const minX = -width - node.x
const maxY = height - node.y
const minY = -height - node.y

const vx =
integral(maxX, maxY) -
integral(maxX, minY) -
integral(minX, maxY) +
integral(minX, minY)
const vy =
integral(maxY, maxX) -
integral(minY, maxX) -
integral(maxY, minX) +
integral(minY, minX)
node.x += vx * strength
node.y += vy * strength
}
}

force.initialize = (_: any) => {
nodes = _
}

return force
}