Drawing diagrams with PlantUML
Flow Diagrams
Flow diagrams show the several steps that make a task or algorithm and can be used for anything from statistical analyses to cake recipes. Figure 6, which is taken from the PlantBuddy GitHub site [4], shows a PlantUML flow diagram. While the language in Figure 6's source code (Listing 6) is intuitive, you need to know how to write a while
cycle (lines 5 and 13) and how to use markers to delimit each phase of the flow. You use a colon at the beginning of a phase and semicolon at the end.
Listing 6
Flow Diagrams
01 @startuml 02 03 :read moisture level; 04 :read humidity + temperature; 05 while (sensor readings) is (invalid) 06 :wait 2s; 07 :read moisture level; 08 :read humidity + temperature; 09 :increase counter; 10 if (MAX_READING_RETRIES reached) then (yes) 11 :send controller to deep sleep; 12 endif 13 endwhile (valid) 14 :submit sensor data; 15 16 @enduml
Diagram Formatting
UML and PlantUML offer many options for controlling text formatting, as well as style and appearance.
According to the manual [5], the scale
command enlarges the generated image in several ways, including but not limited to these options:
scale 1.5 scale 200 width scale 1400*700
However, this option did not work as documented on several of my diagrams.
To format text, PlantUML supports both basic HTML tags like bold and underline, and their equivalents in Creole format, such as:
@startuml This is **bold** This is //italics// This is ""monospaced"" This is --stroked-- This is __underlined__ This is ~~waved~~
The most important formatting command in PlantUML, skinparam
(see [5] for its many attributes), lets you set fonts, background and text color, title properties, and much more. As an example, you can customize UML diagrams with skinparam
to create real templates as shown in Listing 7.
Listing 7
skinparam Customizations
01 skinparam backgroundColor transparent 02 skinparam monochrome true 03 04 skinparam titleBorderRoundCorner 15 05 skinparam titleBorderThickness 2 06 skinparam titleBackgroundColor Aqua-CadetBlue 07 08 skinparam classFontColor red 09 skinparam classFontSize 10 10 skinparam classFontName Aapex
skinparam
's only drawback is that the more you use it, the less portable your diagrams may become. This is especially true with fonts, which may not be available on all systems that will generate diagrams from your UML code.
Diagram Metadata
Besides the actual diagram, UML files can define several widgets that give context, structure, and generally make the diagrams themselves easier to understand. For brevity, Figure 7 shows all of these elements, plus it introduces a few other PlantUML features. The source code for Figure 7 is shown in Listing 8.
Listing 8
Adding Widgets
01 @startuml 02 caption Figure 1 Caption 03 title 04 <size:30><&bullhorn>Use case UML diagram<&star></size> 05 endtitle 06 07 header 08 header (not title) goes here 09 endheader 10 11 center footer You can also have a footer 12 13 legend right 14 This example shows 15 how you can add 16 <u><b>"meta-information"</b></u> 17 to any diagram 18 endlegend 19 20 left to right direction 21 skinparam packageStyle rectangle 22 actor customer 23 actor clerk 24 rectangle checkout { 25 customer -- (checkout) 26 (checkout) .> (payment) : include 27 (help) .> (checkout) : extends 28 (checkout) -- clerk 29 } 30 @enduml
In Listing 8, the actual diagram is shown in lines 20 to 29; again, you can easily understand the source code by comparing it carefully with the drawing. Lines 2 to 18 demonstrate how to define all the widgets I mentioned, from header and footer to title, legend, and captions. Line 4 requires an explanation. While the size:30
statement is self-explanatory, the &bullhorn
and &star
keywords tell PlantUML to insert the corresponding icons from the Open Iconic graphic library [6] (integrated in PlantUML) in their place.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.