UNPKG

d3-dag

Version:

Layout algorithms for visualizing directed acylic graphs.

97 lines (96 loc) 2.99 kB
/** * A topological layout using {@link Zherebko}. * * @packageDocumentation */ import { Graph, Rank } from "../graph"; import { LayoutResult, NodeSize } from "../layout"; import { Tweak } from "../tweaks"; import { U } from "../utils"; /** all operators for the zherebko layout */ export interface ZherebkoOps<in N = never, in L = never> { /** the operator for assigning nodes a rank */ rank: Rank<N, L>; /** node size operator */ nodeSize: NodeSize<N, L>; /** tweaks */ tweaks: readonly Tweak<N, L>[]; } /** * a simple topological layout operator. * * This layout algorithm constructs a topological representation of the graph * meant for visualization. The algorithm is based off a PR by D. Zherebko. The * nodes are topologically ordered, and edges are then positioned into "lanes" * to the left and right of the nodes. * * Create with {@link zherebko}. */ export interface Zherebko<Ops extends ZherebkoOps = ZherebkoOps> { /** * layout the graph using the current operator */ (graph: Ops extends ZherebkoOps<infer N, infer L> ? Graph<N, L> : never): LayoutResult; /** * set the {@link Rank} operator for the topological ordering */ rank<NewRank extends Rank>(val: NewRank): Zherebko<U<Ops, "rank", NewRank>>; /** get the current lane operator */ rank(): Ops["rank"]; /** * set the {@link Tweak}s to apply after layout */ tweaks<const NewTweaks extends readonly Tweak[]>(val: NewTweaks): Zherebko<U<Ops, "tweaks", NewTweaks>>; /** * get the current {@link Tweak}s. */ tweaks(): Ops["tweaks"]; /** * sets the {@link NodeSize} * * (default: `[1, 1]`) */ nodeSize<NewNodeSize extends NodeSize>(val: NewNodeSize): Zherebko<U<Ops, "nodeSize", NewNodeSize>>; /** get the current node size */ nodeSize(): Ops["nodeSize"]; /** * set the gap size between nodes * * (default: `[1, 1]`) */ gap(val: readonly [number, number]): Zherebko<Ops>; /** get the current gap size */ gap(): readonly [number, number]; } /** the default zherebko operator */ export type DefaultZherebko = Zherebko<{ /** no specified ranks */ rank: Rank<unknown, unknown>; /** default node size */ nodeSize: readonly [1, 1]; /** no tweaks */ tweaks: readonly []; }>; /** * create a new {@link Zherebko} with default settings * * This layout creates a simple topological layout. It doesn't support behavior * beyond the layout defaults of {@link Zherebko#rank}, * {@link Zherebko#nodeSize}, {@link Zherebko#gap}, and * {@link Zherebko#tweaks}. * * <img alt="zherebko example" src="media://zherebko.png" width="1000"> * * @example * * ```ts * const graph: Graph = ... * const layout = zherebko(); * const { width, height } = layout(graph); * for (const node of graph.nodes()) { * console.log(node.x, node.y); * } * ``` * */ export declare function zherebko(...args: never[]): DefaultZherebko;