#8292·mermaid

ELK: edge label left off its edge after straightenEdgeTerminals moves the run

Author: jjdeharoCreated Sep 22, 2026Updated Sep 28, 2026
LabelsStatus: Approvedv12.0.1

Description

With the ELK layout (the default in v12), an edge label can end up beside its edge instead of centred on it. It happens when the terminal jog of the edge is straightened: straightenEdgeTerminals moves the long run of the edge onto the port's row or column, but the label keeps the position ELK gave it on the original run.

In applyElkEdgeLayout, edge.x / edge.y are set from the ELK label first. Then straightenEdgeTerminals(data4Layout.edges) replaces edge.points without updating them. The run can move by up to TERMINAL_JOG_MAX (16 px), and the label stays behind by that amount.

Steps to reproduce

  1. Render the code below with Mermaid 12.0.0 and default config.
  2. Look at the first Yes label (edge B --> C).

It sits 3.1 px to the right of its line. In the SVG, the label has translate(69.09, 391.5), but the vertical run of L_B_C_0 is at x = 66. Every other label in the diagram is within 0.1 px of its edge. With elk: { straightenEdges: false } the label is back on the line (0.08 px), but the jog reappears. With other font settings the gap is larger: 6 px in our app.

Screenshots

The first Yes label sits to the right of its vertical line, while No and the second Yes are centred on theirs.

Flowchart rendered with Mermaid 12.0.0: the first Yes label sits to the right of its vertical line

Code Sample

flowchart TD
    A --> B{Decision}
    B -- Yes --> C[Done]
    B -- No --> D[Step 1]
    D --> E{Again?}
    E -- Yes --> C
    E -- No --> F[Stop]

Setup

  • Mermaid version: 12.0.0 (the same code is in develop as of 2026-09-22)
  • Browser and Version: Chromium 149.0.7827.55 and Firefox 151.0 (same result)

Suggested Solutions

When straightenEdgeTerminals accepts a candidate route, move the label with the run it sits on. The run moves perpendicular to its own axis, so if (edge.x, edge.y) lay on the original run, it can be projected onto the nearest segment of the new route.

Additional Context

This is not the same as #7341 / #8238, which is a vertical offset caused by subGraphTitleMargin. This one is perpendicular to the edge and comes from the straightening pass added in #8152.