NetiPlot

React Component

<NetiPlotReact> is a React component that renders a network graph on layered HTML5 canvases. It is driven entirely by props and re-renders via useSyncExternalStore.

Minimal example

import { NetiPlotReact } from '@jonmodell/netiplot'
import type { NetiPlotGraph } from '@jonmodell/netiplot'

const graph: NetiPlotGraph = {
  nodes: [
    { id: 'a', label: 'Alpha' },
    { id: 'b', label: 'Beta' },
  ],
  edges: [{ id: 'e1', from: 'a', to: 'b' }],
}

export default function MyGraph() {
  return (
    <div style={{ position: 'relative', width: '100%', height: '600px', overflow: 'hidden' }}>
      <NetiPlotReact graph={graph} />
    </div>
  )
}

Key props

PropTypeRequiredDescription
graphNetiPlotGraphNodes and edges to render
optionsNetiPlotOptionsDisplay and behaviour configuration
shapesNetiPlotShapeDefinition[]Background shapes drawn behind the graph
onMouseNetiPlotMouseHandlerClick, drag, and hover events
layouterNetiPlotLayouterCustom layout algorithm (default: hierarchical)
shouldRunLayouterShouldRunLayouterPredicate controlling when layout re-runs
callbackFn(data: NetiPlotCallbackData) => voidProgrammatic access (fit, camera, positions)
nodeDrawingFunctionNodeDrawingFunctionCustom canvas draw function per node
shapeDrawingFunctionShapeDrawingFunctionCustom canvas draw function per shape
imagesNetiPlotImageMapImage map for node icons
customControls(data) => ReactNode | nullReplace or hide zoom controls
identifierstringStable instance ID
classNamestringCSS class on the container div

See the Props reference for full type details.

Updating the graph

Pass a new graph object to trigger a re-render. The engine diffs the incoming data and only updates changed nodes and edges.

const [graph, setGraph] = useState(initialGraph)

// Add a node
setGraph(prev => ({
  ...prev,
  nodes: [...prev.nodes, { id: 'new', label: 'New node' }],
}))