【DOT】DOT言語 ~ 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/06/15/004815

の続き。

今回は、DOT言語の使えそうな文法について、
ちょこちょこ書き溜めていく

なお、以下のPlayground で触りながらやるのが一番わかりやすい

Playground
http://magjac.com/graphviz-visual-editor/

目次

【0】使用上の注意
【1】コメントアウト
【2】Rank
 1)rankdir
 2)rank
【3】Node/Edgeのレイアウト
 1)shape
 2)style
 3)color
 4)label
【4】graph
 1)layout
 2)nodesep
 3)ranksep
 4)margin
 5)splines
【5】subgraph
 1)compound

【0】使用上の注意

* 基本、「先勝ち」で先頭のものから順に反映される
 => 反映されない、もしくは、意図した反映にならない等があった場合は
  反映したものを前後させてTry&Errorしてみるといいかも。

【1】コメントアウト

    //コメント
    /*コメント*/
    #コメント

【2】Rank

* 以下のサイトが分かりやすい

https://qiita.com/miyase256/items/c3954bb51aa23a3ad13a

* 「【4】graph」の「3)ranksep」も参照しておいたほうがいい

1)rankdir

* グラフの向き

https://graphviz.org/docs/attrs/rankdir/

digraph {
  // TB:Top to bottom / LR: Left to right / RL: Right to left
  rankdir="TB"
  a -> b -> c;
}

2)rank

* Nodeの位置(ランク)を設定
 + rank="min" / rank="source"
 + rank="same"
 + rank="max" / rank="sink"

https://graphviz.org/docs/attrs/rank/

digraph {
    ranksep=0.3;
    nodesep=0.7;
    A; B; C; D; E;
    {rank = source; A; C;}
    {rank = same; B; D;}
    A -> B -> C;
    D -> E;
    B -> D;
}

【3】Node/Edgeのレイアウト

1)shape

* Node(節)の見た目の形を指定

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

a1[shape="ellipse"];
// box/circle/box3d/note/cylinder/component etc

2)style

* Node(節)/ Edge(線)のスタイルを指定

https://graphviz.org/docs/attr-types/style/

* どんな種類があるかは、以下の公式ドキュメントを参照
 => e.g. bold(太字)、dashed(点線)、invis(invisible=非表示) etc

https://graphviz.org/docs/attr-types/style/

t1[shape = box, style = "dashed,rounded,filled"];

3)color

* Node(節)/ Edge(線) の色を指定

https://graphviz.org/docs/attrs/color/

* どんな種類があるかは、以下の公式ドキュメントを参照
 => e.g. darkorange1 etc.

https://graphviz.org/doc/info/colors.html

t1[shape = box, color = "#9BD4A3"];

t2[shape = box, style = "dashed,rounded,filled" color="lightpink"];

4)label

* 表示文字列の指定

https://graphviz.org/docs/attrs/label/
https://graphviz.org/doc/info/shapes.html#record

http://magjac.com/graphviz-visual-editor/

digraph {
    node [shape=Mrecord];
    a1 [label="<f0> left|<f1> mid&#92; dle|<f2> right"];
    a2 [label="<f0> one|<f1> two"];
    a3 [label="hello&#92;nworld |{ b |{c|<here> d|e}| f}| g | h"];
    a1:f1 -> a2:f0;
    a1:f2 -> a3:here;
}

【4】graph

https://graphviz.org/docs/graph/

1)layout

graph{
  graph[layout=<レイアウト>] # e.g. dot, osage ...
  ...
}

# 以下のサイト参照。

http://www.yosbits.com/wordpress/?page_id=4845

2)nodesep

* Node間の最小余白
* type: double, default: 0.25, minimum: 0.02

https://graphviz.org/docs/attrs/nodesep/

3)ranksep

* ランク間の幅を指定

https://graphviz.org/docs/attrs/ranksep/

digraph {
    ranksep=0.3;
    nodesep=0.7;
    A; B; C; D; E;
    {rank = source; A; C;}
    {rank = same; B; D;}
    A -> B -> C;
    D -> E;
    B -> D;
}

4)margin

* マージン(余白の設定)

5)splines

* Node間のEdgeの表現の仕方
cf. spline(スプライン) = 薄く細長い一片
 => 以下の公式ドキュメントの例を見るのが一番早い

https://graphviz.org/docs/attrs/splines/

【4】subgraph

digraph {
  subgraph cluster0 {
     a  -> b
  }
  subgraph cluster1 {
     A -> B
  }
}

https://hackmd.io/@maxy8821/HkO3cjzQr#subgraph-cluster%E2%97%8B%E2%97%8B

1)compound

* Edge(線)がクラスタ(subgraph)に繋げるかどうか

https://graphviz.org/docs/attrs/compound/


http://magjac.com/graphviz-visual-editor/

digraph G {
  compound=true; # 上のURLPlayGrandtrue/falseを切り替えればわかる
  node [ shape = "Mrecord" ];
  subgraph cluster_0 {
    style = rounded;
    label = "Zero";
    Node_0_0 -> Node_0_1;
  };
  subgraph cluster_1 {
    style = rounded;
    label = "One";
    Node_1_0 -> Node_1_1;
  };
  subgraph cluster_2 {
    style = rounded;
    label = "Two";
    Node_2_0 -> Node_2_1;
  };
  NodeA [ style = rounded ];
  NodeA -> Node_0_0 [lhead = cluster_0];
  NodeA -> Node_1_0 [lhead = cluster_1];
  NodeA -> Node_2_0 [lhead = cluster_2];
}

関連記事

DOT言語 ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/15/004815
DOT言語 ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/07/18/120407
DOT言語表示ツール ~ d3-graphviz
https://dk521123.hatenablog.com/entry/2023/06/18/102448
Python + DOT言語で図作成するには
https://dk521123.hatenablog.com/entry/2023/06/14/174104
Python で d3-graphviz を使ってアニメーション表示ツールを作る
https://dk521123.hatenablog.com/entry/2023/06/24/000000
sqlparser-rs ~ SQL Parser for Rust ~
https://dk521123.hatenablog.com/entry/2023/06/12/000000