2024-03-22

Mastering Graphs with DOT: A Short Guide to Diagram Creation

The DOT language is a plain text graph description language that is part of the Graphviz software suite. It’s designed to be both human-readable and machine-processable, making it an ideal choice for generating diagrams programmatically.

When you write a DOT file, you define the nodes and edges of your graph, along with any desired attributes such as shapes, colors, and labels. Tools like Graphviz then process the DOT file to produce a visual representation of the graph in various formats, including PNG, PDF, and SVG.

Basic Syntax

A DOT file begins with the keyword digraph for directed graphs or graph for undirected graphs, followed by a block of statements enclosed in curly braces {}. Each statement describes nodes, edges, or attributes and ends with a semicolon ;.

Nodes: Nodes are the fundamental units in a graph. You can define a node simply by naming it:

   A;

You can also set attributes for nodes using square brackets []. For example, to label a node or change its shape:

   A [label="Node A", shape=circle];

Edges: Edges represent connections between nodes. In directed graphs, use -> to show direction; in undirected graphs, use --.

   A -> B;

Like nodes, edges can also have attributes:

   A -> B [label="Edge from A to B", color=red];

Attributes: Attributes customize the appearance of nodes and edges. Common attributes include label, color, shape, and style. Attributes are defined within square brackets and separated by commas.

Subgraphs: Subgraphs define a subset of nodes and edges within a graph. They are useful for clustering related elements together.

   subgraph cluster_0 {
     label="Cluster 0";
     A;
     B;
     A -> B;
   }

Creating DOT diagrams from source code

Creating DOT diagrams from source code can be achieved through various methods, each catering to different preferences and workflows:

1. Command Line Graphviz: Utilizing Graphviz directly from the command line is a straightforward approach. After writing your DOT source code, you can use the dot command to process the file and generate a diagram in your desired format, such as PNG or SVG.

Graphviz on Windows, compiled with gcc, can be installed with:

   pacman -S mingw-w64-x86_64-graphviz                

2. Pintora Command Line and Node Script: For those who prefer JavaScript, Pintora offers a command-line interface and can be integrated into Node.js scripts. This allows you to write DOT code within your JavaScript files and generate diagrams programmatically, which is especially useful for dynamic data.

To install Pintora as npm:

   npm install pintora

Pintora command line can be also used in a PowerShell terminal with:

   pintora.ps1 render -i mydiagram.dot

3. Embedding in PDF with Typst and GVIZ: If you’re looking to include DOT diagrams in a PDF document, you can use the Typst typesetting system along with GVIZ package. This combination enables you to embed DOT diagrams directly into your PDFs, ensuring they are part of the document flow.

4. Remarkpy Desktop Application: Remarkpy, a Markdown presentation tool for Windows, allows you to incorporate DOT diagrams into presentations. With Remarkpy, you can write your DOT code and generate presentations that include your diagrams, making it a great tool for educational and professional purposes.

The full markdown version of this presentation is available on BuyMeACoffee