How to make the specified nodes horizontally aligned in Mermaid?
Asked Answered
T

3

12

This is my flowchart:

graph TB
    aaa ---> bbb ---> ddd & ccc
    ddd -.-> fff
    ccc --->|eee| fff
    fff ---> hhh & ggg & kkk
    hhh --->|iii| mmm
    ggg & kkk -.-> mmm

enter image description here

But actually I want the node ccc and node ddd to be aligned horizontally. And the hhh , node ggg and kkk to be aligned horizontally. Is it possible?


Of course, the purpose of this post is to think about how to align these nodes horizontally, but if possible I would also like to make these edges as short as possible to save space while aligning them. For example, I want the node ddd to go up, not the node ccc to come down

Timorous answered 12/2, 2022 at 19:30 Comment(0)
B
26

You can achieve this by using longer or shorter arrows, which changes the minimum link length between nodes:

graph TB
    aaa ---> bbb ---> ddd & ccc
    ddd -..-> fff
    %%   ^^ extra .
    ccc --->|eee| fff
    fff ---> hhh & ggg & kkk
    hhh --->|iii| mmm
    ggg & kkk -..-> mmm
    %%         ^^ extra .

Flowchart with nodes aligned as OP wishes

Bravado answered 12/2, 2022 at 22:0 Comment(0)
V
6

According Minimum length of a link permalink

Each node in the flowchart is ultimately assigned to a rank in the rendered graph, i.e. to a vertical or horizontal level (depending on the flowchart orientation), based on the nodes to which it is linked. By default, links can span any number of ranks, but you can ask for any link to be longer than the others by adding "extra dashes" in the link definition.

so you don't need to triple dash

change

  • C2 ---> |msg| D to C2 --> |msg| D
  • E1 --->|msg| F to E1 -->|msg| F

Compare

<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.14.0/mermaid.min.js"></script>

<table>
<tr>
  <th>Bad</th>
  <th>Good</th>
</tr>

<tr>
  <td>
    <div class="mermaid">
      graph TB
      A --> B --> C1 & C2
      C1 -.-> D
      C2 ---> |msg| D
      D --> E1 & E2 & E3
      E1 --->|msg| F
      E2 & E3 -.-> F
    </div>
  </td>
  <td>
    <div class="mermaid">
      graph TB
      A --> B --> C1 & C2
      C1 -.-> D
      C2 --> |msg| D
      D --> E1 & E2 & E3
      E1 -->|msg| F
      E2 & E3 -.-> F
    </div>
  </td>  
</tr>
</table>
Vaginitis answered 18/2, 2022 at 7:49 Comment(0)
P
1

To add on to previous answers: You can also add invisible links.

flowchart LR
    A ~~~ B
Politian answered 9/8 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.