JS-modulesnetflow-layers
1.13.0 · API 1

netflow-layers

Lays out a graph in rows (top to bottom or left to right) with story order, tags instead of very long links, a column for the pages every route passes, and tall maps cut into columns.

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).

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

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

Highlights

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)RowsStand-in stopsGold linksCrossingsWidth x height (px)Build time (ms)
today (rows by fewest steps)1,33733,0871,0271,2164,223 x 160,446392
story order1,90848,779978052,862 x 228,966532
tags (links over 8 rows)1,337111,027605,816 x 160,44660
tags + must-pass column1,337111,027601,913 x 160,44654
tags + column + columns for tall maps1,337111,0276017,389 x 13,446114
everything on1,9081,286973616,874 x 13,566198

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.

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.

AlternativeSize (smaller is better)Where it wins / loses
ELK layered (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 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 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 flows5.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

How to use it

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):

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:

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).

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).

Details

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.

Details

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.

Details

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.

Details

"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 }:

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.

Who uses it

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.

Good to know

What it is not

Speed and efficiency numbers

Measured on real data: cloud Chromium 156.0.8078.79, Firefox 158.0 and WebKitGTK 2.52.6, one browser at a time, 2026.11.22

Medians of 3 runs (ms); lower is better. The graph is the real product path graph of the retail file: a link joins two items bought one after the other in the same invoice, the 300 or 1,000 items with the most links, and the two strongest links per item (600 and 2,000 links), options dir: "TB" and tagLen: 8. Without thinning, 300 items with 27,731 real links are refused as too big for a layered drawing. This replaces the earlier generated 60 and 300 item graphs.

Compared: netflow-layers against dagre 0.8.5 on a real product path graph (ms, Chromium / Firefox / WebKitGTK).

netflow-layers lays out 300 real items 35 to 52 times faster than dagre
netflow-layers
300 items, Chromium11.3
300 items, Firefox11
300 items, WebKitGTK15
1,000 items, Chromium26.4
1,000 items, Firefox23
1,000 items, WebKitGTK21
dagre
300 items, Chromium395
300 items, Chromium · / ours35x
300 items, Firefox444
300 items, Firefox · / ours40x
300 items, WebKitGTK773
300 items, WebKitGTK · / ours52x
1,000 items, Chromium3,636
1,000 items, Chromium · / ours138x
1,000 items, Firefox4,294
1,000 items, Firefox · / ours187x
1,000 items, WebKitGTK5,103
1,000 items, WebKitGTK · / ours243x

Source: Measured on real data: cloud Chromium 156.0.8078.79, Firefox 158.0 and WebKitGTK 2.52.6, one browser at a time, 2026.11.22.

Crossings against dagre and ELK

Confirmed on real data against dagre (2026.11.26, the real retail path graph above, thinned to 2 links per item, default options, node tools/real-corpus/layers-crossings.js): every link as a straight line between box centers, 774 against dagre's 1,269 at 300 items and 9,896 against 12,593 at 1,000; the lines as drawn (through stand-in stops and dagre's bend points), 577 against 1,140 and 7,216 against 10,941. ELK was not counted on real data. The 2026.11.22 count (17,099 against 12,593 at 1,000) had tagLen: 8 on and drew each tag link, which shows as two short stubs, as a full line. Random maps with no loops, row crossings with the module's default options (since 1.8.0: deep and rows left out), lower is tidier. This is the module on its own, not Netflow Map's Auto layout, which is measured separately.

Compared: netflow-layers against dagre 0.8.5 and ELK 0.12.1 (row crossings, lower is tidier).

netflow-layers' default options draw 4 to 12% fewer row crossings than dagre and ELK on generated random maps with no loops
400 items
netflow-layers5103
ELK5313
dagre5374
1,000 items
netflow-layers21166
ELK22602
dagre23965

Counted in Node, so the numbers do not depend on the browser. The default layout takes about 35 to 120 ms on these maps in the cloud browsers, far under dagre, which took about 0.8 s at 400 items and 7.2 s at 1,000 in the cloud.

Source: Crossings against dagre and ELK.

Crossings against Graphviz with the opt-in sift option

A layered test map, row crossings, lower is tidier. sift is off by default; without it the same map gives 4,679 and 28,035 crossings.

Compared: netflow-layers with sift: true against Graphviz (crossings, lower is tidier).

With the opt-in sift option, netflow-layers draws 6 to 9% fewer crossings than Graphviz on a layered test map
1,000 items
netflow-layers, sift: true4222
Graphviz4494
3,000 items
netflow-layers, sift: true24390
Graphviz26894

Layout takes 1.19 to 1.37 times as long as without sift, in the three cloud browsers (Chromium 156, Firefox 158, WebKitGTK 2.52.6).

Source: Crossings against Graphviz with the opt-in sift option.

More

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).

Credits

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):