Search code examples
umlclass-diagramplantuml

How do you make a horizontal class aggregation representation with text in plantuml?


I want to have the following in plantuml. enter image description here

What I currently have is:

enter image description here

which was generated from

@startuml
Pond o-  "o...*" Swan
@enduml

But it doesn't look very good, the text is squished in. How can I make it look better? I've already consulted this page: http://plantuml.com/class-diagram, but I am not finding anything that useful.


Solution

  • The problem

    PlantUML does not handle horizontal layout very well. Maybe the examples used on the documentation page suggests that quality assurance tests might mostly be done with a vertical layout.

    With a vertical presentation, PlantUML keeps a line for cardinality on both sides. It reduces the width of cardinality to the minimum and places them in such a way not to cross the association line. So:

    Pond  "xxxxxx" o-- "o..*" Swan 
    

    results in:

    enter image description here

    The horizontal version adopts a similar strategy. Unfortunately, it doesn't consider the width of the cardinality as an indication of the minimum width of the association line. It neither reserves a minimum horizontal space on both sides for achieveing a decent output at least for the most common cases. So:

    Pond  "xxxxxx" o- "o..*" Swan 
    

    results in:

    enter image description here

    This is clearly a serious bug in the layout.

    Workaround

    PlantUML also allows to put a label on an association. Fortunately, the layout engine takes the length of this label into account, since it is supposed to be placed above the line. So you can help yourself by adding a blank label for padding:

    Pond  "xxxxx" o- "o..*" Swan : "               "
                                   ^
                                   THis label is only for padding
    

    Results in:

    enter image description here

    Of course this is not a great solution. It's only a practical workaround for an anoying bug.