Search code examples
umlplantumlstate-diagram

How to rearrange blocks in a UML state diagram


I just created a state diagram and right now I am struggling with the positions of the blocks. My goal is to have States 1,2,4,5 in a "down"-row and state 3 at the right from state 1.

See my UML code and the attached screenshot for the current (left) and wanted (right) design

UML Code:

@startuml diagramStackO
scale 400 width
[*]    -down->  State1
State1 -down->  State2
State1 -right-> State3
State2 -up->    State3
State2 -down->  State4
State4 -->      State5
State5 -->      [*]
@enduml

Screenshot (left: current right: what it should look like)

enter image description here


Solution

  • My first advice would be to just ignore it and move on... Tweaking diagrams in PlantUML to get them "just right", even for little things, can take up a lot of time.

    However, as someone who spends way too much time in getting their diagrams just right, there are two things that come to mind.

    Using ortho

    The first one is to just add skinparam linetype ortho. This is the least work but also gives the least improvment:

    @startuml
    skinparam linetype ortho
    
    [*]    -down->  State1
    State1 -down->  State2
    State1 -right-> State3
    State2 -up->    State3
    State2 -down->  State4
    State4 -->      State5
    State5 -->      [*]
    @enduml
    

    Using a hidden container

    The other is adding a hidden container which a bit more work, but not too much, I think.

    To get the vertical alignment that you want, the states need to be grouped together:

    All we need to do now is hide the container:

    The code I used to achieve this effect is:

    @startuml
    skinparam {
        shadowing false
    
        state {
            BackgroundColor<<HIDDEN>> hidden
            BorderColor<<HIDDEN>> hidden
        }
    }
    
    
    state " " as _ <<HIDDEN>> {
        state State1
        state State2
        state State4
        state State5
    }
    
    
    [*]    -down->  State1
    State1 -down->  State2
    State1 -right-> State3
    State2 -right->    State3
    State2 -down->  State4
    State4 -->      State5
    State5 -->      [*]
    @enduml