Skip to content

Quick start

Install the package. The Dagre layouts have one optional peer dependency:

Terminal window
npm install ego-graph
# Only if you use radialDagre or sectoredDagre:
npm install @dagrejs/dagre

The root is your point of view. Width and height let the layout leave enough room for each node.

import { polarPetal } from 'ego-graph';
const positions = polarPetal({
root: 'customer-42',
nodes: [
{ id: 'customer-42', width: 180, height: 56 },
{ id: 'invoice-1', width: 160, height: 56 },
{ id: 'invoice-2', width: 160, height: 56 },
],
edges: [
{ source: 'customer-42', target: 'invoice-1' },
{ source: 'customer-42', target: 'invoice-2' },
],
});
// Map<string, { x: number; y: number }>, keyed by node id.

Positions are top-left coordinates, so they can be assigned directly to most canvas and node renderers. The root is centred at the origin; translate the complete result to fit your viewport.

The documentation repository keeps its canonical samples under examples/, imports their text into MDX with ?raw, and runs them in bun test. The TypeScript configuration resolves their ego-graph imports to the adjacent package source (../ego-graph/src), so examples validate against the checkout instead of a copied API definition.

When product examples are added to the package repository, add its examples/ folder to the same test command and have the corresponding MDX page import the file with ?raw. That makes a single file the source for both the displayed snippet and its verification.