---
# System prepended metadata

title: Graphviz 筆記
tags: [dot, graphviz]

---

---
title: "Graphviz 筆記"
path: "Graphviz 筆記"
---

{%hackmd @RintarouTW/About %}

# Graphviz 筆記

## DOT Language

- context based
- keywords: $node, edge, graph, digraph, subgraph$
- comment: /* */ and //

```
graph/digraph/cluster/subgraph {
  rankdir=LR
  rank="same"
  node
  edge
}
```

### Subgraphs and Clusters

A -> {B C} $\iff$ A->B A->C

**strict** forbids muti-edges.

<center>

```graphviz
strict graph { 
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
  a -- b
  a -- b
  b -- a
} 
```
</center>

If the name of the subgraph begins with **cluster**, Graphviz notes the subgraph as a special cluster subgraph with the bounding box.

## Styling

Use the $graph\_attr, node\_attr, and edge\_attr$ arguments to change the default appearance of your graph, nodes, and edges.

```
node {
  shape=plaintext|box|star|egg|rarrow
  style=filled
  color=COLOR_NAME
  label="text"
}

edge {
  arrowhead=vee|
  arrowsize=2 
  style=filled
  color=COLOR_NAME
  label="text"
}
```

### Node Shape, Edge, Label, Color

<center>

```graphviz
digraph "pet-shop" {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"]; 
    graph [rankdir=LR]
    node [shape=plaintext]
    edge [arrowhead=vee arrowsize=2]
    parrot
    dead
    parrot -> dead
}
```
</center>

```
digraph "pet-shop" {
    graph [rankdir=LR]
    node [shape=plaintext]
    edge [arrowhead=vee arrowsize=2]
    parrot
    dead
    parrot -> dead
}
```

<center>

```graphviz
graph ni {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
    node [shape=rarrow]
    1 [label="Ni!"]
    2 [label="Ni!"]
    3 [label="Ni!" shape=egg]
    node [shape=star]
    4 [label="Ni!"]
    5 [label="Ni!"]
    rankdir=LR
    1 -- 2
    2 -- 3
    3 -- 4
    4 -- 5
}
```
</center>

```
graph ni {
    node [shape=rarrow]
    1 [label="Ni!"]
    2 [label="Ni!"]
    3 [label="Ni!" shape=egg]
    node [shape=star]
    4 [label="Ni!"]
    5 [label="Ni!"]
    rankdir=LR
    1 -- 2
    2 -- 3
    3 -- 4
    4 -- 5
}
```

<center>

```graphviz
digraph nodestyling {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
  node [color=lightblue2 style=filled]
  rankdir=LR
  1 -> 2
  node [shape=egg]
  2 -> 3
}
```
</center>


```
digraph nodestyling {
  node [color=lightblue2 style=filled]
  rankdir=LR
  1 -> 2
  node [shape=egg]
  2 -> 3
}
```


### Backslash & Multiline

The Graphviz layout engine supports a number of escape sequences such as \n, \l, \r (for multi-line labels: centered, left-justified, right-justified) and \N, \G, \L (expanded to the current node name, graph name, object label).

<center>

```graphviz
digraph {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
    backslash [label="\\"]
    multi_line [label="centered\nleft\lright\r"]
}
```
</center>


```
digraph {
    backslash [label="\\"]
    multi_line [label="centered\nleft\lright\r"]
}
```

<center>

```graphviz
digraph {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
    spam -> "eggs eggs"
    "node" -> "\"here's a quote\""
}
```
</center>

```
digraph {
    spam -> "eggs eggs"
    "node" -> "\"here's a quote\""
}
```

<center>

```graphviz
graph parent {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
    spam -- eggs
    subgraph child {
        node [shape=box]
        foo -- bar
    }
}
```
</center>

```
graph parent {
    spam -- eggs
    subgraph child {
        node [shape=box]
        foo -- bar
    }
}
```

<center>

```graphviz
digraph {
graph [bgcolor=transparent;fontcolor="#888888";color="#888888"];
node [fontcolor="#888888";color="#888888"];
edge [fontcolor="#888888";color="#888800"];
    "King Arthur" -> {
        "Sir Bedevere", "Sir Lancelot"
    }
    "Sir Bedevere" -> "Sir Lancelot" [constraint=false]
}
```
</center>


```
digraph {
    "King Arthur" -> {
        "Sir Bedevere", "Sir Lancelot"
    }
    "Sir Bedevere" -> "Sir Lancelot" [constraint=false]
}
```

## Node Shape Types

https://www.graphviz.org/doc/info/shapes.html

## Arrow Types

https://www.graphviz.org/doc/info/attrs.html#k:arrowType

## Node, Graph, Edge Attributes

https://www.graphviz.org/doc/info/attrs.html

###### tags: `graphviz` `dot`