Help us understand the problem. What is going on with this article?

Graphvizで図を描く

More than 3 years have passed since last update.

http://www.graphviz.org/Home.php

DOT言語でグラフを記述して、レンダリングできる。
グラフでなくても、ちょっとした図を描くときに便利。

digraph G {
    a -> b -> c;
    b -> d;
}

コマンド

インストール

macだったのでHomeBrewでインストールできた。

brew install graphviz

画像に変換

gccでC言語のソースをコンパイルするような感じ

#hoge.dotをhoge.pngに変換して開く
dot -Tpng hoge.dot -o hoge.png && open hoge.png 

DOT言語

http://www.graphviz.org/doc/info/attrs.html#k:style

とりあえず簡単な使い方だけメモ。;,をどこに入れるべきなのかよくわかってないが、適当に書いても案外コンパイルできる。

例:オートマトン

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

こういうのはレイアウトを気にせず簡単に書ける。むき出しで書いても良いが、引用符で囲んどいたほうが安全。

20151010160030.jpg

digraph G {
    rankdir=LR;
    empty [label = "" shape = plaintext];
    node [shape = doublecircle]; e1 e2;
    node [shape = circle];
    empty -> s0 [label = "開始"];
    s0 -> s1 [label = "1/a"];
    s0 -> e1 [label = "0"];
    s1 -> s2 [label = "2/b"];
    s1 -> s3 [label = "3/c"];
    s3 -> s2 [label = "2/d"];
    s3 -> e2 [label = "4"];
    s2 -> e1 [label = "0"];
    s2 -> s2 [label = "2"];
}

例:クラス図

頑張ればUMLも記述できるが、さすがにしんどい。

20151010160023.jpg

digraph G {
    label = "図1:クラス図"
    fontsize = 12

    node [fontname = "Bitstream Vera Sans", fontsize = 8, shape = "record"]
    edge [fontname = "Bitstream Vera Sans", fontsize = 8]

    Animal [ label = "{\{abstract\} Animal|+ name : string\l+ age : int\l|\l}"]

    subgraph cluster_Animal {
        label = "Package animal.impl"

        Dog [label = "{Dog||+ bark() : void\l}"]
        Cat [label = "{Cat||+ meow() : void\l}"]
    }

    edge [arrowhead = "empty"]
    Dog -> Animal
    Cat -> Animal

    edge [arrowhead = "none", headlabel = "0..*", taillabel = "0..*"]
    Dog -> Cat
}

例:2部グラフ

はじめに(サブ)グラフとノードの設定をする。
ノードのshapeをpointにするとラベル名が表示されないので、
サブグラフのshapeをplaintextにしてラベルを付け包んで点を表現してみた。
もっと簡単な書き方があるかも知れない。

20151010160026.jpg

digraph G {
    graph [style=rounded color=black shape=plaintext rankdir=LR];
    node [shape=point];

    subgraph cluster_source {
        subgraph cluster_p1 {
            label="p1" color=white shape=plaintext;
            p1
        }
        subgraph cluster_p2 {
            label="p2" color=white shape=plaintext;
            p2
        }
        subgraph cluster_p3 {
            label="p3" color=white shape=plaintext;
            p3
        }
    }

    subgraph cluster_sink {
        subgraph cluster_a {
            label="a" color=white shape=plaintext;
            a
        }
        subgraph cluster_b {
            label="b" color=white shape=plaintext;
            b
        }
        subgraph cluster_c {
            label="c" color=white shape=plaintext;
            c
        }
    }

    p1 -> a p1 -> c
    p2 -> b
    p3 -> c p3 -> a
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした