Quick start
Install the package. Dagre is an optional peer — install it only if a layout can reach it:
npm install ego-graph
# If you use dagre, dagreSubtree, or sectorThreshold:npm install @dagrejs/dagrecustomLayout from ego-graph is Dagre-free. Import from ego-graph/dagre when a spec uses Dagre.
Make the graph explicit
Section titled “Make the graph explicit”The root is your point of view. Width and height let the layout leave enough room for each node.
import { customLayout } from 'ego-graph';
const positions = customLayout( { 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' }, ], }, { firstLayer: { mode: 'radial' }, nextLayers: { mode: 'radial' }, },);
// 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.
Compose two layer specs when the first ring should not match the rest of the tree:
import { customLayout } from 'ego-graph';
const positions = customLayout(graph, { firstLayer: { mode: 'radial' }, nextLayers: { mode: 'cloud', threshold: 8 },});Keep samples executable
Section titled “Keep samples executable”The documentation repository keeps its canonical samples under examples/, imports their text into MDX with ?raw, and runs them with bun run test. TypeScript paths resolve ego-graph to the adjacent package source (../ego-graph/src, or packages/ego-graph/src in this workspace), 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.