Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-edges/loops look very awkward in state diagrams #6336

Open
hjfreyer opened this issue Feb 27, 2025 · 3 comments
Open

Self-edges/loops look very awkward in state diagrams #6336

hjfreyer opened this issue Feb 27, 2025 · 3 comments
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect

Comments

@hjfreyer
Copy link

Description

In state diagrams where a state connects to itself, it looks, well... ugly. Like the edge was about to point to a different node, but then realized it was heading in the wrong direction and doubled-back on itself.

Steps to reproduce

stateDiagram-v2
    [*] --> Node
    Node --> Node: Self Edge

Or, in the live editor.

Screenshots

Image

Code Sample


Setup

  • Mermaid version: v11.4.1
  • Browser and Version: Chrome Version 132.0.6834.208 (Official Build) (64-bit)

Suggested Solutions

Compare that to this graphviz digraph:

digraph G {
  start -> n;

  n [label= "Node"];
  n -> n [label = "Self Edge"];
}

Image

So much more natural. The salient differences are the fact that the edge is to the side of the node, and that it doesn't have the unfortunate kink in it.

Additional Context

Issue #1443 may be related. It's complaining about rendering of self-edges, but it's less clear to me what the specific complaint is.

@hjfreyer hjfreyer added Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect labels Feb 27, 2025
@sfarina
Copy link

sfarina commented Feb 27, 2025

I came here to post about this issue as well. It gets wild if you're trying to illustrate a finite state machine:

stateDiagram-v2
  A --> B
  B-->B
  B -->A
  A --> C
  C --> C
  C --> B
  C --> A

Image

@BambioGaming
Copy link

BambioGaming commented Feb 28, 2025

Hello,

I completely agree that self-loops in Mermaid state diagrams currently do not render as cleanly as they do in Graphviz digraphs. The issue appears to stem from how self-loops are treated like standard edges rather than being explicitly optimized for circular positioning.

Possible Issues in the Current Implementation

1. Lack of Explicit Self-Loop Handling

  • The stateDb.js processing functions that do not differentiate self-loops from regular transitions.
  • As a result, the self-loop is drawn as a reversed transition rather than a properly curved arc.

2. Edge Routing & Layout Constraints

  • The automatic layout engine does not prioritize self-loops, causing them to be positioned awkwardly.
  • Graphviz, by contrast, uses explicit loop placement rules that result in smoother loops.

3. Styling & Customization Limitations

  • The setStyle() function in stateDb.js does not provide options for defining self-loop curvature or positioning.
  • Unlike Graphviz, Mermaid does not support loop-specific attributes like headport or tailport to adjust where the loop attaches to the node.

Possible Fixes & Enhancements

  • Introduce explicit self-loop detection, ensuring that self-loops are styled separately from regular edges.
  • Add support for loop curvature controls in setStyle(), similar to how Graphviz allows adjustments.
  • Implement edge anchor customization (e.g., allowing users to specify where the self-loop attaches to the node).

Would love to hear your thoughts.

@nour0205
Copy link

nour0205 commented Feb 28, 2025

Hello @hjfreyer

I have also tested self-loops across multiple diagram types and confirm that the issue affects state diagrams class diagrams and flowcharts, where loops are misaligned, overlapping, or bending unnaturally even when forcing a horizontal layout.

However, like you mentioned for graphviz, sequence diagrams also render self-loops correctly, which requires further investigation.

Screenshots for Reference:

Image

Image

  • Correct self-loops in sequence diagrams
  • Misaligned self-loops in state diagrams and flowcharts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect
Projects
None yet
Development

No branches or pull requests

4 participants