HOME/Articles/

如何使用 Graphviz - 快速上手

Article Outline

Graphviz 的一些範例

Dotted edges etc 虛線邊緣等等

This example uses a edge's attribute style to draw a dotted edge. 這個例子使用邊緣的屬性 style 來繪製虛線邊緣。

digraph D {

  A [shape=diamond]
  B [shape=box]
  C [shape=circle]

  A -> B [style=dashed, color=grey]
  A -> C [color="black:invis:black"]
  A -> D [penwidth=5, arrowhead=none]

}

形式:記錄 vs. 純文字

If the shape attribute is set to record, the text (as controlled with the label attribute) is layouted in tabular form. 如果 shape 屬性設置為 record ,則文字(由 label 屬性控制)以表格形式排列。

The vertical bar (|) starts a new row or column. 垂直欄( | )開始一個新的行或列。

The curly braces { ... } change (flip) the direction of |. 花括號 { ... } 可以改變(翻轉) | 的方向。

digraph D {

    node [fontname="Arial"];

    node_A [shape=record    label="shape=record|{above|middle|below}|right"];
    node_B [shape=plaintext label="shape=plaintext|{curly|braces and|bars without}|effect"];


}

Multiple edges 多重邊

Normally, a dependency (an edge) is declared with an arrow: Node1 -> Node2. Multiple nodes can be listed in curly braces, thus declaring multiple edges in one go. 通常,依賴關係(邊緣)是用箭頭聲明的: Node1 -> Node2 。多個節點可以在花括號中列出,從而一次聲明多個邊緣。

digraph D {

  A -> {B, C, D} -> {F}

}

Left, mid and right aligned text

左對齊、置中對齊和右對齊的文字

\l makes the preceding (!) text left aligned, \n makes it centered and \r makes it right aligned. \l 使前面的文字靠左對齊, \n 使其置中, \r 使其靠右對齊。

digraph L {

  node [shape=record fontname=Arial];

  a  [label="one\ltwo three\lfour five six seven\l"]
  b  [label="one\ntwo three\nfour five six seven"]
  c  [label="one\rtwo three\rfour five six seven\r"]

  a -> b -> c

}

Giving the graph a title

給圖表加上標題

With the label attribute, the graph can be given a caption: 使用 label 屬性,可以為圖表新增標題:

digraph D {

  label = "The foo, the bar and the baz";
  labelloc = "t"; // place the label at the top (b seems to be default)

  node [shape=plaintext]

  FOO -> {BAR, BAZ};


}

HTML like labels 類似 HTML 的標籤

If the text of label is enclosed in <…> rather than in "…", the text is treated as HTML and rendered as HTML like label. 如果 label 的文字被包含在 <…> 而不是 "…" 中,則該文字將被視為 HTML 並像標籤一樣呈現為 HTML。

digraph D {

  label = &lt;The &lt;font color='red'&gt;&lt;b&gt;foo&lt;/b&gt;&lt;/font&gt;,&lt;br/&gt; the &lt;font point-size='20'&gt;bar&lt;/font&gt; and&lt;br/&gt; the &lt;i&gt;baz&lt;/i&gt;&gt;;
  labelloc = "t"; // place the label at the top (b seems to be default)

  node [shape=plaintext]

  FOO -&gt; {BAR, BAZ};

}

rank 等級、排名、地位、階級

{ rank=same node_1 node_2 … } specifies that the specified nodes have the same rank, that is, that their distance from the top or left border is equal. { rank=same node_1 node_2 … } 指定了指定的節點具有相同的等級,也就是它們距離頂部或左邊界的距離相等。

digraph R {

  node [shape=record];

  { rank=same rA sA tA }
  { rank=same uB vB wB }


   rA -&gt; sA;
   sA -&gt; vB;
   t  -&gt; rA;
   uB -&gt; vB;
   wB -&gt; u;
   wB -&gt; tA;

}

Subgraph (clusters) 子圖(叢集)

digraph Q {

  node [shape=record];


  and_1   [label = "Node 1"];
  and_2   [label = "Node 2"];
  and_3_a [label = "Above Right Node 3"];
  and_3_l [label = "Left of Node 3"];
  and_3   [label = "Node 3"];
  and_3_r [label = "Right of Node 3"];
  and_4   [label = "Node 4"];


  and_3_a -&gt; and_3_r;
  and_1 -&gt; and_2 -&gt; and_3 -&gt; and_4;

  subgraph cluster_R {

    {rank=same and_3_l and_3 and_3_r}

    and_3_l -&gt; and_3 -&gt; and_3_r [color=grey arrowhead=none];

  }

}

Nested clusters 巢狀式群集

digraph D {

  subgraph cluster_p {
    label = "Parent";

    subgraph cluster_c1 {
      label = "Child one";
      a;

      subgraph cluster_gc_1 {
        label = "Grand-Child one";
         b;
      }
      subgraph cluster_gc_2 {
        label = "Grand-Child two";
          c;
          d;
      }

    }

    subgraph cluster_c2 {
      label = "Child two";
      e;
    }
  }
}

HTML table HTML 表格

HTML tables are produced if the value of the label attribute is enclosed in <…> rather than "…": 如果 label 屬性的值被包含在 <…> 而不是 "…" 中,則會產生 HTML 表格:

digraph H {

  aHtmlTable [
   shape=plaintext
   color=blue      // The color of the border of the table
   label=&lt;

     &lt;table border='1' cellborder='0'&gt;
       &lt;tr&gt;&lt;td&gt;col 1&lt;/td&gt;&lt;td&gt;foo&lt;/td&gt;&lt;/tr&gt;
       &lt;tr&gt;&lt;td&gt;COL 2&lt;/td&gt;&lt;td&gt;bar&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;

  &gt;];

}

Nested HTML table 巢狀的 HTML 表格

HTML tables can be nested HTML 表格可以巢狀

digraph {

  tbl [

    shape=plaintext
    label=&lt;

      &lt;table border='0' cellborder='1' color='blue' cellspacing='0'&gt;
        &lt;tr&gt;&lt;td&gt;foo&lt;/td&gt;&lt;td&gt;bar&lt;/td&gt;&lt;td&gt;baz&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td cellpadding='4'&gt;
          &lt;table color='orange' cellspacing='0'&gt;
            &lt;tr&gt;&lt;td&gt;one  &lt;/td&gt;&lt;td&gt;two  &lt;/td&gt;&lt;td&gt;three&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;four &lt;/td&gt;&lt;td&gt;five &lt;/td&gt;&lt;td&gt;six  &lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine &lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
        &lt;/td&gt;
        &lt;td colspan='2' rowspan='2'&gt;
          &lt;table color='pink' border='0' cellborder='1' cellpadding='10' cellspacing='0'&gt;
            &lt;tr&gt;&lt;td&gt;eins&lt;/td&gt;&lt;td&gt;zwei&lt;/td&gt;&lt;td rowspan='2'&gt;drei&lt;br/&gt;sechs&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;vier&lt;/td&gt;&lt;td&gt;fünf&lt;/td&gt;                             &lt;/tr&gt;
          &lt;/table&gt;
        &lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;&lt;td&gt;abc&lt;/td&gt;&lt;/tr&gt;

      &lt;/table&gt;

    &gt;];

}

Colors 顏色

digraph D {

  node [shape=plaintext]

  some_node [
   label=&lt;
     &lt;table border="0" cellborder="1" cellspacing="0"&gt;
       &lt;tr&gt;&lt;td bgcolor="yellow"&gt;Foo&lt;/td&gt;&lt;/tr&gt;
       &lt;tr&gt;&lt;td bgcolor="lightblue"&gt;&lt;font color="#0000ff"&gt;Bar&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
       &lt;tr&gt;&lt;td bgcolor="#f0e3ff"&gt;&lt;font color="#ff1020"&gt;Baz&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;&gt;
  ];



}

Rounded box 圓角盒子

digraph H {

  aHtmlTable [
   shape=plaintext
   label=&lt;

     &lt;table border='1' cellborder='0' style='rounded'&gt;
       &lt;tr&gt;&lt;td&gt;col 1&lt;/td&gt;&lt;td&gt;foo&lt;/td&gt;&lt;/tr&gt;
       &lt;tr&gt;&lt;td&gt;COL 2&lt;/td&gt;&lt;td&gt;bar&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;

  &gt;];

}

Ports 連接埠

digraph H {

  parent [
   shape=plaintext
   label=&lt;
     &lt;table border='1' cellborder='1'&gt;
       &lt;tr&gt;&lt;td colspan="3"&gt;The foo, the bar and the baz&lt;/td&gt;&lt;/tr&gt;
       &lt;tr&gt;&lt;td port='port_one'&gt;First port&lt;/td&gt;&lt;td port='port_two'&gt;Second port&lt;/td&gt;&lt;td port='port_three'&gt;Third port&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;
  &gt;];

  child_one [
   shape=plaintext
   label=&lt;
     &lt;table border='1' cellborder='0'&gt;
       &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;
  &gt;];

  child_two [
   shape=plaintext
   label=&lt;
     &lt;table border='1' cellborder='0'&gt;
       &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;
  &gt;];

  child_three [
   shape=plaintext
   label=&lt;
     &lt;table border='1' cellborder='0'&gt;
       &lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
     &lt;/table&gt;
  &gt;];

  parent:port_one   -&gt; child_one;
  parent:port_two   -&gt; child_two;
  parent:port_three -&gt; child_three;

}

Project Dependencies 專案相依性

digraph D {

  node [shape=plaintext fontname="Sans serif" fontsize="8"];

  task_menu [ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 1&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Choose Menu&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="darkgreen"&gt;done&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];

  task_ingredients [ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 2&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Buy ingredients&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="darkgreen"&gt;done&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];

  task_invitation [ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 4&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Send invitation&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="darkgreen"&gt;done&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];

  task_cook [ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 5&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Cook&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="red"&gt;todo&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];

  task_table[ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 3&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Lay table&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="red"&gt;todo&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];

  task_eat[ label=&lt;
   &lt;table border="1" cellborder="0" cellspacing="1"&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Task 6&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;Eat&lt;/td&gt;&lt;/tr&gt;
     &lt;tr&gt;&lt;td align="left"&gt;&lt;font color="red"&gt;todo&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
   &lt;/table&gt;&gt;];


  task_menu        -&gt; task_ingredients;
  task_ingredients -&gt; task_cook;
  task_invitation  -&gt; task_cook;
  task_table       -&gt; task_eat;
  task_cook        -&gt; task_eat;

}