mermaid.flowchart.order

index · mermaid.flowchart

Overview

The second step of a flowchart's layout: the order of each layer's nodes, chosen so that few links cross.

The method is the barycenter sweep. A layer is sorted by the mean position of each node's neighbours in the layer before it, down the layers and back up, a fixed number of times. A node with no neighbour there is keyed by its own position, and sorted with the rest. The order with the fewest crossings seen is kept, so a sweep that makes things worse costs nothing.

The result depends on the graph alone. Every tie falls back on the position a node had, so the same source gives the same picture.

A subgraph's nodes stay together in every layer, between the subgraph's two border nodes, and move among their neighbours as one item. Two subgraphs keep one order in every layer they share. A chart with no subgraph is sorted by the same rule: its one item is the whole graph.

Functions

NameSignature
crossingscrossings(layered) -> count
sweepsweep(layered) -> layered

crossings(layered) -> count

Count the links that cross in a layered graph

Counts over every pair of neighbouring layers, in the order the layers' tables are in.

sweep(layered) -> layered

Order each layer of a layered graph to cross few links

layered is what mermaid.flowchart.rank.layers returns. Each layer's table is put in its new order, and each layout node gets its position in order, from 1. layered.crossings holds the crossings left. Returns the table it was given.