Skip to main content

ELK

πŸ”— Gallery

ELK is a mature layout engine, actively maintained by an academic research group at Christian Albrechts University in Kiel.

note

This page was generated by AI. D2 rendered the diagrams from the linked source files.

Reference​

https://www.eclipse.org/elk/reference.html

Algorithms​

Set layout-engine to elk to use ELK. The default algorithm, layered, arranges shapes in layers. To choose another algorithm, set elk-algorithm in vars.d2-config.data. The other diagram-wide options go in the same map. From the command line, add -- to the option name. A value in the diagram takes precedence over the flag. Values are case-insensitive. Run d2 layout elk to see the flags your version supports.

OptionDefaultWhat it controls
elk-algorithmlayeredThe algorithm that arranges the diagram.
elk-forceModelfruchterman_reingoldHow attraction and repulsion place shapes.
elk-forceRepulsion5How strongly unconnected shapes repel each other in the eades model.
elk-stressDesiredEdgeLength100Target distance between the centers of connected shapes in stress layout.
elk-mrtreeSearchOrderdfsWhich paths determine the levels in Mr. Tree.
elk-mrtreeWeightingmodel_orderThe order of branches within a tree level.
elk-radialRadius0Initial spacing between rings in a radial tree.
elk-radialWedgeCriterianode_sizeEach branch's share of the circle.
elk-radialCompactornoneHow closely rings or branches pack around the root.

Choose an algorithm​

These five layouts use the same tree. Layered groups shapes into columns. Force balances attraction and repulsion. Stress places shapes according to their distance through the graph. Mr. Tree organizes parents and children into levels. Radial expects a directed tree and arranges its levels in rings.

Each comparison on this page changes one option and shows the diagrams at the same scale.

layered (default)D2 source
forceD2 source
stressD2 source
mrtreeD2 source
radialD2 source

To select an algorithm and set its options for one render, run

Layered layout options​

Use these options with elk-algorithm: layered, the default. Set elk-layer-constraint directly on the shape you want to position.

OptionDefaultWhat it controls
elk-mergeEdgesfalseShared entry and exit points.
elk-edgeRoutingorthogonalRight-angle or diagonal segments.
elk-feedbackEdgesfalseThe path an edge takes back through a cycle.
elk-wrappingStrategyoffWhere a pipeline can wrap onto another row.
elk-forceNodeModelOrderfalseThe order of top-level shapes within a layer.
elk-nodeNode20Gap between shapes in the same layer.
elk-edgeNode40Clearance between a shape and an edge passing through its layer.
elk-edgeEdge10Gap between edges passing through the same layer.
elk-nodeNodeBetweenLayers70Gap between shapes in adjacent layers.
elk-edgeNodeBetweenLayers40Clearance between a bend and the shapes in adjacent layers.
elk-edgeEdgeBetweenLayers50Gap between parallel edge segments between layers.
elk-padding50 on each sideSpace inside a container’s border.
elk-nodeSelfLoop50Gap between a shape and its self-loop.
elk-layer-constraintnoneThe layer assigned to one shape.

Routing and order​

Merge connections​

With elk-mergeEdges set to true, the connections to Orders, Billing and Events leave Gateway at one point and then branch. Incoming connections can also share an entry point. Connections to specific SQL table columns stay attached to their columns.

false (default)D2 source
trueD2 source

To enable merging for a single render, leave elk-mergeEdges out of the source and run

Choose how connections bend​

The routes from Router to East and West turn at right angles with elk-edgeRouting set to orthogonal. Switch to polyline and the two routes run diagonally. The connection to Local is straight in both diagrams.

orthogonal (default)D2 source
polylineD2 source

Route a cycle​

Follow the arrow from Write back to Read. By default it leaves the left side of Write. With elk-feedbackEdges enabled, it leaves the right side, goes around the whole row and enters Read from the left.

false (default)D2 source
trueD2 source

Wrap a long pipeline​

With elk-wrappingStrategy set to off, the pipeline runs from Fetch to Ship in one row. single_edge can wrap between layers joined by a single edge. Here it wraps after Pack. multi_edge can also wrap where several edges cross between layers. Here it wraps before Pack, where both Build and Test connect to it.

off (default)D2 source
single_edgeD2 source
multi_edgeD2 source

Keep shapes in declaration order​

The source declares One before Two. By default, ELK puts Two above One to avoid a crossing. Set elk-forceNodeModelOrder to true to keep the declared order within each layer of top-level shapes. The lines from Alpha and Beta now cross.

false (default)D2 source
trueD2 source

Spacing and padding​

In a left-to-right diagram, shapes in the same layer form a vertical column. Spacing values are minimum distances in pixels. Labels and routing can require more room.

Separate shapes in the same layer​

The three destinations of Queue share a column. Increasing elk-nodeNode from 20 to 100 increases the vertical gap between neighboring boxes from 20 to 100 pixels.

20 px (default)D2 source
100 pxD2 source

Move a passing edge away from a shape​

The direct edge from Start to Ship bypasses Check and Pack. Increasing elk-edgeNode moves that route farther below the two boxes.

40 px (default)D2 source
100 pxD2 source

Separate edges in the same layer​

Two edges run directly from Input to Save, passing under Check. With elk-edgeEdge set to 80, the lower edge bends away so the two routes have more space between them as they pass through the middle layer.

10 px (default)D2 source
80 pxD2 source

Separate shapes in adjacent layers​

Each stage in Read -> Build -> Ship occupies its own layer. Increasing elk-nodeNodeBetweenLayers from 70 to 180 adds 110 pixels between each pair of boxes along the direction of the pipeline.

70 px (default)D2 source
180 pxD2 source

Move bends away from adjacent shapes​

The two edges turn between the source column and Store. Increasing elk-edgeNodeBetweenLayers gives those vertical segments more space on both sides. Store moves farther right to make room.

40 px (default)D2 source
100 pxD2 source

Separate edges between layers​

Each of API, Web and Job connects to both destinations. Increasing elk-edgeEdgeBetweenLayers spreads out the vertical segments in the gap before East and West.

50 px (default)D2 source
125 pxD2 source

Add space inside a container​

Set the four sides of elk-padding with a string. Changing left and right from 50 to 110 adds 60 pixels at each end of Read -> Write, making the Worker container 120 pixels wider.

50 px on each side (default)D2 source
110 px left and rightD2 source

Move a self-loop away from its shape​

The retry edge leaves Worker and returns to it. Increase elk-nodeSelfLoop from 50 to 120 to move the top of the loop and its label farther above the box.

50 px (default)D2 source
120 pxD2 source

Shape placement​

Place a shape in the first or last layer​

Set elk-layer-constraint on the shape itself. With none, the default, ELK places Audit alongside Build. With direction: right, first puts Audit in the leftmost layer, so the arrow from Start points back to it. last moves Audit into the rightmost layer alongside Ship.

none (default)D2 source
firstD2 source
lastD2 source

Force layout​

Choose a force model​

elk-forceModel selects how each pair of shapes attracts or repels. With fruchterman_reingold, the default, the example stretches vertically from Store to Cache. With eades, it spreads out horizontally and brings Cache closer to Web.

fruchterman_reingold (default)D2 source
eadesD2 source

Adjust repulsion​

elk-forceRepulsion sets the strength of repulsion between shapes without a direct connection in the eades model. Here Parse and Send are two connections apart. Raising repulsion from 5 to 500 moves their centers from about 370 to 440 pixels apart and changes the bend in the chain.

5 (default)D2 source
500D2 source

Stress layout​

Set the target edge length​

elk-stressDesiredEdgeLength sets the target distance in pixels between the centers of directly connected shapes. Use a positive number. Changing 100 to 200 roughly doubles the distance from Client to API and from API to Store. The boxes keep their size, so the visible arrows grow longer.

100 (default)D2 source
200D2 source

Mr. Tree layout​

Mr. Tree leaves 20 pixels between shapes (elk-nodeNode) and uses 3 pixels of edge clearance (elk-edgeNode) by default.

Choose a spanning tree​

When a shape is reachable by several paths, elk-mrtreeSearchOrder decides how to build the tree that assigns levels. Depth-first search (dfs) follows Root -> API -> Cache -> Store, placing Store three levels out. Breadth-first search (bfs) finds the direct edge Root -> Store and places Store in the first level after Root. Both drawings use 40 pixels between shapes and 10 pixels of edge clearance so you can follow the shortcut.

dfs (default)D2 source
bfsD2 source

Order sibling branches​

elk-mrtreeWeighting controls sibling order. model_order follows declaration order. descendants uses the total number of shapes in each subtree, so the long Deep branch moves to the top. fan uses the largest number of descendants on any single level, so Wide, with three children side by side, moves to the top.

model_order (default)D2 source
descendantsD2 source
fanD2 source

Radial layout​

Space the rings​

elk-radialRadius sets the initial distance in pixels between tree levels. The default, 0, derives that distance from the largest shape. ELK can expand a ring to keep shapes clear of each other. Set 220 to give the three children of Hub a wider ring.

0 (default)D2 source
220D2 source

Divide the circle between branches​

elk-radialWedgeCriteria controls how much angular space each branch receives. node_size takes shape dimensions into account, including the wide Archive box. leaf_number counts leaves. The Live branch has two of the four leaves, so it gets half of the circle.

node_size (default)D2 source
leaf_numberD2 source

Bring radial branches closer​

elk-radialCompactor controls how radial layout uses spare space. none keeps the original rings. radial_compaction pulls each ring inward as a unit. wedge_compaction lets branches move inward independently. In this tree the branch ending at Four can move closer to Hub than the branch ending at Two.

none (default)D2 source
radial_compactionD2 source
wedge_compactionD2 source