ELK
ELK is a mature layout engine, actively maintained by an academic research group at Christian Albrechts University in Kiel.
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.
| Option | Default | What it controls |
|---|---|---|
elk-algorithm | layered | The algorithm that arranges the diagram. |
elk-forceModel | fruchterman_reingold | How attraction and repulsion place shapes. |
elk-forceRepulsion | 5 | How strongly unconnected shapes repel each other in the eades model. |
elk-stressDesiredEdgeLength | 100 | Target distance between the centers of connected shapes in stress layout. |
elk-mrtreeSearchOrder | dfs | Which paths determine the levels in Mr. Tree. |
elk-mrtreeWeighting | model_order | The order of branches within a tree level. |
elk-radialRadius | 0 | Initial spacing between rings in a radial tree. |
elk-radialWedgeCriteria | node_size | Each branch's share of the circle. |
elk-radialCompactor | none | How 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.
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.
| Option | Default | What it controls |
|---|---|---|
elk-mergeEdges | false | Shared entry and exit points. |
elk-edgeRouting | orthogonal | Right-angle or diagonal segments. |
elk-feedbackEdges | false | The path an edge takes back through a cycle. |
elk-wrappingStrategy | off | Where a pipeline can wrap onto another row. |
elk-forceNodeModelOrder | false | The order of top-level shapes within a layer. |
elk-nodeNode | 20 | Gap between shapes in the same layer. |
elk-edgeNode | 40 | Clearance between a shape and an edge passing through its layer. |
elk-edgeEdge | 10 | Gap between edges passing through the same layer. |
elk-nodeNodeBetweenLayers | 70 | Gap between shapes in adjacent layers. |
elk-edgeNodeBetweenLayers | 40 | Clearance between a bend and the shapes in adjacent layers. |
elk-edgeEdgeBetweenLayers | 50 | Gap between parallel edge segments between layers. |
elk-padding | 50 on each side | Space inside a containerβs border. |
elk-nodeSelfLoop | 50 | Gap between a shape and its self-loop. |
elk-layer-constraint | none | The 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.