C4-PlantUML
repository·master·Indexed 27 days ago
https://github.com/plantuml-stdlib/c4-plantumlA 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.
What's inside C4-PlantUML
Create C4 Model Core Diagrams
masterThe 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
Add custom tags and stereotypes to elements
masterYou can introduce new visual styles by defining custom tags. These tags can be applied to elements using the
$tagsargument 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.
- Use
Enable rounded rectangle style
masterTo use rounded rectangles for all elements without changing the existing color scheme, set the
!ROUNDED_STYLEvariable to1.Important: This statement must be placed BEFORE any
!includestatements 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") @endumlEnable support for additional PlantUML elements
masterTo 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:
- In the PlantUML script: Set
!ENABLE_ALL_PLANT_ELEMENTS = 1at the top of your file, before any C4 library files are included. - Via Command Line: Pass the parameter
-DENABLE_ALL_PLANT_ELEMENTS=1when 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- In the PlantUML script: Set
Create custom C4-PlantUML themes
masterYou 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(whereC4_foois your theme name).Unlike standard PlantUML themes that use only
skinparamorstyle, a C4-PlantUML theme must also overwrite specific color and font variables to ensure all elements (including the legend) are updated correctly.Apply C4-PlantUML themes
masterC4-PlantUML themes follow the naming convention
C4_...to ensure they include necessary C4 variable definitions. The!themestatement must be placed BEFORE any!includestatements.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") @endumlUse C4-PlantUML snippets in Visual Studio Code
masterYou can use C4-PlantUML snippets in VS Code by including theC4.code-snippetsfile in your project's.vscodefolder. This requires the PlantUML extension for VS Code.Configure PlantUML for local C4 library usage
masterIf 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 thejava -jar plantuml.jarcommand.Visual Studio Code: Add the following to your
settings.jsonto enable local includes in the PlantUML extension.java -jar plantuml.jar -DRELATIVE_INCLUDE="." ..."plantuml.jarArgs": [ "-DRELATIVE_INCLUDE=." ]Configure custom sprites, images, and OpenIconic
masterYou can use the
$spriteparameter 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}.
- Standard Library Sprites: Use the syntax
Apply sprites to relationships
masterWhen defining relationships (e.g.,
Rel,Rel_L,Rel_R), you can use the$spriteparameter 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
spritekeyword.
- Scaling: Use
Include the C4-PlantUML library
masterTo use C4 modeling, you must include one of the core library files at the top of your
.pumlfile. Choose the level of detail required for your diagram:- Standard Library (Recommended): Uses the built-in PlantUML C4 library (no internet required).
- Remote GitHub (Always up-to-date): Fetches the latest version from GitHub (requires internet).
- Local Files (Offline/Custom): Use files downloaded to your local machine to ensure independence from internet connectivity.