Search code examples
umlclass-diagramplantuml

UML - How to align class diagrams with child classes verticaly


In the attached UML diagram are 5 classes and I want to find out how the top of classes A,B and C can be verticaly aligned while the child classes remain aligned. Please find my UML code and my screenshots below.

Tanks a lot for your support ! :)

What it looks currently like:

enter image description here

How it should look like (paint edited):

enter image description here

UML - code:

@startuml TestClassDiagram
scale 800 width
skinparam SameClassWidth true
skinparam ClassFontSize 15

class classA {
{field}  - attribute1  : int
{field}  - attribute2  : int
{method} + method1(void)
{method} + method2(void)
{method} + method3(void)
{method} + method4(void)
{method} + method5(void)
}

class classB {
{field}  - attribute1 : int
{field}  - attribute2 : int
{method} + method1(void)
{method} + method2(void)
}
class classBchild     {
{method} + method1(void)    
}

class classC {
{field}  - attribute1  : int
{field}  - attribute2  : int
{field}  - attribute3  : int
{field}  - attribute4  : int
{method} + method1(void)
{method} + method2(void)
{method} + method3(void)
{method} + method4(void)
{method} + method5(void)
}
class classCchild {
{method} + method1(void)   
}

classB <|-- classBchild
classC <|-- classCchild

@enduml

Solution

  • There isn't a feature to align classes in PlantUML (yet).

    If we add arrows between all elements, it is clear to see wht PlantUML is trying to do:

    It is simply aligning all classes in the diagram from the middle.

    using this, we can create a hack that sort of achieves the result you want by padding the class definition with extra newlines until they are all the same size:

    @startuml
    skinparam {
        SameClassWidth true
        ClassFontSize 15
    }
    
    class A as "classA" {
        {field}  - attribute1  : int
        {field}  - attribute2  : int
    
    
        __
        {method} + method1(void)
        {method} + method2(void)
        {method} + method3(void)
        {method} + method4(void)
        {method} + method5(void)
    }
    
    class B as "classB" {
        {field}  - attribute1 : int
        {field}  - attribute2 : int
    
    
        __
        {method} + method1(void)
        {method} + method2(void)
    
    
    
    }
    
    class Bc as "classBchild"     {
        {method} + method1(void)    
    }
    
    class C as "classC" {
        {field}  - attribute1  : int
        {field}  - attribute2  : int
        {field}  - attribute3  : int
        {field}  - attribute4  : int
    
        {method} + method1(void)
        {method} + method2(void)
        {method} + method3(void)
        {method} + method4(void)
        {method} + method5(void)
    }
    
    class Cc as "classCchild" {
        {method} + method1(void)   
    }
    
    B <|-- Bc
    C <|-- Cc
    @enduml