C4-PlantUML

repository·master·Indexed 27 days ago

https://github.com/plantuml-stdlib/c4-plantuml

A library for PlantUML that implements the C4 model for creating standardized software architecture diagrams. It provides specialized macros and styling for System Context, Container, Component, and Deployment diagrams, as well as support for Dynamic and Sequence diagrams. The library includes tools for defining relationships, managing layouts with Lay_* commands, and decorating elements with sprites, links, and tags.

Tokens
17.6K
Snippets
51
Records
84
Agent score
93%

What's inside C4-PlantUML

  1. Create C4 Model Core Diagrams

    master

    The C4-PlantUML library allows you to create the core C4 model diagrams: System Context, Container, and Component diagrams. These diagrams follow the standard C4 modeling levels to describe software architecture at different levels of abstraction.

    Available diagram types in this library include:

    Core Diagrams:

    • System Context Diagram: Shows the system in the context of its users and other systems.
    • Container Diagram: Zooms into the system to show its high-level technical building blocks (containers).
    • Component Diagram: Zooms into an individual container to show its internal components.

    Supplementary Diagrams:

    • System Landscape Diagram
    • Dynamic Diagram
    • Sequence Diagram
    • Deployment Diagram
  2. Add custom tags and stereotypes to elements

    master

    You can introduce new visual styles by defining custom tags. These tags can be applied to elements using the $tags argument and will automatically appear in the diagram legend.

    Key Rules:

    • Use $tags="tagname" (no space around =).
    • Combine multiple tags using + (e.g., $tags="v1.0+v1.1").
    • If two tags define the same skinparam, the first definition is used. To merge specific properties, define a combined tag using & in the name (e.g., AddElementTag("v1.0&v1.1", ...)).
    • Avoid using commas , in tag names.
    • Use SHOW_LEGEND() as the last line of your diagram to display custom tags.
  3. Enable rounded rectangle style

    master

    To use rounded rectangles for all elements without changing the existing color scheme, set the !ROUNDED_STYLE variable to 1.

    Important: This statement must be placed BEFORE any !include statements for C4 files.

    @startuml
    ' Must be set before any C4_* file is included
    !ROUNDED_STYLE=1
     
    !include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml
    
    Person(admin, "Administrator")
    System(twitter, "Twitter")
    
    Rel(admin, twitter, "Uses")
    @enduml
  4. Enable support for additional PlantUML elements

    master

    To use advanced PlantUML shapes (like actor, cloud, package, etc.) with correct C4 styling, you must explicitly enable them. If not enabled, these elements will appear in the diagram but will lack the correct C4 visual styles.

    You can enable this feature using one of two methods:

    1. In the PlantUML script: Set !ENABLE_ALL_PLANT_ELEMENTS = 1 at the top of your file, before any C4 library files are included.
    2. Via Command Line: Pass the parameter -DENABLE_ALL_PLANT_ELEMENTS=1 when running PlantUML.
    @startuml
    !ENABLE_ALL_PLANT_ELEMENTS = 1
    !include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Component.puml
    
    Component(comp, "Copy component")
    Component(config, "Config component", $baseShape="package")
    
    ComponentDb(dbA, "DB A")
    ' alternative syntax for ComponentDb() with $baseShape="database"
    Component(dbB, "DB B", $baseShape="database")
    
    Rel_U(comp, config, "Configured by")
    Rel_L(comp, dbA, "Reads from")
    Rel_R(comp, dbB, "Writes to")
    
    SHOW_LEGEND()
    @enduml
  5. Create custom C4-PlantUML themes

    master

    You can create custom themes on your local file system by duplicating an existing theme. By default, theme files should be named puml-theme-C4_foo.puml (where C4_foo is your theme name).

    Unlike standard PlantUML themes that use only skinparam or style, a C4-PlantUML theme must also overwrite specific color and font variables to ensure all elements (including the legend) are updated correctly.

  6. Apply C4-PlantUML themes

    master

    C4-PlantUML themes follow the naming convention C4_... to ensure they include necessary C4 variable definitions. The !theme statement must be placed BEFORE any !include statements.

    You can invoke themes from remote repositories, local paths, or using C4-Stdlib/calculated paths (for PlantUML v1.2023.8+).

    ' 1. Invoke a remote theme
    !theme C4_united from https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/themes
    
    ' 2. Invoke a local theme
    !theme C4_foo from /path/to/themes/folder
    
    ' 3. Use C4-Stdlib (PlantUML v1.2023.8+)
    !theme C4_united from <C4/themes>
    
    ' 4. Use calculated paths (PlantUML v1.2023.8+)
    !RELATIVE_INCLUDE = "https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master"
    !theme C4_united from %get_variable_value("RELATIVE_INCLUDE")/themes
    
    !include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml
    
    Person(admin, "Administrator")
    @enduml
  7. Configure PlantUML for local C4 library usage

    master

    If you are using local copies of the C4 library files to work offline, you must tell PlantUML to look in the current directory for includes.

    Command Line: Pass the -DRELATIVE_INCLUDE="." argument to the java -jar plantuml.jar command.

    Visual Studio Code: Add the following to your settings.json to enable local includes in the PlantUML extension.

    java -jar plantuml.jar -DRELATIVE_INCLUDE="." ...
    "plantuml.jarArgs": [
      "-DRELATIVE_INCLUDE=."
    ]
  8. Configure custom sprites, images, and OpenIconic

    master

    You can use the $sprite parameter to display custom images or icons. C4-PlantUML supports several formats:

    • Standard Library Sprites: Use the syntax {SpriteName} (requires the sprite to be included via !include).
    • External Images: Use the syntax img:{File or Url}.
    • OpenIconic: Use the syntax &{OpenIconicName}.

    Customization Options:

    • Scale: Adjust size using ,scale={factor} (e.g., ,scale=0.5).
    • Color: Change color using ,color={color}.
  9. Apply sprites to relationships

    master

    When defining relationships (e.g., Rel, Rel_L, Rel_R), you can use the $sprite parameter to add icons to the connection lines. Because relationship icons are often smaller, you may need to use scaling or specific small sprites.

    Techniques for relationships:

    • Scaling: Use $sprite="name,scale=0.5" to prevent icons from being too large.
    • Variables: Store complex sprite strings in a variable (e.g., !$mySprite="name,scale=0.5") and pass it via $sprite=$mySprite.
    • OpenIconic: If the sprite string starts with &, it is treated as an OpenIconic name (e.g., $sprite="&envelope-closed").
    • Custom Sprites: You can define your own small sprites using the sprite keyword.
  10. Include the C4-PlantUML library

    master

    To use C4 modeling, you must include one of the core library files at the top of your .puml file. Choose the level of detail required for your diagram:

    1. Standard Library (Recommended): Uses the built-in PlantUML C4 library (no internet required).
    2. Remote GitHub (Always up-to-date): Fetches the latest version from GitHub (requires internet).
    3. Local Files (Offline/Custom): Use files downloaded to your local machine to ensure independence from internet connectivity.