@ouestware/graphology-layout-utils
v1.1.0
Published
Shared types and helpers to work with graphology layouts
Readme
OuestWare's Graphology Experiments - Layout utils
Shared types and helpers to work with graphology layouts.
It defines the LayoutMapping type returned by the various layout packages in this repository (a mapping of each node
to its coordinates), and provides applyLayout to write such a mapping back onto a graph's node attributes.
Usage
import Graph from "graphology";
import { type LayoutMapping, applyLayout } from "@ouestware/graphology-layout-utils";
const graph = new Graph();
// ...populate the graph...
// A LayoutMapping maps each node to at least { x, y }:
const layout: LayoutMapping = {
a: { x: 0, y: 0 },
b: { x: 1, y: 1 },
};
// Write x / y back as node attributes:
applyLayout(graph, layout);Layouts may carry extra fields beyond x / y, and applyLayout can rename any field to a different node attribute:
const layout: LayoutMapping<{ x: number; y: number; width: number; height: number }> = {
a: { x: 0, y: 0, width: 10, height: 20 },
};
// x / y are written as-is, while "width" / "height" are stored as "w" / "h":
applyLayout(graph, layout, { width: "w", height: "h" });Notes
LayoutMapping<T>is generic over the coordinates type, which must extend{ x: number; y: number }.applyLayoutusesmergeNodeAttributes, so it only overwrites the mapped fields and leaves other node attributes untouched.- Fields left out of the
renamemapping are written under their own key.
