UNPKG

@dagrejs/dagre

Version:

Graph layout for JavaScript

1,784 lines (1,537 loc) 116 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.dagre = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ /* Copyright (c) 2012-2014 Chris Pettitt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ module.exports = { graphlib: require("@dagrejs/graphlib"), layout: require("./lib/layout"), debug: require("./lib/debug"), util: { time: require("./lib/util").time, notime: require("./lib/util").notime }, version: require("./lib/version") }; },{"./lib/debug":6,"./lib/layout":8,"./lib/util":27,"./lib/version":28,"@dagrejs/graphlib":29}],2:[function(require,module,exports){ "use strict"; let greedyFAS = require("./greedy-fas"); let uniqueId = require("./util").uniqueId; module.exports = { run: run, undo: undo }; function run(g) { let fas = (g.graph().acyclicer === "greedy" ? greedyFAS(g, weightFn(g)) : dfsFAS(g)); fas.forEach(e => { let label = g.edge(e); g.removeEdge(e); label.forwardName = e.name; label.reversed = true; g.setEdge(e.w, e.v, label, uniqueId("rev")); }); function weightFn(g) { return e => { return g.edge(e).weight; }; } } function dfsFAS(g) { let fas = []; let stack = {}; let visited = {}; function dfs(v) { if (Object.hasOwn(visited, v)) { return; } visited[v] = true; stack[v] = true; g.outEdges(v).forEach(e => { if (Object.hasOwn(stack, e.w)) { fas.push(e); } else { dfs(e.w); } }); delete stack[v]; } g.nodes().forEach(dfs); return fas; } function undo(g) { g.edges().forEach(e => { let label = g.edge(e); if (label.reversed) { g.removeEdge(e); let forwardName = label.forwardName; delete label.reversed; delete label.forwardName; g.setEdge(e.w, e.v, label, forwardName); } }); } },{"./greedy-fas":7,"./util":27}],3:[function(require,module,exports){ let util = require("./util"); module.exports = addBorderSegments; function addBorderSegments(g) { function dfs(v) { let children = g.children(v); let node = g.node(v); if (children.length) { children.forEach(dfs); } if (Object.hasOwn(node, "minRank")) { node.borderLeft = []; node.borderRight = []; for (let rank = node.minRank, maxRank = node.maxRank + 1; rank < maxRank; ++rank) { addBorderNode(g, "borderLeft", "_bl", v, node, rank); addBorderNode(g, "borderRight", "_br", v, node, rank); } } } g.children().forEach(dfs); } function addBorderNode(g, prop, prefix, sg, sgNode, rank) { let label = { width: 0, height: 0, rank: rank, borderType: prop }; let prev = sgNode[prop][rank - 1]; let curr = util.addDummyNode(g, "border", label, prefix); sgNode[prop][rank] = curr; g.setParent(curr, sg); if (prev) { g.setEdge(prev, curr, { weight: 1 }); } } },{"./util":27}],4:[function(require,module,exports){ "use strict"; module.exports = { adjust: adjust, undo: undo }; function adjust(g) { let rankDir = g.graph().rankdir.toLowerCase(); if (rankDir === "lr" || rankDir === "rl") { swapWidthHeight(g); } } function undo(g) { let rankDir = g.graph().rankdir.toLowerCase(); if (rankDir === "bt" || rankDir === "rl") { reverseY(g); } if (rankDir === "lr" || rankDir === "rl") { swapXY(g); swapWidthHeight(g); } } function swapWidthHeight(g) { g.nodes().forEach(v => swapWidthHeightOne(g.node(v))); g.edges().forEach(e => swapWidthHeightOne(g.edge(e))); } function swapWidthHeightOne(attrs) { let w = attrs.width; attrs.width = attrs.height; attrs.height = w; } function reverseY(g) { g.nodes().forEach(v => reverseYOne(g.node(v))); g.edges().forEach(e => { let edge = g.edge(e); edge.points.forEach(reverseYOne); if (Object.hasOwn(edge, "y")) { reverseYOne(edge); } }); } function reverseYOne(attrs) { attrs.y = -attrs.y; } function swapXY(g) { g.nodes().forEach(v => swapXYOne(g.node(v))); g.edges().forEach(e => { let edge = g.edge(e); edge.points.forEach(swapXYOne); if (Object.hasOwn(edge, "x")) { swapXYOne(edge); } }); } function swapXYOne(attrs) { let x = attrs.x; attrs.x = attrs.y; attrs.y = x; } },{}],5:[function(require,module,exports){ /* * Simple doubly linked list implementation derived from Cormen, et al., * "Introduction to Algorithms". */ class List { constructor() { let sentinel = {}; sentinel._next = sentinel._prev = sentinel; this._sentinel = sentinel; } dequeue() { let sentinel = this._sentinel; let entry = sentinel._prev; if (entry !== sentinel) { unlink(entry); return entry; } } enqueue(entry) { let sentinel = this._sentinel; if (entry._prev && entry._next) { unlink(entry); } entry._next = sentinel._next; sentinel._next._prev = entry; sentinel._next = entry; entry._prev = sentinel; } toString() { let strs = []; let sentinel = this._sentinel; let curr = sentinel._prev; while (curr !== sentinel) { strs.push(JSON.stringify(curr, filterOutLinks)); curr = curr._prev; } return "[" + strs.join(", ") + "]"; } } function unlink(entry) { entry._prev._next = entry._next; entry._next._prev = entry._prev; delete entry._next; delete entry._prev; } function filterOutLinks(k, v) { if (k !== "_next" && k !== "_prev") { return v; } } module.exports = List; },{}],6:[function(require,module,exports){ let util = require("./util"); let Graph = require("@dagrejs/graphlib").Graph; module.exports = { debugOrdering: debugOrdering }; /* istanbul ignore next */ function debugOrdering(g) { let layerMatrix = util.buildLayerMatrix(g); let h = new Graph({ compound: true, multigraph: true }).setGraph({}); g.nodes().forEach(v => { h.setNode(v, { label: v }); h.setParent(v, "layer" + g.node(v).rank); }); g.edges().forEach(e => h.setEdge(e.v, e.w, {}, e.name)); layerMatrix.forEach((layer, i) => { let layerV = "layer" + i; h.setNode(layerV, { rank: "same" }); layer.reduce((u, v) => { h.setEdge(u, v, { style: "invis" }); return v; }); }); return h; } },{"./util":27,"@dagrejs/graphlib":29}],7:[function(require,module,exports){ let Graph = require("@dagrejs/graphlib").Graph; let List = require("./data/list"); /* * A greedy heuristic for finding a feedback arc set for a graph. A feedback * arc set is a set of edges that can be removed to make a graph acyclic. * The algorithm comes from: P. Eades, X. Lin, and W. F. Smyth, "A fast and * effective heuristic for the feedback arc set problem." This implementation * adjusts that from the paper to allow for weighted edges. */ module.exports = greedyFAS; let DEFAULT_WEIGHT_FN = () => 1; function greedyFAS(g, weightFn) { if (g.nodeCount() <= 1) { return []; } let state = buildState(g, weightFn || DEFAULT_WEIGHT_FN); let results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx); // Expand multi-edges return results.flatMap(e => g.outEdges(e.v, e.w)); } function doGreedyFAS(g, buckets, zeroIdx) { let results = []; let sources = buckets[buckets.length - 1]; let sinks = buckets[0]; let entry; while (g.nodeCount()) { while ((entry = sinks.dequeue())) { removeNode(g, buckets, zeroIdx, entry); } while ((entry = sources.dequeue())) { removeNode(g, buckets, zeroIdx, entry); } if (g.nodeCount()) { for (let i = buckets.length - 2; i > 0; --i) { entry = buckets[i].dequeue(); if (entry) { results = results.concat(removeNode(g, buckets, zeroIdx, entry, true)); break; } } } } return results; } function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) { let results = collectPredecessors ? [] : undefined; g.inEdges(entry.v).forEach(edge => { let weight = g.edge(edge); let uEntry = g.node(edge.v); if (collectPredecessors) { results.push({ v: edge.v, w: edge.w }); } uEntry.out -= weight; assignBucket(buckets, zeroIdx, uEntry); }); g.outEdges(entry.v).forEach(edge => { let weight = g.edge(edge); let w = edge.w; let wEntry = g.node(w); wEntry["in"] -= weight; assignBucket(buckets, zeroIdx, wEntry); }); g.removeNode(entry.v); return results; } function buildState(g, weightFn) { let fasGraph = new Graph(); let maxIn = 0; let maxOut = 0; g.nodes().forEach(v => { fasGraph.setNode(v, { v: v, "in": 0, out: 0 }); }); // Aggregate weights on nodes, but also sum the weights across multi-edges // into a single edge for the fasGraph. g.edges().forEach(e => { let prevWeight = fasGraph.edge(e.v, e.w) || 0; let weight = weightFn(e); let edgeWeight = prevWeight + weight; fasGraph.setEdge(e.v, e.w, edgeWeight); maxOut = Math.max(maxOut, fasGraph.node(e.v).out += weight); maxIn = Math.max(maxIn, fasGraph.node(e.w)["in"] += weight); }); let buckets = range(maxOut + maxIn + 3).map(() => new List()); let zeroIdx = maxIn + 1; fasGraph.nodes().forEach(v => { assignBucket(buckets, zeroIdx, fasGraph.node(v)); }); return { graph: fasGraph, buckets: buckets, zeroIdx: zeroIdx }; } function assignBucket(buckets, zeroIdx, entry) { if (!entry.out) { buckets[0].enqueue(entry); } else if (!entry["in"]) { buckets[buckets.length - 1].enqueue(entry); } else { buckets[entry.out - entry["in"] + zeroIdx].enqueue(entry); } } function range(limit) { const range = []; for (let i = 0; i < limit; i++) { range.push(i); } return range; } },{"./data/list":5,"@dagrejs/graphlib":29}],8:[function(require,module,exports){ "use strict"; let acyclic = require("./acyclic"); let normalize = require("./normalize"); let rank = require("./rank"); let normalizeRanks = require("./util").normalizeRanks; let parentDummyChains = require("./parent-dummy-chains"); let removeEmptyRanks = require("./util").removeEmptyRanks; let nestingGraph = require("./nesting-graph"); let addBorderSegments = require("./add-border-segments"); let coordinateSystem = require("./coordinate-system"); let order = require("./order"); let position = require("./position"); let util = require("./util"); let Graph = require("@dagrejs/graphlib").Graph; module.exports = layout; function layout(g, opts) { let time = opts && opts.debugTiming ? util.time : util.notime; time("layout", () => { let layoutGraph = time(" buildLayoutGraph", () => buildLayoutGraph(g)); time(" runLayout", () => runLayout(layoutGraph, time, opts)); time(" updateInputGraph", () => updateInputGraph(g, layoutGraph)); }); } function runLayout(g, time, opts) { time(" makeSpaceForEdgeLabels", () => makeSpaceForEdgeLabels(g)); time(" removeSelfEdges", () => removeSelfEdges(g)); time(" acyclic", () => acyclic.run(g)); time(" nestingGraph.run", () => nestingGraph.run(g)); time(" rank", () => rank(util.asNonCompoundGraph(g))); time(" injectEdgeLabelProxies", () => injectEdgeLabelProxies(g)); time(" removeEmptyRanks", () => removeEmptyRanks(g)); time(" nestingGraph.cleanup", () => nestingGraph.cleanup(g)); time(" normalizeRanks", () => normalizeRanks(g)); time(" assignRankMinMax", () => assignRankMinMax(g)); time(" removeEdgeLabelProxies", () => removeEdgeLabelProxies(g)); time(" normalize.run", () => normalize.run(g)); time(" parentDummyChains", () => parentDummyChains(g)); time(" addBorderSegments", () => addBorderSegments(g)); time(" order", () => order(g, opts)); time(" insertSelfEdges", () => insertSelfEdges(g)); time(" adjustCoordinateSystem", () => coordinateSystem.adjust(g)); time(" position", () => position(g)); time(" positionSelfEdges", () => positionSelfEdges(g)); time(" removeBorderNodes", () => removeBorderNodes(g)); time(" normalize.undo", () => normalize.undo(g)); time(" fixupEdgeLabelCoords", () => fixupEdgeLabelCoords(g)); time(" undoCoordinateSystem", () => coordinateSystem.undo(g)); time(" translateGraph", () => translateGraph(g)); time(" assignNodeIntersects", () => assignNodeIntersects(g)); time(" reversePoints", () => reversePointsForReversedEdges(g)); time(" acyclic.undo", () => acyclic.undo(g)); } /* * Copies final layout information from the layout graph back to the input * graph. This process only copies whitelisted attributes from the layout graph * to the input graph, so it serves as a good place to determine what * attributes can influence layout. */ function updateInputGraph(inputGraph, layoutGraph) { inputGraph.nodes().forEach(v => { let inputLabel = inputGraph.node(v); let layoutLabel = layoutGraph.node(v); if (inputLabel) { inputLabel.x = layoutLabel.x; inputLabel.y = layoutLabel.y; inputLabel.rank = layoutLabel.rank; if (layoutGraph.children(v).length) { inputLabel.width = layoutLabel.width; inputLabel.height = layoutLabel.height; } } }); inputGraph.edges().forEach(e => { let inputLabel = inputGraph.edge(e); let layoutLabel = layoutGraph.edge(e); inputLabel.points = layoutLabel.points; if (Object.hasOwn(layoutLabel, "x")) { inputLabel.x = layoutLabel.x; inputLabel.y = layoutLabel.y; } }); inputGraph.graph().width = layoutGraph.graph().width; inputGraph.graph().height = layoutGraph.graph().height; } let graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"]; let graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" }; let graphAttrs = ["acyclicer", "ranker", "rankdir", "align"]; let nodeNumAttrs = ["width", "height"]; let nodeDefaults = { width: 0, height: 0 }; let edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"]; let edgeDefaults = { minlen: 1, weight: 1, width: 0, height: 0, labeloffset: 10, labelpos: "r" }; let edgeAttrs = ["labelpos"]; /* * Constructs a new graph from the input graph, which can be used for layout. * This process copies only whitelisted attributes from the input graph to the * layout graph. Thus this function serves as a good place to determine what * attributes can influence layout. */ function buildLayoutGraph(inputGraph) { let g = new Graph({ multigraph: true, compound: true }); let graph = canonicalize(inputGraph.graph()); g.setGraph(Object.assign({}, graphDefaults, selectNumberAttrs(graph, graphNumAttrs), util.pick(graph, graphAttrs))); inputGraph.nodes().forEach(v => { let node = canonicalize(inputGraph.node(v)); const newNode = selectNumberAttrs(node, nodeNumAttrs); Object.keys(nodeDefaults).forEach(k => { if (newNode[k] === undefined) { newNode[k] = nodeDefaults[k]; } }); g.setNode(v, newNode); g.setParent(v, inputGraph.parent(v)); }); inputGraph.edges().forEach(e => { let edge = canonicalize(inputGraph.edge(e)); g.setEdge(e, Object.assign({}, edgeDefaults, selectNumberAttrs(edge, edgeNumAttrs), util.pick(edge, edgeAttrs))); }); return g; } /* * This idea comes from the Gansner paper: to account for edge labels in our * layout we split each rank in half by doubling minlen and halving ranksep. * Then we can place labels at these mid-points between nodes. * * We also add some minimal padding to the width to push the label for the edge * away from the edge itself a bit. */ function makeSpaceForEdgeLabels(g) { let graph = g.graph(); graph.ranksep /= 2; g.edges().forEach(e => { let edge = g.edge(e); edge.minlen *= 2; if (edge.labelpos.toLowerCase() !== "c") { if (graph.rankdir === "TB" || graph.rankdir === "BT") { edge.width += edge.labeloffset; } else { edge.height += edge.labeloffset; } } }); } /* * Creates temporary dummy nodes that capture the rank in which each edge's * label is going to, if it has one of non-zero width and height. We do this * so that we can safely remove empty ranks while preserving balance for the * label's position. */ function injectEdgeLabelProxies(g) { g.edges().forEach(e => { let edge = g.edge(e); if (edge.width && edge.height) { let v = g.node(e.v); let w = g.node(e.w); let label = { rank: (w.rank - v.rank) / 2 + v.rank, e: e }; util.addDummyNode(g, "edge-proxy", label, "_ep"); } }); } function assignRankMinMax(g) { let maxRank = 0; g.nodes().forEach(v => { let node = g.node(v); if (node.borderTop) { node.minRank = g.node(node.borderTop).rank; node.maxRank = g.node(node.borderBottom).rank; maxRank = Math.max(maxRank, node.maxRank); } }); g.graph().maxRank = maxRank; } function removeEdgeLabelProxies(g) { g.nodes().forEach(v => { let node = g.node(v); if (node.dummy === "edge-proxy") { g.edge(node.e).labelRank = node.rank; g.removeNode(v); } }); } function translateGraph(g) { let minX = Number.POSITIVE_INFINITY; let maxX = 0; let minY = Number.POSITIVE_INFINITY; let maxY = 0; let graphLabel = g.graph(); let marginX = graphLabel.marginx || 0; let marginY = graphLabel.marginy || 0; function getExtremes(attrs) { let x = attrs.x; let y = attrs.y; let w = attrs.width; let h = attrs.height; minX = Math.min(minX, x - w / 2); maxX = Math.max(maxX, x + w / 2); minY = Math.min(minY, y - h / 2); maxY = Math.max(maxY, y + h / 2); } g.nodes().forEach(v => getExtremes(g.node(v))); g.edges().forEach(e => { let edge = g.edge(e); if (Object.hasOwn(edge, "x")) { getExtremes(edge); } }); minX -= marginX; minY -= marginY; g.nodes().forEach(v => { let node = g.node(v); node.x -= minX; node.y -= minY; }); g.edges().forEach(e => { let edge = g.edge(e); edge.points.forEach(p => { p.x -= minX; p.y -= minY; }); if (Object.hasOwn(edge, "x")) { edge.x -= minX; } if (Object.hasOwn(edge, "y")) { edge.y -= minY; } }); graphLabel.width = maxX - minX + marginX; graphLabel.height = maxY - minY + marginY; } function assignNodeIntersects(g) { g.edges().forEach(e => { let edge = g.edge(e); let nodeV = g.node(e.v); let nodeW = g.node(e.w); let p1, p2; if (!edge.points) { edge.points = []; p1 = nodeW; p2 = nodeV; } else { p1 = edge.points[0]; p2 = edge.points[edge.points.length - 1]; } edge.points.unshift(util.intersectRect(nodeV, p1)); edge.points.push(util.intersectRect(nodeW, p2)); }); } function fixupEdgeLabelCoords(g) { g.edges().forEach(e => { let edge = g.edge(e); if (Object.hasOwn(edge, "x")) { if (edge.labelpos === "l" || edge.labelpos === "r") { edge.width -= edge.labeloffset; } switch (edge.labelpos) { case "l": edge.x -= edge.width / 2 + edge.labeloffset; break; case "r": edge.x += edge.width / 2 + edge.labeloffset; break; } } }); } function reversePointsForReversedEdges(g) { g.edges().forEach(e => { let edge = g.edge(e); if (edge.reversed) { edge.points.reverse(); } }); } function removeBorderNodes(g) { g.nodes().forEach(v => { if (g.children(v).length) { let node = g.node(v); let t = g.node(node.borderTop); let b = g.node(node.borderBottom); let l = g.node(node.borderLeft[node.borderLeft.length - 1]); let r = g.node(node.borderRight[node.borderRight.length - 1]); node.width = Math.abs(r.x - l.x); node.height = Math.abs(b.y - t.y); node.x = l.x + node.width / 2; node.y = t.y + node.height / 2; } }); g.nodes().forEach(v => { if (g.node(v).dummy === "border") { g.removeNode(v); } }); } function removeSelfEdges(g) { g.edges().forEach(e => { if (e.v === e.w) { var node = g.node(e.v); if (!node.selfEdges) { node.selfEdges = []; } node.selfEdges.push({ e: e, label: g.edge(e) }); g.removeEdge(e); } }); } function insertSelfEdges(g) { var layers = util.buildLayerMatrix(g); layers.forEach(layer => { var orderShift = 0; layer.forEach((v, i) => { var node = g.node(v); node.order = i + orderShift; (node.selfEdges || []).forEach(selfEdge => { util.addDummyNode(g, "selfedge", { width: selfEdge.label.width, height: selfEdge.label.height, rank: node.rank, order: i + (++orderShift), e: selfEdge.e, label: selfEdge.label }, "_se"); }); delete node.selfEdges; }); }); } function positionSelfEdges(g) { g.nodes().forEach(v => { var node = g.node(v); if (node.dummy === "selfedge") { var selfNode = g.node(node.e.v); var x = selfNode.x + selfNode.width / 2; var y = selfNode.y; var dx = node.x - x; var dy = selfNode.height / 2; g.setEdge(node.e, node.label); g.removeNode(v); node.label.points = [ { x: x + 2 * dx / 3, y: y - dy }, { x: x + 5 * dx / 6, y: y - dy }, { x: x + dx , y: y }, { x: x + 5 * dx / 6, y: y + dy }, { x: x + 2 * dx / 3, y: y + dy } ]; node.label.x = node.x; node.label.y = node.y; } }); } function selectNumberAttrs(obj, attrs) { return util.mapValues(util.pick(obj, attrs), Number); } function canonicalize(attrs) { var newAttrs = {}; if (attrs) { Object.entries(attrs).forEach(([k, v]) => { if (typeof k === "string") { k = k.toLowerCase(); } newAttrs[k] = v; }); } return newAttrs; } },{"./acyclic":2,"./add-border-segments":3,"./coordinate-system":4,"./nesting-graph":9,"./normalize":10,"./order":15,"./parent-dummy-chains":20,"./position":22,"./rank":24,"./util":27,"@dagrejs/graphlib":29}],9:[function(require,module,exports){ let util = require("./util"); module.exports = { run, cleanup, }; /* * A nesting graph creates dummy nodes for the tops and bottoms of subgraphs, * adds appropriate edges to ensure that all cluster nodes are placed between * these boundaries, and ensures that the graph is connected. * * In addition we ensure, through the use of the minlen property, that nodes * and subgraph border nodes to not end up on the same rank. * * Preconditions: * * 1. Input graph is a DAG * 2. Nodes in the input graph has a minlen attribute * * Postconditions: * * 1. Input graph is connected. * 2. Dummy nodes are added for the tops and bottoms of subgraphs. * 3. The minlen attribute for nodes is adjusted to ensure nodes do not * get placed on the same rank as subgraph border nodes. * * The nesting graph idea comes from Sander, "Layout of Compound Directed * Graphs." */ function run(g) { let root = util.addDummyNode(g, "root", {}, "_root"); let depths = treeDepths(g); let depthsArr = Object.values(depths); let height = util.applyWithChunking(Math.max, depthsArr) - 1; // Note: depths is an Object not an array let nodeSep = 2 * height + 1; g.graph().nestingRoot = root; // Multiply minlen by nodeSep to align nodes on non-border ranks. g.edges().forEach(e => g.edge(e).minlen *= nodeSep); // Calculate a weight that is sufficient to keep subgraphs vertically compact let weight = sumWeights(g) + 1; // Create border nodes and link them up g.children().forEach(child => dfs(g, root, nodeSep, weight, height, depths, child)); // Save the multiplier for node layers for later removal of empty border // layers. g.graph().nodeRankFactor = nodeSep; } function dfs(g, root, nodeSep, weight, height, depths, v) { let children = g.children(v); if (!children.length) { if (v !== root) { g.setEdge(root, v, { weight: 0, minlen: nodeSep }); } return; } let top = util.addBorderNode(g, "_bt"); let bottom = util.addBorderNode(g, "_bb"); let label = g.node(v); g.setParent(top, v); label.borderTop = top; g.setParent(bottom, v); label.borderBottom = bottom; children.forEach(child => { dfs(g, root, nodeSep, weight, height, depths, child); let childNode = g.node(child); let childTop = childNode.borderTop ? childNode.borderTop : child; let childBottom = childNode.borderBottom ? childNode.borderBottom : child; let thisWeight = childNode.borderTop ? weight : 2 * weight; let minlen = childTop !== childBottom ? 1 : height - depths[v] + 1; g.setEdge(top, childTop, { weight: thisWeight, minlen: minlen, nestingEdge: true }); g.setEdge(childBottom, bottom, { weight: thisWeight, minlen: minlen, nestingEdge: true }); }); if (!g.parent(v)) { g.setEdge(root, top, { weight: 0, minlen: height + depths[v] }); } } function treeDepths(g) { var depths = {}; function dfs(v, depth) { var children = g.children(v); if (children && children.length) { children.forEach(child => dfs(child, depth + 1)); } depths[v] = depth; } g.children().forEach(v => dfs(v, 1)); return depths; } function sumWeights(g) { return g.edges().reduce((acc, e) => acc + g.edge(e).weight, 0); } function cleanup(g) { var graphLabel = g.graph(); g.removeNode(graphLabel.nestingRoot); delete graphLabel.nestingRoot; g.edges().forEach(e => { var edge = g.edge(e); if (edge.nestingEdge) { g.removeEdge(e); } }); } },{"./util":27}],10:[function(require,module,exports){ "use strict"; let util = require("./util"); module.exports = { run: run, undo: undo }; /* * Breaks any long edges in the graph into short segments that span 1 layer * each. This operation is undoable with the denormalize function. * * Pre-conditions: * * 1. The input graph is a DAG. * 2. Each node in the graph has a "rank" property. * * Post-condition: * * 1. All edges in the graph have a length of 1. * 2. Dummy nodes are added where edges have been split into segments. * 3. The graph is augmented with a "dummyChains" attribute which contains * the first dummy in each chain of dummy nodes produced. */ function run(g) { g.graph().dummyChains = []; g.edges().forEach(edge => normalizeEdge(g, edge)); } function normalizeEdge(g, e) { let v = e.v; let vRank = g.node(v).rank; let w = e.w; let wRank = g.node(w).rank; let name = e.name; let edgeLabel = g.edge(e); let labelRank = edgeLabel.labelRank; if (wRank === vRank + 1) return; g.removeEdge(e); let dummy, attrs, i; for (i = 0, ++vRank; vRank < wRank; ++i, ++vRank) { edgeLabel.points = []; attrs = { width: 0, height: 0, edgeLabel: edgeLabel, edgeObj: e, rank: vRank }; dummy = util.addDummyNode(g, "edge", attrs, "_d"); if (vRank === labelRank) { attrs.width = edgeLabel.width; attrs.height = edgeLabel.height; attrs.dummy = "edge-label"; attrs.labelpos = edgeLabel.labelpos; } g.setEdge(v, dummy, { weight: edgeLabel.weight }, name); if (i === 0) { g.graph().dummyChains.push(dummy); } v = dummy; } g.setEdge(v, w, { weight: edgeLabel.weight }, name); } function undo(g) { g.graph().dummyChains.forEach(v => { let node = g.node(v); let origLabel = node.edgeLabel; let w; g.setEdge(node.edgeObj, origLabel); while (node.dummy) { w = g.successors(v)[0]; g.removeNode(v); origLabel.points.push({ x: node.x, y: node.y }); if (node.dummy === "edge-label") { origLabel.x = node.x; origLabel.y = node.y; origLabel.width = node.width; origLabel.height = node.height; } v = w; node = g.node(v); } }); } },{"./util":27}],11:[function(require,module,exports){ module.exports = addSubgraphConstraints; function addSubgraphConstraints(g, cg, vs) { let prev = {}, rootPrev; vs.forEach(v => { let child = g.parent(v), parent, prevChild; while (child) { parent = g.parent(child); if (parent) { prevChild = prev[parent]; prev[parent] = child; } else { prevChild = rootPrev; rootPrev = child; } if (prevChild && prevChild !== child) { cg.setEdge(prevChild, child); return; } child = parent; } }); /* function dfs(v) { var children = v ? g.children(v) : g.children(); if (children.length) { var min = Number.POSITIVE_INFINITY, subgraphs = []; children.forEach(function(child) { var childMin = dfs(child); if (g.children(child).length) { subgraphs.push({ v: child, order: childMin }); } min = Math.min(min, childMin); }); _.sortBy(subgraphs, "order").reduce(function(prev, curr) { cg.setEdge(prev.v, curr.v); return curr; }); return min; } return g.node(v).order; } dfs(undefined); */ } },{}],12:[function(require,module,exports){ module.exports = barycenter; function barycenter(g, movable = []) { return movable.map(v => { let inV = g.inEdges(v); if (!inV.length) { return { v: v }; } else { let result = inV.reduce((acc, e) => { let edge = g.edge(e), nodeU = g.node(e.v); return { sum: acc.sum + (edge.weight * nodeU.order), weight: acc.weight + edge.weight }; }, { sum: 0, weight: 0 }); return { v: v, barycenter: result.sum / result.weight, weight: result.weight }; } }); } },{}],13:[function(require,module,exports){ let Graph = require("@dagrejs/graphlib").Graph; let util = require("../util"); module.exports = buildLayerGraph; /* * Constructs a graph that can be used to sort a layer of nodes. The graph will * contain all base and subgraph nodes from the request layer in their original * hierarchy and any edges that are incident on these nodes and are of the type * requested by the "relationship" parameter. * * Nodes from the requested rank that do not have parents are assigned a root * node in the output graph, which is set in the root graph attribute. This * makes it easy to walk the hierarchy of movable nodes during ordering. * * Pre-conditions: * * 1. Input graph is a DAG * 2. Base nodes in the input graph have a rank attribute * 3. Subgraph nodes in the input graph has minRank and maxRank attributes * 4. Edges have an assigned weight * * Post-conditions: * * 1. Output graph has all nodes in the movable rank with preserved * hierarchy. * 2. Root nodes in the movable layer are made children of the node * indicated by the root attribute of the graph. * 3. Non-movable nodes incident on movable nodes, selected by the * relationship parameter, are included in the graph (without hierarchy). * 4. Edges incident on movable nodes, selected by the relationship * parameter, are added to the output graph. * 5. The weights for copied edges are aggregated as need, since the output * graph is not a multi-graph. */ function buildLayerGraph(g, rank, relationship) { let root = createRootNode(g), result = new Graph({ compound: true }).setGraph({ root: root }) .setDefaultNodeLabel(v => g.node(v)); g.nodes().forEach(v => { let node = g.node(v), parent = g.parent(v); if (node.rank === rank || node.minRank <= rank && rank <= node.maxRank) { result.setNode(v); result.setParent(v, parent || root); // This assumes we have only short edges! g[relationship](v).forEach(e => { let u = e.v === v ? e.w : e.v, edge = result.edge(u, v), weight = edge !== undefined ? edge.weight : 0; result.setEdge(u, v, { weight: g.edge(e).weight + weight }); }); if (Object.hasOwn(node, "minRank")) { result.setNode(v, { borderLeft: node.borderLeft[rank], borderRight: node.borderRight[rank] }); } } }); return result; } function createRootNode(g) { var v; while (g.hasNode((v = util.uniqueId("_root")))); return v; } },{"../util":27,"@dagrejs/graphlib":29}],14:[function(require,module,exports){ "use strict"; let zipObject = require("../util").zipObject; module.exports = crossCount; /* * A function that takes a layering (an array of layers, each with an array of * ordererd nodes) and a graph and returns a weighted crossing count. * * Pre-conditions: * * 1. Input graph must be simple (not a multigraph), directed, and include * only simple edges. * 2. Edges in the input graph must have assigned weights. * * Post-conditions: * * 1. The graph and layering matrix are left unchanged. * * This algorithm is derived from Barth, et al., "Bilayer Cross Counting." */ function crossCount(g, layering) { let cc = 0; for (let i = 1; i < layering.length; ++i) { cc += twoLayerCrossCount(g, layering[i-1], layering[i]); } return cc; } function twoLayerCrossCount(g, northLayer, southLayer) { // Sort all of the edges between the north and south layers by their position // in the north layer and then the south. Map these edges to the position of // their head in the south layer. let southPos = zipObject(southLayer, southLayer.map((v, i) => i)); let southEntries = northLayer.flatMap(v => { return g.outEdges(v).map(e => { return { pos: southPos[e.w], weight: g.edge(e).weight }; }).sort((a, b) => a.pos - b.pos); }); // Build the accumulator tree let firstIndex = 1; while (firstIndex < southLayer.length) firstIndex <<= 1; let treeSize = 2 * firstIndex - 1; firstIndex -= 1; let tree = new Array(treeSize).fill(0); // Calculate the weighted crossings let cc = 0; southEntries.forEach(entry => { let index = entry.pos + firstIndex; tree[index] += entry.weight; let weightSum = 0; while (index > 0) { if (index % 2) { weightSum += tree[index + 1]; } index = (index - 1) >> 1; tree[index] += entry.weight; } cc += entry.weight * weightSum; }); return cc; } },{"../util":27}],15:[function(require,module,exports){ "use strict"; let initOrder = require("./init-order"); let crossCount = require("./cross-count"); let sortSubgraph = require("./sort-subgraph"); let buildLayerGraph = require("./build-layer-graph"); let addSubgraphConstraints = require("./add-subgraph-constraints"); let Graph = require("@dagrejs/graphlib").Graph; let util = require("../util"); module.exports = order; /* * Applies heuristics to minimize edge crossings in the graph and sets the best * order solution as an order attribute on each node. * * Pre-conditions: * * 1. Graph must be DAG * 2. Graph nodes must be objects with a "rank" attribute * 3. Graph edges must have the "weight" attribute * * Post-conditions: * * 1. Graph nodes will have an "order" attribute based on the results of the * algorithm. */ function order(g, opts) { if (opts && typeof opts.customOrder === 'function') { opts.customOrder(g, order); return; } let maxRank = util.maxRank(g), downLayerGraphs = buildLayerGraphs(g, util.range(1, maxRank + 1), "inEdges"), upLayerGraphs = buildLayerGraphs(g, util.range(maxRank - 1, -1, -1), "outEdges"); let layering = initOrder(g); assignOrder(g, layering); if (opts && opts.disableOptimalOrderHeuristic) { return; } let bestCC = Number.POSITIVE_INFINITY, best; for (let i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) { sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2); layering = util.buildLayerMatrix(g); let cc = crossCount(g, layering); if (cc < bestCC) { lastBest = 0; best = Object.assign({}, layering); bestCC = cc; } } assignOrder(g, best); } function buildLayerGraphs(g, ranks, relationship) { return ranks.map(function(rank) { return buildLayerGraph(g, rank, relationship); }); } function sweepLayerGraphs(layerGraphs, biasRight) { let cg = new Graph(); layerGraphs.forEach(function(lg) { let root = lg.graph().root; let sorted = sortSubgraph(lg, root, cg, biasRight); sorted.vs.forEach((v, i) => lg.node(v).order = i); addSubgraphConstraints(lg, cg, sorted.vs); }); } function assignOrder(g, layering) { Object.values(layering).forEach(layer => layer.forEach((v, i) => g.node(v).order = i)); } },{"../util":27,"./add-subgraph-constraints":11,"./build-layer-graph":13,"./cross-count":14,"./init-order":16,"./sort-subgraph":18,"@dagrejs/graphlib":29}],16:[function(require,module,exports){ "use strict"; let util = require("../util"); module.exports = initOrder; /* * Assigns an initial order value for each node by performing a DFS search * starting from nodes in the first rank. Nodes are assigned an order in their * rank as they are first visited. * * This approach comes from Gansner, et al., "A Technique for Drawing Directed * Graphs." * * Returns a layering matrix with an array per layer and each layer sorted by * the order of its nodes. */ function initOrder(g) { let visited = {}; let simpleNodes = g.nodes().filter(v => !g.children(v).length); let simpleNodesRanks = simpleNodes.map(v => g.node(v).rank); let maxRank = util.applyWithChunking(Math.max, simpleNodesRanks); let layers = util.range(maxRank + 1).map(() => []); function dfs(v) { if (visited[v]) return; visited[v] = true; let node = g.node(v); layers[node.rank].push(v); g.successors(v).forEach(dfs); } let orderedVs = simpleNodes.sort((a, b) => g.node(a).rank - g.node(b).rank); orderedVs.forEach(dfs); return layers; } },{"../util":27}],17:[function(require,module,exports){ "use strict"; let util = require("../util"); module.exports = resolveConflicts; /* * Given a list of entries of the form {v, barycenter, weight} and a * constraint graph this function will resolve any conflicts between the * constraint graph and the barycenters for the entries. If the barycenters for * an entry would violate a constraint in the constraint graph then we coalesce * the nodes in the conflict into a new node that respects the contraint and * aggregates barycenter and weight information. * * This implementation is based on the description in Forster, "A Fast and * Simple Hueristic for Constrained Two-Level Crossing Reduction," thought it * differs in some specific details. * * Pre-conditions: * * 1. Each entry has the form {v, barycenter, weight}, or if the node has * no barycenter, then {v}. * * Returns: * * A new list of entries of the form {vs, i, barycenter, weight}. The list * `vs` may either be a singleton or it may be an aggregation of nodes * ordered such that they do not violate constraints from the constraint * graph. The property `i` is the lowest original index of any of the * elements in `vs`. */ function resolveConflicts(entries, cg) { let mappedEntries = {}; entries.forEach((entry, i) => { let tmp = mappedEntries[entry.v] = { indegree: 0, "in": [], out: [], vs: [entry.v], i: i }; if (entry.barycenter !== undefined) { tmp.barycenter = entry.barycenter; tmp.weight = entry.weight; } }); cg.edges().forEach(e => { let entryV = mappedEntries[e.v]; let entryW = mappedEntries[e.w]; if (entryV !== undefined && entryW !== undefined) { entryW.indegree++; entryV.out.push(mappedEntries[e.w]); } }); let sourceSet = Object.values(mappedEntries).filter(entry => !entry.indegree); return doResolveConflicts(sourceSet); } function doResolveConflicts(sourceSet) { let entries = []; function handleIn(vEntry) { return uEntry => { if (uEntry.merged) { return; } if (uEntry.barycenter === undefined || vEntry.barycenter === undefined || uEntry.barycenter >= vEntry.barycenter) { mergeEntries(vEntry, uEntry); } }; } function handleOut(vEntry) { return wEntry => { wEntry["in"].push(vEntry); if (--wEntry.indegree === 0) { sourceSet.push(wEntry); } }; } while (sourceSet.length) { let entry = sourceSet.pop(); entries.push(entry); entry["in"].reverse().forEach(handleIn(entry)); entry.out.forEach(handleOut(entry)); } return entries.filter(entry => !entry.merged).map(entry => { return util.pick(entry, ["vs", "i", "barycenter", "weight"]); }); } function mergeEntries(target, source) { let sum = 0; let weight = 0; if (target.weight) { sum += target.barycenter * target.weight; weight += target.weight; } if (source.weight) { sum += source.barycenter * source.weight; weight += source.weight; } target.vs = source.vs.concat(target.vs); target.barycenter = sum / weight; target.weight = weight; target.i = Math.min(source.i, target.i); source.merged = true; } },{"../util":27}],18:[function(require,module,exports){ let barycenter = require("./barycenter"); let resolveConflicts = require("./resolve-conflicts"); let sort = require("./sort"); module.exports = sortSubgraph; function sortSubgraph(g, v, cg, biasRight) { let movable = g.children(v); let node = g.node(v); let bl = node ? node.borderLeft : undefined; let br = node ? node.borderRight: undefined; let subgraphs = {}; if (bl) { movable = movable.filter(w => w !== bl && w !== br); } let barycenters = barycenter(g, movable); barycenters.forEach(entry => { if (g.children(entry.v).length) { let subgraphResult = sortSubgraph(g, entry.v, cg, biasRight); subgraphs[entry.v] = subgraphResult; if (Object.hasOwn(subgraphResult, "barycenter")) { mergeBarycenters(entry, subgraphResult); } } }); let entries = resolveConflicts(barycenters, cg); expandSubgraphs(entries, subgraphs); let result = sort(entries, biasRight); if (bl) { result.vs = [bl, result.vs, br].flat(true); if (g.predecessors(bl).length) { let blPred = g.node(g.predecessors(bl)[0]), brPred = g.node(g.predecessors(br)[0]); if (!Object.hasOwn(result, "barycenter")) { result.barycenter = 0; result.weight = 0; } result.barycenter = (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2); result.weight += 2; } } return result; } function expandSubgraphs(entries, subgraphs) { entries.forEach(entry => { entry.vs = entry.vs.flatMap(v => { if (subgraphs[v]) { return subgraphs[v].vs; } return v; }); }); } function mergeBarycenters(target, other) { if (target.barycenter !== undefined) { target.barycenter = (target.barycenter * target.weight + other.barycenter * other.weight) / (target.weight + other.weight); target.weight += other.weight; } else { target.barycenter = other.barycenter; target.weight = other.weight; } } },{"./barycenter":12,"./resolve-conflicts":17,"./sort":19}],19:[function(require,module,exports){ let util = require("../util"); module.exports = sort; function sort(entries, biasRight) { let parts = util.partition(entries, entry => { return Object.hasOwn(entry, "barycenter"); }); let sortable = parts.lhs, unsortable = parts.rhs.sort((a, b) => b.i - a.i), vs = [], sum = 0, weight = 0, vsIndex = 0; sortable.sort(compareWithBias(!!biasRight)); vsIndex = consumeUnsortable(vs, unsortable, vsIndex); sortable.forEach(entry => { vsIndex += entry.vs.length; vs.push(entry.vs); sum += entry.barycenter * entry.weight; weight += entry.weight; vsIndex = consumeUnsortable(vs, unsortable, vsIndex); }); let result = { vs: vs.flat(true) }; if (weight) { result.barycenter = sum / weight; result.weight = weight; } return result; } function consumeUnsortable(vs, unsortable, index) { let last; while (unsortable.length && (last = unsortable[unsortable.length - 1]).i <= index) { unsortable.pop(); vs.push(last.vs); index++; } return index; } function compareWithBias(bias) { return (entryV, entryW) => { if (entryV.barycenter < entryW.barycenter) { return -1; } else if (entryV.barycenter > entryW.barycenter) { return 1; } return !bias ? entryV.i - entryW.i : entryW.i - entryV.i; }; } },{"../util":27}],20:[function(require,module,exports){ module.exports = parentDummyChains; function parentDummyChains(g) { let postorderNums = postorder(g); g.graph().dummyChains.forEach(v => { let node = g.node(v); let edgeObj = node.edgeObj; let pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w); let path = pathData.path; let lca = pathData.lca; let pathIdx = 0; let pathV = path[pathIdx]; let ascending = true; while (v !== edgeObj.w) { node = g.node(v); if (ascending) { while ((pathV = path[pathIdx]) !== lca && g.node(pathV).maxRank < node.rank) { pathIdx++; } if (pathV === lca) { ascending = false; } } if (!ascending) { while (pathIdx < path.length - 1 && g.node(pathV = path[pathIdx + 1]).minRank <= node.rank) { pathIdx++; } pathV = path[pathIdx]; } g.setParent(v, pathV); v = g.successors(v)[0]; } }); } // Find a path from v to w through the lowest common ancestor (LCA). Return the // full path and the LCA. function findPath(g, postorderNums, v, w) { let vPath = []; let wPath = []; let low = Math.min(postorderNums[v].low, postorderNums[w].low); let lim = Math.max(postorderNums[v].lim, postorderNums[w].lim); let parent; let lca; // Traverse up from v to find the LCA parent = v; do { parent = g.parent(parent); vPath.push(parent); } while (parent && (postorderNums[parent].low > low || lim > postorderNums[parent].lim)); lca = parent; // Traverse from w to LCA parent = w; while ((parent = g.parent(parent)) !== lca) { wPath.push(parent); } return { path: vPath.concat(wPath.reverse()), lca: lca }; } function postorder(g) { let result = {}; let lim = 0; function dfs(v) { let low = lim; g.children(v).forEach(dfs); result[v] = { low: low, lim: lim++ }; } g.children().forEach(dfs); return result; } },{}],21:[function(require,module,exports){ "use strict"; let Graph = require("@dagrejs/graphlib").Graph; let util = require("../util"); /* * This module provides coordinate assignment based on Brandes and Köpf, "Fast * and Simple Horizontal Coordinate Assignment." */ module.exports = { positionX: positionX, findType1Conflicts: findType1Conflicts, findType2Conflicts: findType2Conflicts, addConflict: addConflict, hasConflict: hasConflict, verticalAlignment: verticalAlignment, horizontalCompaction: horizontalCompaction, alignCoordinates: alignCoordinates, findSmallestWidthAlignment: findSmallestWidthAlignment, balance: balance }; /* * Marks all edges in the graph with a type-1 conflict with the "type1Conflict" * property. A type-1 conflict is one where a non-inner segment crosses an * inner segment. An inner segment is an edge with both incident nodes marked * with the "dummy" property. * * This algorithm scans layer by layer, starting with the second, for type-1 * conflicts between the current layer and the previous layer. For each layer * it scans the nodes from left to right until it reaches one that is incident * on an inner segment. It then scans predecessors to determine if they have * edges that cross that inner segment. At the end a final scan is done for all * nodes on the current rank to see if they cross the last visited inner * segment. * * This algori