Rendering, layout, and data updates

Know when to update the graph, read node positions, or respond to data replacement.

When can I continue?

TaskWhat to do
Update the graph after its first renderWait for await graph.render(), then change data or states.
Read positions after running a layoutWait for await graph.layout(), then call getNodePosition().
Update the application after each data replacementListen for datachange to receive the new node and edge counts.
await graph.render();

graph.on('datachange', ({ nodeCount, edgeCount }) => {
  console.log('Data replaced', nodeCount, edgeCount);
});
await graph.setData(nextData);

await graph.layout();
const position = graph.getNodePosition('A');

To receive a notification for the first render, register graph.once('afterrender', handler) before calling render(). The layout:end and afterlayout events fire before positions are written back. Await graph.layout() before reading final positions.

Observe the sequence

The log shows the initial layout and render events when the example loads. Use the buttons in the top-right corner to see which events each operation triggers.

ButtonWhat to look for
Update A, Add D, Remove Dafterupdateitem, afteradditem, and afterremoveitem, respectively. Add D before trying to remove it.
Replace dataLayout events followed by datachange, since this graph has a layout configured.
LayoutLayout events.
Loading example…
Update A, add/remove D, replace data, or run Layout. The log keeps the last six lines in occurrence order; reset restores initial render events
const events = [
  'beforelayout', 'layout:start', 'layout:end', 'afterlayout',
  'beforerender', 'afterrender', 'datachange',
  'afteradditem', 'afterremoveitem', 'afterupdateitem',
] as const;
const cleanups: Array<() => void> = [];
const watch = <K extends keyof GraphEventMap>(name: K) => {
  const handler = () => push(name);
  graph.on(name, handler);
  cleanups.push(() => graph.off(name, handler));
};
for (const event of events) watch(event); // Before render: capture initial events.

See the event API reference for event order, callback arguments, and cancellation behavior. See the graph lifecycle for cleanup on unmount.