@graspologic/graph
Provides a set of graph data structures, used with the layout engine and the renderer.
Usage
Below are several common usage patterns for this library.
Dynamically adding nodes to your graph
Standalone
import { NodeImpl } from '@graspologic/graph'
import { WebGLGraphRenderer } from '@graspologic/renderer'
const renderer = WebGLGraphRenderer.createInstance()
const node = new NodeImpl()
node.position = [10, 10, 0]
node.radius = 10
renderer.graph.add(node)
React
import React, { useEffect, useRef } from 'react'
import { NodeImpl } from '@graspologic/graph'
import { GraphView } from '@graspologic/react'
import { GraphRenderer } from '@graspologic/renderer'
export const MyRenderer: React.FC = () => {
const graphRef = useRef<GraphRenderer>(null)
useEffect(() => {
if (graphRef.current) {
const node = new NodeImpl()
node.position = [10, 10, 0]
node.radius = 10
graphRef.current!.graph.add(node)
}
}, [graphRef])
return (
<GraphView ref={graphRef} data={{ nodes: [], edges: [] }}/>
)
}
Dynamically adding edges to your graph
Standalone
import { EdgeImpl } from '@graspologic/graph'
import { WebGLGraphRenderer } from '@graspologic/renderer'
const renderer = WebGLGraphRenderer.createInstance()
const edge = new EdgeImpl()
edge.sourcePosition = [0, 0, 0]
edge.targetPosition = [10, 10, 0]
renderer.graph.add(edge)
React
import React, { useEffect, useRef } from 'react'
import { EdgeImpl } from '@graspologic/graph'
import { GraphView, Nodes, Edges } from '@graspologic/react'
import { GraphRenderer } from '@graspologic/renderer'
export const MyRenderer: React.FC = () => {
const graphRef = useRef<GraphRenderer>(null)
useEffect(() => {
if (graphRef.current) {
const edge = new EdgeImpl()
edge.sourcePosition = [0, 0, 0]
edge.targetPosition = [10, 10, 0]
graphRef.current!.graph.add(edge)
}
}, [graphRef])
return (
<GraphView ref={graphRef} data={{ nodes: [], edges: [] }}>
<Nodes />
<Edges />
</GraphView>
)
}
Dynamically animating the edges of your graph
Standalone
import { EdgeImpl } from '@graspologic/graph'
import { WebGLGraphRenderer } from '@graspologic/renderer'
const renderer = WebGLGraphRenderer.createInstance()
const edge = new EdgeImpl()
edge.sourcePosition = [0, 0, 0]
edge.targetPosition = [10, 10, 0]
renderer.graph.add(edge)
edge.animateSourcePosition([100, 100, 0], 1000)
React
import React, { useEffect, useRef } from 'react'
import { EdgeImpl } from '@graspologic/graph'
import { GraphView } from '@graspologic/react'
import { GraphRenderer } from '@graspologic/renderer'
export const MyRenderer: React.FC = () => {
const graphRef = useRef<GraphRenderer>(null)
useEffect(() => {
if (graphRef.current) {
const edge = new EdgeImpl()
edge.sourcePosition = [0, 0, 0]
edge.targetPosition = [10, 10, 0]
graphRef.current!.graph.add(edge)
edge.animateSourcePosition([100, 100, 0], 1000)
}
}, [graphRef])
return (
<GraphView ref={graphRef} data={{ nodes: [], edges: [] }}>
<Nodes />
<Edges />
</GraphView>
)
}
Dynamically animating the nodes of your graph
Standalone
import { NodeImpl } from '@graspologic/graph'
import { WebGLGraphRenderer } from '@graspologic/renderer'
const renderer = WebGLGraphRenderer.createInstance()
const node = new NodeImpl()
node.position = [10, 10, 0]
node.radius = 10
renderer.graph.add(node)
node.animatePosition([100, 100, 0], 1000)
React
import React, { useEffect, useRef } from 'react'
import { NodeImpl } from '@graspologic/graph'
import { GraphView } from '@graspologic/react'
import { GraphRenderer } from '@graspologic/renderer'
export const MyRenderer: React.FC = () => {
const graphRef = useRef < GraphRenderer > null
useEffect(() => {
if (graphRef.current) {
const node = new NodeImpl()
node.position = [10, 10, 0]
node.radius = 10
renderer.graph.add(node)
node.animatePosition([100, 100, 0], 1000)
}
}, [graphRef])
return (
<GraphView ref={graphRef} data={{ nodes: [], edges: [] }}>
<Nodes />
<Edges />
</GraphView>
)
}
See the API documentation or examples for additional examples.