# netflow-layers

**Version 1.13.0 · API 1**

**Tested in:** Chromium 156, Firefox 158, WebKitGTK 2.52.6 (sift timing and crossings); Node module tests incl. the new sift check (WebAssembly and JS give the same result)

Part of [JS-modules](../../README.md). MIT License, Copyright (c) 2026 LFC Foundry.

## What it is and how it works

Lays out a graph in rows (a layered drawing), top to bottom or left to right. It is the layered engine behind Netflow Map's Flow, Top-down, Spine, Lanes, Arc and Matrix views. 26.6 KB gzipped (20.2 KB minified and gzipped).

- Each page goes in a row, by fewest steps from the start or in story order.
- Pages in each row are reordered to cut crossings, then placed.
- Long links get stand-in stops or short tags.
- One call draws it: `NetflowLayers.toSvg(links)` returns SVG text (see Use).

## Why it exists

Ordinary layered drawings make a mess of "going back" links and very long links in stories with thousands of pages. This engine has switches for exactly those maps.

**Where it helps**

- Story and branching-narrative maps.
- User-flow and page-journey diagrams.
- Tall maps that should be cut into columns.

**Not for:** link labels, ports or curved routing around boxes.

## Use

Copy `netflow-layers.js`.

Load `netflow-layers.js` (nothing else needed). One call, data only (items are made from the links and sized to their names):

```js
svgElement.outerHTML = NetflowLayers.toSvg([{ from: "start", to: "a" }, { from: "start", to: "b" }, { from: "a", to: "b" }]);
containerDiv.innerHTML = NetflowLayers.toSvg([{ from: "start", to: "a" }, { from: "start", to: "b" }, { from: "a", to: "b" }]);   // the same call into a <div> you already have
```

Links point from an item to the next one. Your table can keep its own column names: `toSvg(rows, { from: 'Source', to: 'Target', value: 'Amount' })`, or leave them out and the first two text columns and the first number column are used (rows straight from a CSV work). `toSvg` returns SVG text (a string): put it in the page with `element.outerHTML = ...` or save it as a `.svg` file.

The two steps:

```js
var R = NetflowLayers.layout({
  root: "start",
  nodes: [{ id: "start", w: 140, h: 44 }, { id: "a", w: 140, h: 44 }, { id: "b", w: 140, h: 44 }],
  edges: [{ from: "start", to: "a" }, { from: "start", to: "b" }, { from: "a", to: "b" }, { from: "b", to: "start" }]
}, { dir: "TB", rows: "story", tagLen: 8 });
svgElement.innerHTML = NetflowLayers.toSvg(R);        // or draw R.nodes and R.edges yourself
```

Your input is never changed: the result holds copies of your nodes and links with the places added. **Try it:** `demo.html` (type links, the picture redraws). **Test it:** `node modules/netflow-layers/test/run.js` (19 checks, about 0.4 s).

## Options

`layout(graph, opts)`. Input: `graph.nodes` are `{ id, w, h }` plus anything you like (`label`, `choke: true` for the must-pass pages, `lane` as a number); `graph.edges` are `{ from, to }` (`count` optional); `graph.root` is the id where the story starts (without it, the first pages with no way in start the rows).

| Option | Meaning |
|---|---|
| `dir` | `"LR"` (default, rows go left to right) or `"TB"` (rows go down). |
| `rows` | `"best"` (default since 1.7.0: lays out both ways below and keeps the one with fewer crossings; a tie keeps `"route"`), `"route"` (a page sits at its fewest steps from the start; the default before 1.7.0) or `"story"` (a page sits below every page that leads to it; only real loops go back). |
| `tagLen` | A number such as 8: links longer than this many rows become `kind: "tag"` (a stub and a name at each end), with no stand-in stops. `0` = off (default). |
| `spine` | `true` puts the nodes marked `choke: true` in one line. |
| `better` | `true` adds six more rounds of reordering and deepens from both the best and the usual result, so it never ends with more crossings than without it; about 1.1 to 1.9 times the time. |
| `deep` | Left out (default since 1.7.0): a lighter deeper search after the usual rounds (neighbor swaps after each row, at most 12 rounds, stops after 4 with no gain; 2 before 1.8.0). `true`: the full search (at most 80 rounds, stops after 4 with no gain). `false`: none (the default before 1.7.0). Never more crossings than without it. 1,000 random items: 33,763 crossings with `deep: false, rows: "route"`, 21,166 by default (21,440 before 1.8.0) and with `deep: true` (dagre 23,965, ELK 22,602), in about 17, 112 to 118 and 114 to 122 ms in 3 cloud browsers. At 400 items the default is quicker than `deep: true` (34 to 38 against 44 to 51 ms) with the same 5,103 crossings. Maps over 20,000 stops skip it; `orderRounds: 0` skips both the search and the second row choice. |
| `sift` | `true`: after the order search, each item slides to the place in its row with the fewest crossings (inside its lane), most-linked first, then neighbor swaps that also take ties; 2 rounds. A number gives that many rounds (`6` for a bit fewer crossings at about twice the layout time). Never more crossings than without it; off by default. Layered test map, 1,000 / 3,000 items: 4,222 / 24,390 crossings (Graphviz 4,494 / 26,894; without sift 4,679 / 28,035) at 1.19 to 1.37 times the layout time in 3 cloud browsers. |
| `orderRounds` | A number: rounds of the usual order search (default 6, or 2 on maps over 600 stops). `0` keeps the first order, for a caller such as netflow-sankey that reorders anyway. |
| `bundle` | `true` lets links that start at the same page share one run of stand-in stops, and links that end at the same page and are alone at their start share one run from the end upward. Fewer stops and crossings on maps without tags; about the same where tags already removed the long links. |
| `wrap` | `true` (with `dir: "TB"`) cuts a map more than 2.2 times taller than wide into columns side by side; links between columns become tags. |
| `lanes` | `true` puts nodes with the same `lane` number in their own band. |
| `prev` | `{ id: 0-1 order from the last drawing }` keeps the order stable. |
| `spread` | `"wide"` gives a looser, more tree-like drawing for wide screens: boxes beside each other get 2.4 times the gap top to bottom (1.4 times left to right), rows are 1.7 times further apart left to right (same top to bottom), and a box that leads to several boxes sits over the middle of them instead of over their weighted average. `"tight"` or nothing is the older, compact drawing exactly. |
| `gapNode`, `gapLayer`, `maxVertices` | Spacing, and the stop limit (default 90,000 stand-in stops, then it throws with `tooBig: true`). |

All the switches are off by default; with everything off the drawing is exactly Netflow Map's older one (15 checks, 8 pinned fingerprints made by the app's own engine).

Returns `{ nodes, edges, pos, order, lanes, width, height, crossings, rows, ruler, info, dir }`:
- `nodes`: yours with `x, y` (top left) and `layer` (row number) added; `pos` is the same by id.
- `edges`: yours with `kind` (`fwd`, `back`, `flat` = same row, `self`, `tag`) and `pts` (the stand-in stops of a long link, each `[x, y]`). A `tag` has `tagFwd` (true when it goes on down the page) and no `pts`.
- `info`: `{ rows, columns, stops, back, flat, tags, goldTags, rowMode }`. `ruler`: for each row `{ at, edge }` (where to write "step 12" and where its column starts).

`toSvg(result, colors)` draws boxes, curves and tag chips as a plain SVG string (text is escaped); `title`, `subtitle` and `source` (the same words as in every chart module) draw a bold title and a lighter subtitle above the chart and a small source line below it, without changing the chart itself; `taper: true` draws forward links as shapes that narrow from 5 px to 1 px, so direction shows without arrowheads. `highlight` (since 1.12.0) names one box by id or label: it and every link touching it keep their colors, and the rest fade toward `background` (default white); names stay readable.

## WebAssembly (1.5.0)
The row-order search (crossing counts, sweeps, neighbor swaps) and the row placement run in WebAssembly, built from `wasm/order.c` (`sh wasm/build.sh`, then `python3 wasm/embed.py` puts the 10,909 bytes into `netflow-layers.js` as base64). A page needs only `'wasm-unsafe-eval'` in `script-src`; nothing is fetched. The pictures are the same to the last digit as the plain JavaScript, which stays in the file as the fallback (no WebAssembly, blocked by the page rule, a map under 100 stops, or a failed call). `NetflowLayers.wasm` is `{ on, min, calls, error }`: set `on = false` to force JavaScript, `min` to change the 100-stop limit, read `calls` and `error` to see what happened. Measured in ungoogled Chromium 155, Firefox 158 and WebKitGTK 2.52.6, 5,000 pages (lower is better): 37.7 to 26.9, 55 to 41 and 53 to 43 ms with wasm off and on; 1,000 pages 6.65 to 4.8, 10 to 8 and 8.5 to 7 ms. The first map of 100 stops or more compiles the module once (about 4 ms in WebKitGTK, under 1 ms in Chromium).

## How it compares

A small layered-graph drawer. It does less than ELK, and adds switches aimed at long story maps; ELK also wraps tall maps and bundles edges. With its default options (since 1.7.0) it draws fewer crossings than dagre and ELK on generated random maps of 400 and 1,000 items (on a real retail product path graph, thinned to 2 links per item, with default options: 774 against dagre's 1,269 at 300 items and 9,896 against 12,593 at 1,000 counting every link as a straight line between box centers, and 577 against 1,140 and 7,216 against 10,941 counting the lines as drawn, through the stand-in stops and dagre's bend points; ELK not counted; `node tools/real-corpus/layers-crossings.js`. An earlier check on 2026.11.22 had `tagLen: 8` on and counted each tag link, which is drawn as two short stubs, as a full straight line, which gave 17,099 at 1,000 items; with those tags left out as drawn it is 924); on a real retail product path graph it lays out 300 items 35 to 52 times and 1,000 items 138 to 243 times faster than dagre in the three test browsers (table below). ELK's speed and d3-dag: not measured.

| Alternative | Size (smaller is better) | Where it wins / loses |
|---|---|---|
| ELK layered ([elkjs](https://github.com/kieler/elkjs) 0.12.1, EPL-2.0 or GPL-3.0-or-later) | 1,502,625 bytes minified, 439,741 gzipped (`elk.bundled.js`, the layout code in one file; the API file alone is 4,365 bytes with the layout code in a separate worker file) | ELK wins on routing (polyline, orthogonal, spline), edge bundling, wrapping, network-simplex rows, Brandes-Koepf placement and over 140 options. Crossings with default options since 1.8.0, the same as `deep: true`: 4 to 6% fewer than ELK (5,103 against 5,313 at 400 items, 21,166 against 22,602 at 1,000). The old default (`deep: false, rows: "route"`) draws about 50% more (7,950 and 33,763). |
| [dagre](https://github.com/dagrejs/dagre) 0.8.5 (MIT) | 101,693 bytes minified, 27,772 gzipped (whole `dagre.js`, terser 5.51.2 `-c passes=2 -m`) | The long-standing layered layout for boxes and arrows. dagre draws 5,374 crossings at 400 items and 23,965 at 1,000. The old default here (`deep: false, rows: "route"`) draws 41 to 48% more; default options since 1.8.0 draw 5 to 12% fewer (5,103 and 21,166), (generated random maps; in about 34 to 38 and 112 to 118 ms in 3 cloud browsers against dagre's 0.8 s and 7.2 s); on a real retail product path graph (2026.11.22): 11 to 15 ms against dagre's 395 to 773 ms at 300 items and 21 to 26 ms against 3.6 to 5.1 s at 1,000 items; `deep: true` draws the same in about 44 to 51 and 114 to 122 ms. This module is about a third of its size |
| [d3-dag](https://github.com/erikbrinkman/d3-dag) 1.2.2 (MIT) | 137,471 bytes minified, 42,115 gzipped (the published `d3-dag.iife.min.js`, minified again) | Not compared here on speed or crossings; its "fast" quality level is the class this module is in |
| d3-sankey (BSD-3-Clause, 0.12.3), a different tool for weighted flows | 5.7 KB minified, 2.0 KB gzipped (this module: 78,408 bytes readable, 26.6 KB gzipped; 20.2 KB minified and gzipped) | No loops in d3-sankey; this engine handles them. Fewer crossings (see below). In the race below it is faster in WebKitGTK at 1,500 items and slower elsewhere. |

**The one race on file** (1,500 items and 2,283 links, lower is better, ms; Netflow Map's Flow view built on this engine, then on d3-sankey; the whole app build with extra work such as must-pass pages, scores and loops, so it was not run on this module alone): ungoogled Chromium 155 30 against 21, Firefox 158 80 against 74, WebKitGTK 2.52.6 25 against 146 22 against 78. In Chromium 155 it was slower at all three sizes (28, 20 and 30 ms against 5.4, 7.8 and 21 ms at 100, 400 and 1,500 items), and slower than d3-sankey at 100 items in all three browsers.

**Crossings** at 100, 400 and 1,500 items (lower is better; counted as straight lines between item centers): 173, 1,039 and 3,761 for this engine against 468, 5,415 and 107,409 for d3-sankey.

**Where it loses**
- Plain boxes only: link labels, ports and curved routing around boxes are not here (ELK has them).
- Since 1.7.0 the default beats dagre and ELK on crossings but takes about 4 to 7 times as long as the old default (`deep: false, rows: "route"`, which still draws 41 to 50% more crossings than both); still far under dagre.
- Not measured: ELK's speed, and d3-dag's speed and crossings.
- Story order is a choice, not the default: on a loop-free random map crossings rise from 90,351 to 131,569 (worse). Tags and bundling are the same kind of choice.
- Brandes-Koepf straight-line placement was built, measured and left out: fewer slanted pieces but wider maps (4,223 to 49,496 px at 5,002 pages).

## What it does differently (Node 22, median of 5 builds, 2026-10-17; story-shaped test maps)

Node numbers are not what a visitor's browser does: use them to compare rows with each other. Sizes and counts are exact; only the times depend on the browser. They come from Netflow Map's full build path, which first folds straight runs of pages into one box, so page counts differ a little from feeding the module a raw map; the drawing code is the same.

| Story (5,002 pages) | Rows | Stand-in stops | Gold links | Crossings | Width x height (px) | Build time (ms) |
|---|---|---|---|---|---|---|
| today (rows by fewest steps) | 1,337 | 33,087 | 1,027 | 1,216 | 4,223 x 160,446 | 392 |
| story order | 1,908 | 48,779 | 97 | 805 | 2,862 x 228,966 | 532 |
| tags (links over 8 rows) | 1,337 | 11 | 1,027 | 60 | 5,816 x 160,446 | 60 |
| tags + must-pass column | 1,337 | 11 | 1,027 | 60 | 1,913 x 160,446 | 54 |
| tags + column + columns for tall maps | 1,337 | 11 | 1,027 | 60 | 17,389 x 13,446 | 114 |
| everything on | 1,908 | 1,286 | 97 | 36 | 16,874 x 13,566 | 198 |

All columns are smaller-is-better. Story order makes "going back" honest (links drawn as back or sideways drop from 1,027 to 97, the real loops). Tags replace the stand-in stops of very long links (33,087 stops become 11), and a 20,000-page story that is refused as too big draws in about 0.4 s. Bundling on this story with shortest-route rows: stand-in stops 33,087 to 30,654 and crossings 1,216 to 1,065 (about 12% fewer); on a loop-free random map of 1,000 pages stops go 531 to 326 (39% fewer); with tags on there is little left to bundle. On a random map with no loops, story order drops gold links from 851 to 0.

## What it is not

- Plain boxes only: you give each node's size; no link labels, ports or curved routing around boxes.
- Rows are by fewest steps from `root` unless `rows: "story"`.
- More than 90,000 stand-in stops (page boxes plus the stops long links need) throws; turn on `tagLen` first.

## See it in action

Used right now in Netflow Map (its repository is private, so there is no link). Checked in each app's shipped file on its main branch on 2026.10.29. The module's own page is `demo.html` in this folder.

## For AI agents

- Give every node `w` and `h`, set `root`, and try `rows: "story"` and `tagLen: 8` for long stories; use `dir: "TB"` with `wrap: true` for tall maps. Handle the `tooBig` error by turning on `tagLen`.
- The result is plain data (`R.nodes`, `R.edges`); draw it yourself or use `toSvg`. The input is never changed.

## Prior art and credits

- The layered ("Sugiyama-style") way of drawing a graph in rows: rows first, then an order inside each row to cut crossings, then places. Idea only, no code. The same steps are in [Eclipse ELK](https://eclipse.dev/elk/) (EPL license; its overview was read, its code was not), dagre and d3-dag.
- Ideas read about in ELK's documentation and papers and written fresh here: wrapping a tall map into columns (ELK calls it [edge wrapping](https://eclipse.dev/elk/reference/options/org-eclipse-elk-layered-wrapping-strategy.html) and layer unzipping) and letting links that share a page share their stops (bundling, also in a Microsoft Research paper on layered drawings with edge bundling). Idea only, no code.
- Tapered links (narrow from the source to the target, so direction shows without arrowheads) follow the idea of Holten and van Wijk (["A user study on visualizing directed edges in graphs"](https://doi.org/10.1145/1518701.1519054), CHI 2009); the research notes mark that study as not re-read, so treat the credit as a pointer.
- Brandes-Koepf placement (["Fast and Simple Horizontal Coordinate Assignment"](https://doi.org/10.1007/3-540-45848-4_3), Graph Drawing 2001) was tried in the research, measured and not included; none of its code is in this module.
- The crossing counter is a standard Fenwick-tree (binary indexed tree) count. Textbook technique, no code from others.
- The engine was cut from Netflow Map (LFC Foundry's own code).

No code from others; the ideas above are theirs.

**Original sources** (links checked 2026-11-14; ideas only unless the section above says code is included):

- Layered drawing: Sugiyama, Tagawa and Toda, ["Methods for Visual Understanding of Hierarchical System Structures"](https://doi.org/10.1109/TSMC.1981.4308636), IEEE SMC, 1981
- Cycle breaking: Eades, Lin and Smyth, ["A fast and effective heuristic for the feedback arc set problem"](https://doi.org/10.1016/0020-0190(93)90079-O), Information Processing Letters, 1993
- Barycenter and median sweeps with neighbor swaps ("transpose"): Gansner, Koutsofios, North and Vo, ["A Technique for Drawing Directed Graphs"](https://graphviz.org/documentation/TSE93.pdf), IEEE TSE, 1993 (the method of [Graphviz](https://graphviz.org/) dot)
- The `sift` step: Matuszewski, Schonfeld and Molitor, ["Using Sifting for k-Layer Straightline Crossing Minimization"](https://doi.org/10.1007/3-540-46648-7_22), Graph Drawing 1999
- Crossing counting in n log n: Barth, Junger and Mutzel, ["Simple and Efficient Bilayer Cross Counting"](https://doi.org/10.7155/jgaa.00088), JGAA 2004, with a Fenwick tree (Peter Fenwick, ["A new data structure for cumulative frequency tables"](https://doi.org/10.1002/spe.4380240306), 1994)

## Full research notes

The long version of this page (the full tables from Netflow Map's own notes, the history of each switch, and the longer comparison write-ups) is kept in the project's private knowledge base.
