Anaconda安裝Graphviz, mac下Graphviz安裝, pcharm中調用pycharm, Graphviz典型例子

 

 

 

 

mac下的Graphviz安裝及使用

一.安裝 
Graphviz http://www.graphviz.org/ 
mac用戶建議直接用homebrew來安裝,官網上版本比較舊php

1.安裝homebrew 
打開終端複製、粘貼如下命令:html

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 1

2.安裝Graphviz 
homebrew安裝完畢後運行 brew install graphviz便可java

注:運行第2步時可能會提示-bash: brew: command not found 
解決辦法: 
終端輸入sudo vim .bash_profile, 
輸入i進入輸入模式, 
添加export PATH="/usr/local/bin:$PATH" , 
按esc鍵退出輸入模式, 
輸入:wq退出並保存 
最後輸入source .bash_profile使配置生效。 
(注意: 和空格也要輸入,bash_profile保存時可能會警告,:w!強制保存便可,更多vim命令參考http://www.cnblogs.com/usergaojie/p/4583796.htmlnode

二.使用 
隨便找的一個例子: 
1.使用文本編輯器(這裏用的是sublime )輸入如下內容:python

digraph pic { Hello -> World }
  • 1
  • 2
  • 3

2.保存爲pic.dot文件 
3.打開終端,切換到dot文件所在目錄,輸入dot pic.dot -T png -o pic.png 
4.能夠看到在dot文件目錄下生成了一個pic.png文件 
這裏寫圖片描述linux

三.進階 
目的:流程圖較複雜時,可以邊寫代碼邊看效果 
目前找了兩個教程,還沒試,有問題了再更新。 
https://blog.zengrong.net/post/2294.html 
https://zhuanlan.zhihu.com/p/22820399nginx

 

 

 

經過Anaconda安裝Graphviz

簡單三步走

1.打開Anaconda終端,Open terminal

image.png

2.在終端窗口一次輸入:

conda install graphviz 
pip install graphviz 
image.pnggit

3.添加環境變量

找到Graphviz的安裝路徑,而後添加到環境變量中便可。個人安裝路徑是 
C:\Users\linxid\Anaconda3\Library\bin\graphvizgithub

安裝後記得重啓,環境變量修改才能夠生效,調用庫,便可成功。 
經過Graphviz繪製的決策樹: 
image.pngredis

 

 

 

 

 

 
 
 
 
 
 
Graphviz Windows環境安裝、配置環境變量、pcharm中調用、中文亂碼、dot語句基本格式
 

Graphviz Windows環境安裝:

        1.官網下載

           官網下載地址     

                

 

 

 

       2.建立桌面快捷方式

 

                安裝目錄\bin文件夾\:找到gvedit.exe文件右鍵 發送到桌面快捷方式,以下圖:

 

 

 
        3.配置環境變量

 

                將graphviz安裝目錄下的bin文件夾添加到Path環境變量中:
 
 
        
        4.驗證是否安裝並配置成功
               進入windows命令行界面,輸入dot -version,而後按回車,若是顯示graphviz的相關版本信息,則安裝配置成功。如圖:
 
         5.python環境中安裝:(pycharm中)
        
               
                而後輸入Graphivz安裝
 
        6.在pycharm中運行
            
# coding:utf-8
from graphviz import Digraph

dot = Digraph(comment='The Round Table')

# 添加圓點 A, A的標籤是 King Arthur
dot.node('A', 'king')
dot.view()  #後面這句就註釋了,也可使用這個命令查看效果

# 添加圓點 B, B的標籤是 Sir Bedevere the Wise
dot.node('B', 'Sir Bedevere the Wise')
#dot.view()

# 添加圓點 L, L的標籤是 Sir Lancelot the Brave
dot.node('L', 'Sir Lancelot the Brave')
#dot.view()

#建立一堆邊,即鏈接AB的邊,鏈接AL的邊。
dot.edges(['AB', 'AL'])
#dot.view()

# 在建立兩圓點之間建立一條邊
dot.edge('B', 'L', constraint='false')
#dot.view()

# 獲取DOT source源碼的字符串形式
print(dot.source)

# 保存source到文件,並提供Graphviz引擎
dot.render('test-output/round-table.gv', view=True)
 
 
 
x
30
 
 
 
 
 
1
# coding:utf-8
2
from graphviz import Digraph
3
 
           
4
dot = Digraph(comment='The Round Table')
5
 
           
6
# 添加圓點 A, A的標籤是 King Arthur
7
dot.node('A', 'king')
8
dot.view()  #後面這句就註釋了,也可使用這個命令查看效果
9
 
           
10
# 添加圓點 B, B的標籤是 Sir Bedevere the Wise
11
dot.node('B', 'Sir Bedevere the Wise')
12
#dot.view()
13
 
           
14
# 添加圓點 L, L的標籤是 Sir Lancelot the Brave
15
dot.node('L', 'Sir Lancelot the Brave')
16
#dot.view()
17
 
           
18
#建立一堆邊,即鏈接AB的邊,鏈接AL的邊。
19
dot.edges(['AB', 'AL'])
20
#dot.view()
21
 
           
22
# 在建立兩圓點之間建立一條邊
23
dot.edge('B', 'L', constraint='false')
24
#dot.view()
25
 
           
26
# 獲取DOT source源碼的字符串形式
27
print(dot.source)
28
 
           
29
# 保存source到文件,並提供Graphviz引擎
30
dot.render('test-output/round-table.gv', view=True)
 
 
結果:
        7. 中文亂碼問題
      • 在安裝目錄找到以下文件用記事本打開:
         
 
      • 將對應位置代碼修改成以下:
 
      • 代碼中加以下語句:
  
       8.標籤中有空格畫出圖出錯的狀況
                   將對應標籤放在英文雙引號內
 
       9.dot語言基本格式
      • dot input.dot -T png -o output.txt
            cmd:
    
                    -o    指定存儲檔案的檔名
 
      • 有向圖:diagraph申明,結點關係爲"->",能夠//註釋
      • 無向圖:graph 申明 結點關係"--"
      • 子圖    :subgraph聲明    父圖是無向圖他自己也得是無向圖    父圖是有向圖自己也得是有向圖
        10.示例
digraph G{
{ a b c} -> { d e f }
}
 
 
 
 
 
 
 
 
 
1
digraph G{
2
{ a b c} -> { d e f }
3
}
 
 
 
 
 
       
 
 
 
 
Graphviz(轉載) 

簡述

原文: http://www.tuicool.com/articles/vy2Ajyu

本文翻譯自 Drawing Graphs using Dot and Graphviz

1. 許可

Copyright (C) 2013, 2014 Tony Ballantyne. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.

Code in this document is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

2. 介紹

2.1 什麼是DOT?

DOT是純文本圖像描述語言,對於計算機和人來講,都很容易閱讀。

2.2 什麼是Graphviz?

Graphviz是一個開源的圖像可視化的軟件,所謂圖像可視化就是使用一種簡單的方法,來將結構化的信息表現成用抽象圖片和網絡構成的示意圖。

2.3 誰適合看這篇文章?

這篇文章原本是我寫來給本身當速成手冊看的。不過如今看起來已經被我弄成給學計算機的學生看的教程了。如今這篇文章能夠給任何想經過例子學習DOT的人看。

2.4 相關材料

我博客還有相似的其餘文章 TonyBallantyne.com/tech

若是你是想了解關於我做爲一名SF and Fantasy writer的工做,但不幸點到這篇文章的話,下面的連接可能就對你比較有用:

譯註:這兒所謂的相關材料實際上是做者本身其餘的一些文章,跟DOT關係不大

3. 安裝

若是你要順着這個教程繼續下去,那你可能就須要要裝Graphviz套件了。Graphviz能夠在 官網 免費下載。

4. 基礎例子

4.1 簡單圖例

graph graphname { 
    a -- b; b -- c; b -- d; d -- a; }

4.2 同樣的圖,不同的佈局

graph graphname {
    rankdir=LR;  //Rank Direction Left to Right
    a -- b; b -- c; b -- d; d -- a; }

4.3 簡單有向圖

digraph graphname{ a -> b; b -> c; a -> c; }

4.4 帶標籤的簡單有向圖

digraph graphname{

    T [label="Teacher"] // node T P [label="Pupil"] // node P T->P [label="Instructions", fontcolor=darkgreen] // edge T->P }

4.5 一樣的圖,不一樣的形狀和顏色

digraph graphname {
    T [label="Teacher" color=Blue, fontcolor=Red, fontsize=24, shape=box] // node T P [label="Pupil" color=Blue, fontcolor=Red, fontsize=24, shape=box] // node P T->P [label="Instructions", fontcolor=darkgreen] // edge T->P }

這兒你能夠選擇的形狀有: box , polygon , ellipse , oval , circle , point , egg , triangle , plaintext , diamond , trapezium , parallelogram , house , pentagon , hexagon , septagon , octagon , doublecircle , doubleoctagon , tripleoctagon 更多的形狀看 這裏

4.6  總結

digraph summary{
    start [label="Start with a Node"] next [label="Choose your shape", shape=box] warning [label="Don't go overboard", color=Blue, fontcolor=Red,fontsize=24,style=filled, fillcolor=green,shape=octagon] end [label="Draw your graph!", shape=box, style=filled, fillcolor=yellow] start->next start->warning next->end [label="Getting Better...", fontcolor=darkblue] }

5. 高級

5.1 節省時間的技巧

單獨地去定義每個節點其實很浪費時間的,下面這個技巧可以讓你快點兒。

digraph hierarchy {

    nodesep=1.0 // increases the separation between nodes node [color=Red,fontname=Courier,shape=box] //All nodes will this shape and colour edge [color=Blue, style=dashed] //All the lines look like this Headteacher->{Deputy1 Deputy2 BusinessManager} Deputy1->{Teacher1 Teacher2} BusinessManager->ITManager {rank=same;ITManager Teacher1 Teacher2} // Put them on the same level }

5.2 記錄

你如今能夠用HTML來定義這一類節點了, 這裏 有更多相關信息。

digraph structs {
    node[shape=record]
    struct1 [label="<f0> left|<f1> mid\ dle|<f2> right"]; struct2 [label="{<f0> one|<f1> two\n\n\n}" shape=Mrecord]; struct3 [label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"]; struct1:f1 -> struct2:f0; struct1:f0 -> struct3:f1; }

6. 例子

6.1 有限狀態機

digraph finite_state_machine {
    rankdir=LR;
    size="8,5" node [shape = circle]; S0 -> S1 [ label = "Lift Nozzle" ] S1 -> S0 [ label = "Replace Nozzle" ] S1 -> S2 [ label = "Authorize Pump" ] S2 -> S0 [ label = "Replace Nozzle" ] S2 -> S3 [ label = "Pull Trigger" ] S3 -> S2 [ label = "Release Trigger" ] }

6.2 數據流示意圖

digraph dfd{    
    node[shape=record]
    store1 [label="<f0> left|<f1> Some data store"]; proc1 [label="{<f0> 1.0|<f1> Some process here\n\n\n}" shape=Mrecord]; enti1 [label="Customer" shape=box]; store1:f1 -> proc1:f0; enti1-> proc1:f0; }

6.3 數據流示意圖2

digraph dfd2{
    node[shape=record]
    subgraph level0{
    enti1 [label="Customer" shape=box]; enti2 [label="Manager" shape=box]; } subgraph cluster_level1{ label ="Level 1"; proc1 [label="{<f0> 1.0|<f1> One process here\n\n\n}" shape=Mrecord]; proc2 [label="{<f0> 2.0|<f1> Other process here\n\n\n}" shape=Mrecord]; store1 [label="<f0> |<f1> Data store one"]; store2 [label="<f0> |<f1> Data store two"]; {rank=same; store1, store2} } enti1 -> proc1 enti2 -> proc2 store1 -> proc1 store2 -> proc2 proc1 -> store2 store2 -> proc1 }

6.4 對象繼承

digraph obj{
    node[shape=record];
    rankdir="BT"; teacher [label = "{<f0> Teacher|<f1> \n |<f2> \n }"]; course [label = "{<f0> Course|<f1> \n |<f2> \n }"]; student [label = "{<f0> Student|<f1> \n |<f2> \n }"]; lesson [label = "{<f0> Lesson |<f1> \n |<f2> \n }"]; tutorial [label = "{<f0> Tutorial|<f1> \n |<f2> \n }"]; assessment[label = "{<f0> Assessment|<f1> \n |<f2> \n }"]; coursework [label = "{<f0> Coursework|<f1> \n |<f2> \n }"]; exam [label = "{<f0> Exam|<f1> \n |<f2> \n }"]; {rank=same; teacher course student} teacher->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."]; student->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."]; lesson->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; tutorial->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; assessment->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; coursework->assessment; exam->assessment; }

6.5 關係型實體

digraph ER{
    node[shape=box]; Book; Customer; Loan; {rank=same;Book,Customer,Loan} Book->Loan[dir="forward",arrowhead="crow",arrowtail="normal"]; Customer->Loan[dir="forward",arrowhead="crow",arrowtail="normal"]; }

7. 參考

如下多是你在畫圖時候最有用的一些屬性,完整的列表能夠在 這裏 看。

7.1 圖像屬性

label="My Graph"; # 給圖像設置標籤 rankdir=LR; # 將圖片由原來的從上到下佈局變成從左到右佈局 {rank=same; a, b, c } # 將一組元素放到同一個level splines="line"; # 讓邊框變爲直線,沒有曲線和銳角 K=0.6; # 用來在佈局中影響spring屬性,spring屬性能夠用於將節點往外推,這個在twopi和sfdp佈局中頗有用。

譯註:暫時還沒明白這個spring屬性應該怎麼翻,初步猜想是彈性。胡克定律裏面的常量名也叫K。

7.2 交點屬性

[label="Some Label"] # 給交點打標籤 [color="red"] # 給交點上色 [fillcolor="blue"] # 設置交點的填充色

7.3 邊的屬性

[label="Some Label"] # 給邊設置標籤 (設置路徑權重的時候頗有用) [color="red"] # 給交點上色 (標示路徑的時候頗有用) [penwidth=2.0] # 給邊適配厚度,標示路徑的時候頗有用。

7.4 尺寸, 背景顏色

fixedsize=true; size="1,1"; resolution=72; bgcolor="#C6CFD532"; # 不是我偷懶不翻譯哦,原文就沒有解釋。

8. 附錄

8.1 拓展閱讀

An Introduction to GraphViz and dot

Graphviz Examples and Tutorial

8.2 使用Emacs的Org Mode

Emacs的Org Mode無論對於寫做,仍是執行和導出DOT圖片都是個很理想的工做環境。

8.2.1 配置

下載並安裝Graphviz,而後把相關路徑加到exec-path這個變量裏去。

你也要把你的.emacs文件更新成可以把DOT做爲babel語言加載,下面這個配置能夠很容易的設置DOT爲babel語言,其餘語言也能夠相似操做

(org-babel-do-load-languages (quote org-babel-load-languages) (quote ( (emacs-lisp . t) (java . t) (dot . t) (ditaa . t) (R . t) (python . t) (ruby . t) (gnuplot . t) (clojure . t) (sh . t) (ledger . t) (org . t) (plantuml . t) (latex . t) ) ) )

8.2.2 將Dot嵌入Emacs

Org Mode經過使用Library of Babel來解析各類語言。要想這麼作的話,就用 begin_src 和 end_src 標籤把你的dot代碼想下面這樣包含進去。你也須要在包裹的時候像下面那樣添加一些命令行參數。

用 <s[TAB] 快捷鍵能夠快速生成一個 begin_src 代碼塊。

#+begin_src dot :file ./img/example1.png :cmdline -Kdot -Tpng graph graphname { a -- b; b -- c; b -- d; d -- a; } #+end_src

8.2.3 命令行相關

#+begin_ src dot :file ./img/example1.png :cmdline -Kdot -Tpng 裏的 :cmdline -Kdot -Tpng 就是命令行參數. 他們告訴dot如何渲染和展現。

  • -Kdot 使用 dot 佈局方式. 你也能夠嘗試其餘的佈局方式,好比 Kneato , Kcirco , Ktwopi , Kfdp , Ksfdp
  • -Tpng 渲染成png格式

完整的命令行參數能夠看 這裏

Date: <2013-10-21 Mon>

Author: Tony Ballantyne

Translator: Casa Taloyum

Created: 2014-04-12 Sat 10:13

Emacs 23.3.1 (Org mode 8.0.2)

 

 

 

 

 

 

 

 

Graphviz典型例子

例子1

//先來看一個例子,咱們建立一個文檔graph1.dot: //digraph是有向圖,graph是無向圖,要注意,->用在有向圖中,--用在無向圖中表示一條邊,不能混用。 //第一行給出了圖的類型和名字 digraph graph1{ main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf; init -> make_string; main -> printf; execute -> compare; }

效果:

01

例子2

/* 來看下一個稍微複雜點的例子,咱們開始手動的設置一下圖的屬性。能夠給點設置屬性, 也能夠給邊設置屬性。先來說講怎麼設置邊的屬性,在每條邊後面的雙括號裏設置邊的屬性。 也能夠在用edge設置邊的默認值。 而給點設置屬性就必須給每一個點單獨的設置一個屬性,node表示點的默認值。 */ //點的默認參數是shape=ellipse, width=.75, height=.5 and labeled by the node name. //一些點的形狀在appendix.h中,一些經常使用的形狀有bos,circle,record,plaintext。 digraph graph2{ size = "4,4"; //把圖的尺寸設爲4 inch,4 inch main[shape=box]; //把main點的形狀設爲方形 main -> parse [weight=8]; //weight是設置了這條邊的重要程度,默認是1 parse -> execute; main -> init [style=dotted] //讓這條線是點狀的 main -> cleanup; execute -> {make_string;printf} //這條語句一次鏈接了兩條條線 init -> make_string; //把邊的默認顏色設爲了red edge[color=red]; //label就是在邊上寫了一行字 main -> printf[style=bold,label="100 times"]; //讓make_string變成了一個兩行的字符串(注意那個\n)。 make_string [label="make a\nstring"]; //設置了一下點的默認參數,藍色,這個被用在了compare中。 node [shape=box,style=filled,color=".7 .3 1.0"]; execute -> compare; }

效果:

02

例子3

//能夠設置每條邊箭頭的方向, //用dir,有forward(default),back,both,none四種。 digraph graph3{ A -> B[dir=both]; B -> C[dir=none]; C -> D[dir=back]; D -> A; }

效果:

例子4

/* 點的shape除了record和Mrecord這兩種以外,其餘的形狀都是多邊形, 而咱們能夠對多邊形進行一下屬性上的設置,shape = polygon。 Sides用於設置它的邊數,peripheries用於設置多邊形的外框的層數, regular = true可讓你的多邊形是一個規則的多邊形, orientation = *,可讓你的多邊形旋轉一個角度, 如orientation = 15就是轉了15度。 Skew後面跟一個(-1.0~1.0)的小數,能讓你的圖形斜切一個角度, distortion是讓你的圖形產生透視效果。 */ digraph graph4{ a -> b -> c; b -> d; a[shape=polygon,sides=5,peripheries=3.color=lightblue,style=filled]; c[shape=polygon,sides=4,skew=.4,label="hello world"]; d[shape=invtriangle]; e[shape=polygon,sides=4,distortion=.7]; }

效果:

例子5

//record和Mrecord的區別就是Mrecord的角是圓的。Record就是由矩形組成的圖形。 digraph structs { node [shape=record]; struct1 [shape=record,label="<f0> left | <f1> middle | <f2> right"]; struct2 [shape=record,label="<f0> one | <f1> two"]; struct3 [shape=record,label="hello\nworld |{ b |{ c | <here> d | e }| f }| g | h"]; struct1 -> struct2; struct1 -> struct3; }

效果:

例子6

/* 當你的線和線label比較多時,能夠給線的屬性decorate = true, 使得每條線的label與所屬線之間連線。你還能夠給每條線加上headlabel和taillabel, 給每條線的起始點和終點加上label,他們的顏色由labelfontcolor來決定, 而label的顏色由fontcolor來決定。 */ graph graph6{ label = "I love you"; //給這幅圖設置,名字 labelloc = b; //圖名字的位置在bottom,也能夠是t labeljust = l; //圖名字的對齊方式在left,也能夠是r edge[decorate = true]; C -- D[label = "s1"]; C -- E[label = "s2"]; C -- F[label = "s3"]; D -- E[label = "s4"]; D -- F[label = "s5"]; edge[decorate = false, labelfontcolor = blue, fontcolor = red]; C1 -- D1[headlabel = "c1", taillabel = "d1", label = "c1 - d1"]; }

效果:

例子7

digraph hierarchy {

    graph [fontname="Microsoft Yahei"]; nodesep=1.0 // increases the separation between nodes bgcolor="#EEE8D5" //All nodes will this shape and colour node [color="#FB9400",fontname="Microsoft Yahei",shape=box] edge [color="#414141", style=filled] //All the lines look like this rankdir = TB; n元線性方程組 -> {n維向量空間 矩陣} 雙線性函數 -> 通常線性空間[dir=back] 通常線性空間 -> 線性映射[dir=foreward] {rank=same;雙線性函數;通常線性空間;線性映射;} n維向量空間 -> 通常線性空間 矩陣 -> 線性映射[dir=both] }

效果:

例子8

digraph html {
   abc [shape=none, margin=0, label=< <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR><TD>0</TD><TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD> </TR> <TR><TD>1</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>2</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>3</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>4</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> </TABLE>>]; }

效果:

例子9

/* 默認時圖中的線都是從上到下的,咱們能夠將其改成從左到右, 在文件的最上層打入rankdir=LR就是從左到右,默認是TB(top -> bottom),也能夠是RL,BT。 //當圖中時間表之類的東西時,咱們會須要點能排在一行(列), 這時要用到rank,用花括號把rank=same,而後把須要並排的點一次輸入。 */ digraph html { rankdir = LR; { node[shape = plaintext]; 1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001; } { node[shape = box, style = filled]; WAR3 -> Xhero -> Footman -> DOTA; WAR3 -> Battleship; } {rank = same; 1996; WAR3;} {rank = same; 1998; Xhero; Battleship;} {rank = same; 1999; Footman;} {rank = same; 2001; DOTA;} }

效果:

09

/* 設立一條邊時,咱們能夠制定這條邊從起點的那個位置射出和從哪一個位置結束。 控制符有"n", "ne","e", "se", "s", "sw", "w" 和 "nw",具體效果見下: */ digraph html { node[shape = box]; c:n -> d[label = n]; c1:ne -> d1[label = ne]; c2:e -> d2[label = e]; b:se -> a[label = se]; c3:s -> d3[label = s]; c4:sw -> d4[label = sw]; c5:w -> d5[label = w]; c6:nw -> d6[label = nw]; }

效果:

參考

Node, Edge and Graph Attributes

Gallery

Documentation

An Introduction to GraphViz and dot

Graphviz Examples and Tutorial

 

 
 
 
 
 
 
 
 
 
 
 
 

使用Graphviz繪圖(一)

 

前言

平常開發或者編寫課程論文時,除了代碼文檔的編寫,其中一個很重要的部分就是須要繪製流程圖、示意圖

繪製流程圖有不少工具,通常常見的就有以下幾種:

  • Word、PPT等辦公軟件
  • Viso以及開源的Dia
  • 畫圖(MSPaint)、PS、AI
  • PicPicke
  • 在線流程圖繪製(eg. www.processon.com)

對於這些軟件不管功能強大與否,適合與否,方便與否,都具備一個特色——所見即所得。你製做過程當中看到的就是最終獲得的結果。圖中形式、佈局在製做過程當中都由本身設置,其實還有一類繪圖系統的存在,其思想是——所思即所得。其中具備表明性的軟件就是——Graphviz。Graphviz是貝爾實驗室開發的一個開源的工具包,它使用一個特定的DSL(領域特定語言)——Dot做爲腳本語言,而後使用佈局引擎解析腳本並完成自動佈局。Graphviz的設計初衷是對圖進行自動佈局(有向圖、無向圖、),可使用dot腳原本定義圖形元素,選擇必定的算法進行佈局,經過對輸入腳本的解析,分析出其中的點,邊以及子圖,而後根據屬性進行繪製,繼而將結果以本身須要的格式導出來。

相對於其餘的繪圖軟件,Granphviz的特色有以下幾個方面:

  • 代碼控制,所思即所得
  • 佈局引擎自動佈局
  • (導出格式很是豐富)

以下即爲Graphivz官網上的一些示例效果:

一個生成有向圖的小例子

咱們先來看一個小例子來看一下使用Graphviz繪製一張圖的完整流程(我使用的是Ubuntu 14.04 LTS,在其它系統上的使用應該差很少,下面再也不標註了)。

要使用Graphviz,先要在系統上安裝Graphviz。在Ubuntu上可使用命令sudo apt-get install graphviz進行安裝,在其餘系統安裝的方法能夠查看Graphviz官網進行查看。程序安裝好以後咱們就可已使用了。

Step 1:首先,須要編輯dot腳本

可使用你熟悉的純文本編輯器進行腳本編寫(必須是純文本編輯器,如vim、notepad++,像word這樣的富文本編輯器是不行的),只需設置編碼爲UTF-8。

編輯下面的腳本代碼,保存爲test.dot(先不用管其具體的意思,直接複製就好了):

digraph G{ main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf; init -> make_string; main -> printf; execute -> compare; }

Step 2:隨後,選用佈局生成結果

使用以下命令生成結果:
dot -Tpng sample.dot -o sample.png

對於這條命令,dot表示用dot佈局,-Tpng表示生成png圖片格式,sample.dot腳本文件名-o sample.png表示生成輸出的圖片名稱

改命令也能夠寫成dot -Kdot -Tpng sample.dot -o sample.png,其中-Kdot表示使用dot佈局。

Graphviz支持幾種佈局引擎:

  • dot : 默認佈局方式,主要用於有向圖
  • neato : 主要用於無向圖
  • twopi : 主要用於徑向佈局
  • circo : 圓環佈局
  • fdp : 主要用於無向圖
  • sfdp : 主要繪製較大的無向圖
  • patchwork : 主要用於樹哈希圖(tree map)

Graphviz支持的輸出圖片格式更是至關的多,經常使用的有如下幾種:

  • pdf :
  • gif
  • png :
  • jpeg : 一種有損壓縮圖片格式
  • bmp : 一種位圖格式
  • svg : 矢量圖,通常用與Web,,能夠用瀏覽器打開
  • ps : 矢量線圖,多用於打印
    更多的輸出格式能夠瀏覽Graphviz輸出格式進行查看。

Step 3:查看生成結果

輸出的圖片,能夠用支持相應圖片格式的軟件打開。Graphviz軟件安裝好以後,有一個圖片瀏覽器能夠進行圖片預覽,只需輸入命令display sample.png便可(sample.png爲生成的圖片文件名),該示例預覽結果以下(你能夠在上一步使用不一樣的佈局方式,查看一下結果有什麼不一樣):

正確完成三個步驟獲得結果說明Graphviz已經能夠在你的系統中正確安裝可使用了。後續我會介紹Graphviz Dot腳本的具體編寫方法。

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

graphviz 安裝和入門

2017-11-21   Humen_
 
 
 

畫流程圖裝逼神器:graphviz,不解釋

———————————以下言歸正傳,切入正題———————————–

一、下載

http://www.graphviz.org/Download_windows.php 
下載 graphviz-2.38.zip ,例如我解壓到位置 D:\Program Files\graphviz

二、配置環境變量

將 D:\Program Files\graphviz\release\bin 添加到環境變量 Path 中。

三、驗證

進入windows命令行界面,輸入dot -version,而後按回車,若是顯示graphviz的相關版本信息,則安裝配置成功。 
以下:

C:\Users\shanhy>dot -version
dot - graphviz version 2.38.0 (20140413.2041)
libdir = "D:\Program Files\graphviz\release\bin"
Activated plugin library: gvplugin_dot_layout.dll
Using layout: dot:dot_layout
Activated plugin library: gvplugin_core.dll
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
        D:\Program Files\graphviz\release\bin\config6
                was successfully loaded.
    render      :  cairo dot fig gd gdiplus map pic pov ps svg tk vml vrml xdot
    layout      :  circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
    textlayout  :  textlayout
    device      :  bmp canon cmap cmapx cmapx_np dot emf emfplus eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg m
etafile pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp xdot xdot1.2 xdot1.4
    loadimage   :  (lib) bmp eps gd gd2 gif jpe jpeg jpg png ps svg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

四、基本繪圖入門

運行 D:\Program Files\graphviz\release\bin 目錄下的 gvedit.exe 能夠運行打開圖形界面。 
File > New 建立新的腳本文件,編寫腳本後,點擊 「Layout」圖標(快捷鍵F5)能夠直接查看結果。

小試牛刀:

digraph gvDemo {
    main -> parse -> execute;
    main -> init;
    main -> cleanup;
    execute -> make_string;
    execute -> printf
    init -> make_string;
    main -> printf;
    execute -> compare;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

效果: 
這裏寫圖片描述

digraph gvDemo{
    node [peripheries=2 style=filled color="#eecc80"]
    edge [color="sienna" fontcolor="green"]
    main -> parse -> execute;
    main -> init [arrowhead = box];
    main -> cleanupi -> main;
    make_string[label = once shape=parallelogram style=filled ]
    execute -> make_string[label=Go style=dashed arrowtail=diamond];
    execute -> printf [shape=box];
    init -> make_string;
    main -> printf[dir=none];
    execute -> compare[dir=both];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

效果(這個不錯,能夠當之後的模板用 ^_^): 
這裏寫圖片描述

示例1:graph 使用 – 描述關係

graph gvDemo1 {
    a -- b
    a -- b
    b -- a [color=blue]
}
  • 1
  • 2
  • 3
  • 4
  • 5

效果: 
這裏寫圖片描述

示例2:digraph 使用 -> 描述關係

digraph gvDemo2 {
    a -> b
    a -> b
    b -> a [color=blue style=filled]
}
  • 1
  • 2
  • 3
  • 4
  • 5

效果: 
這裏寫圖片描述

示例3:

digraph gvDemo3 {
    edge[fontname="Microsoft YaHei"]
    node[fontname="Microsoft YaHei"]
    graph[fontname="Microsoft YaHei"]
    label="遊戲資源更新流程"
    rankdir="TB"
    start[label="啓動遊戲" shape=circle style=filled]
    ifwifi[label="網絡環境判斷是否 WIFI" shape=diamond]
    needupdate[label="是否有資源須要更新" shape=diamond]
    startslientdl[label="靜默下載" shape=box]
    enterhall[label="進入遊戲大廳" shape=box]


    enterroom[label="進入房間" shape=box]
    resourceuptodate[label="資源不完整" shape=diamond]
    startplay[label="正常遊戲" shape=circle fillcolor=blue]
    warning[label="提醒玩家是否更新" shape=diamond]
    startdl[label="進入下載界面" shape=box]
    //{rank=same; needupdate, enterhall}


    {shape=diamond; ifwifi, needupdate}


    start -> ifwifi
    ifwifi->needupdate[label="是"]
    ifwifi->enterhall[label="否"]
    needupdate->startslientdl[label="是"]
    startslientdl->enterhall
    needupdate->enterhall[label="否"]


    enterhall -> enterroom
    enterroom -> resourceuptodate
    resourceuptodate -> warning[label="是"]
    resourceuptodate -> startplay[label="否"]
    warning -> startdl[label="確認下載"]
    warning -> enterhall[label="取消下載"]
    startdl -> enterhall[label="取消下載"]
    startdl -> startplay[label="下載完成"]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

效果: 
這裏寫圖片描述

示例4:

graph gvDemo4{
    "黑海" -- "亞速海";
    "黑海" -- "博斯普魯斯海峽"
    "達達尼爾海峽" -- "愛琴海"
    subgraph cluster_T{//新東西
        label = "黑海海峽";//新東西
        "達達尼爾海峽" -- "馬爾馬拉海" -- "博斯普魯斯海峽";
    }
    subgraph cluster_M{
        label = "地中海海域";
        "中部地中海" -- {"愛琴海" "愛奧尼亞海" "西西里海峽"}; //也是新東西
        "西部地中海" -- {"西西里海峽" "第勒尼安海" "利古里亞海" "伊比利海" "阿爾沃蘭海"};
        "愛奧尼亞海" -- "亞得里亞海";
        "阿爾沃蘭海" -- "直布羅陀海峽";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

效果: 
這裏寫圖片描述

這張圖有些新東西能夠看。 
第一個是subgraph 關鍵字。一如名字所示,他是用來定義「次級圖片」用的。 
次級圖片在dot的官方文件中常被叫做cluster subgraph,特指圖示中被方框包裹起來的那兩塊,其定義方式和通常的graph很是類似,不過使用上有兩件事須要留意: 
graph的命名得以cluster前綴開頭,不然語法雖然能過關,但生不出圖面上您預期的效果。 
若是父圖是無向圖,他自己也得是無向圖;反之若是父圖是有向圖,這邊也得乖乖照着來。 
第二個重點是下面這段:

「中部地中海」 – {「愛琴海」 「愛奧尼亞海」 「西西里海峽」}; 
用大括號括起,用空格分開-這是一口氣將好幾個節點羣組起來同時操做的方法,其等效於: 
「中部地中海」 – 「愛琴海」; 2 「中部地中海」 – 「愛奧尼亞海」; 3 「中部地中海」 – 「西西里海峽」;

您甚至能夠用如下程式碼畫出下面這個圖:

digraph gvABC {
    { a b c } -> { d e f }
}
  • 1
  • 2
  • 3

這裏寫圖片描述

除了直接使用工具查看和生成結果外,還可使用命令來操做:

語法:
<cmd> <inputfile> -T <format> -o <outputfile>
示例:
dot D:\test\1.gv -Tpng -o image.png
  • 1
  • 2
  • 3
  • 4

Graphviz 中的 cmd 有好多種,每種使用方法都徹底相同,差異只在於渲染出來的圖片效果不同。 
dot 渲染的圖具備明確方向性(最經常使用,通常都使用這個)。 
neato 渲染的圖缺少方向性。 
twopi 渲染的圖採用放射性佈局。 
circo 渲染的圖採用環型佈局。 
fdp 渲染的圖缺少方向性。 
sfdp 渲染大型的圖,圖片缺少方向性。

PS:

Setting(快捷鍵Shift + F5) 裏面咱們能夠設置生成的文件格式,例如能夠生成 svg、pdf 等等格式,很強大。你能夠參考工具裏面能夠選擇的值帶入到命令行執行。 
Graphviz 使用空格來解析腳本,因此咱們在編寫腳本的時候,對於含有空格的字符,要使用雙引號,這樣纔不會出現錯誤。 
更多關於 dot 語法和graphviz 的使用詳見官方文檔: http://www.graphviz.org/Documentation.php

關於中文亂碼問題的解決方法: 先將源文件保存爲UTF-8格式,而後對中文內容設置 frontname,例如:start[label="啓動遊戲" shape=circle style=filled, fontname="NSimSun"] 
或者設置全局屬性,例如上面gvDemo3 中的代碼片斷:

digraph gvDemo3 {
    edge[fontname="Microsoft YaHei"]
    node[fontname="Microsoft YaHei"]
    graph[fontname="Microsoft YaHei"]

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

fontname後面也能夠直接指定 xxx.ttf 字體文件 
Windows系統中文字體的英文名對應關係參考以下: 
新細明體:PMingLiU 
細明體:MingLiU 
標楷體:DFKai-SB 
黑體:SimHei 
宋體:SimSun 
新宋體:NSimSun 
仿宋:FangSong 
楷體:KaiTi 
仿宋_GB2312:FangSong_GB2312 
楷體_GB2312:KaiTi_GB2312 
微軟正黑體:Microsoft JhengHei 
微軟雅黑體:Microsoft YaHei

參考資料: 
http://www.graphviz.org/Documentation/dotguide.pdf(官方) 
http://blog.jobbole.com/94472/

 
 
 
 
 
 

windows下Graphviz安裝及入門教程

  • 下載安裝配置環境變量
    • intall
    • 配置環境變量
    • 驗證
  • 基本繪圖入門
    • graph
    • digraph
    • 一個複雜的例子
  • 和python交互

 

發現好的工具,如同發現新大陸。有時,咱們會好奇,論文中、各類專業的書中那麼形象的插圖是如何作出來的,無一例外不是對繪圖工具的熟練使用。


下載安裝、配置環境變量

intall

windows版本下載地址:http://www.graphviz.org/download/

這裏寫圖片描述

雙擊msi文件,而後一直next(記住安裝路徑,後面配置環境變量會用到路徑信息),安裝完成以後,會在windows開始菜單建立快捷信息,默認快捷方式不放在桌面。 


這裏寫圖片描述 

 

配置環境變量

將graphviz安裝目錄下的bin文件夾添加到Path環境變量中:

 


這裏寫圖片描述 

 

 


這裏寫圖片描述 

 

驗證

進入windows命令行界面,輸入dot -version,而後按回車,若是顯示graphviz的相關版本信息,則安裝配置成功。

 


這裏寫圖片描述 

 

基本繪圖入門

打開windows下的graphviz編輯器gvedit,編寫以下的dot腳本語言,保存成gv格式文本文件。而後進入命令行界面,使用dot命令,將gv文件轉化爲png圖形文件。

dot D:\test\1.gv -Tpng -o image.png
  • 1

graph

graph使用--描述關係

graph pic1 { 
  a -- b a -- b b -- a [color=blue] } 

 

 


這裏寫圖片描述 

 

digraph

使用->描述關係

digraph pic2 { a -> b a -> b b -> a [style=filled color=blue] } 

 

 


這裏寫圖片描述 

 

一個複雜的例子

digraph startgame { label="遊戲資源更新流程" rankdir="TB" start[label="啓動遊戲" shape=circle style=filled] ifwifi[label="網絡環境判斷是否 WIFI" shape=diamond] needupdate[label="是否有資源須要更新" shape=diamond] startslientdl[label="靜默下載" shape=box] enterhall[label="進入遊戲大廳" shape=box] enterroom[label="進入房間" shape=box] resourceuptodate[label="資源不完整" shape=diamond] startplay[label="正常遊戲" shape=circle fillcolor=blue] warning[label="提醒玩家是否更新" shape=diamond] startdl[label="進入下載界面" shape=box] //{rank=same; needupdate, enterhall} {shape=diamond; ifwifi, needupdate} start -> ifwifi ifwifi->needupdate[label="是"] ifwifi->enterhall[label="否"] needupdate->startslientdl[label="是"] startslientdl->enterhall needupdate->enterhall[label="否"] enterhall -> enterroom enterroom -> resourceuptodate resourceuptodate -> warning[label="是"] resourceuptodate -> startplay[label="否"] warning -> startdl[label="確認下載"] warning -> enterhall[label="取消下載"] startdl -> enterhall[label="取消下載"] startdl -> startplay[label="下載完成"] }

 

 

 


這裏寫圖片描述 

 

和python交互

graphviz強大而便捷的關係圖/流程圖繪製方法,很容易讓咱們聯想到機器學習中的Decision Tree的展現方式。幸運的是,scikit-learn提供了生成.dot文件的接口,具體操做以下:

python編輯環境下:

from sklearn.tree import export_graphviz # 導入的是一個函數 # tree表示已經訓練好的模型,即已經調用過DecisionTreeClassifier實例的fit(X_train, y_train)方法 export_graphviz(tree, out_file='tree.dot', feature_names=['petal length', 'petal width'])

進入windows命令行界面,cd 切換到tree.dot所在的路徑,執行

dot -Tpng tree.dot -o tree.png

 


這裏寫圖片描述 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

學習Graphviz繪圖

1、關於Graphviz

1.1 簡介

Graphviz (英文:Graph Visualization Software的縮寫)是一個由AT&T實驗室啓動的開源工具包,用於繪製DOT語言腳本描述的圖形。它也提供了供其它軟件使用的庫。Graphviz是一個自由軟件,其受權爲Eclipse Public License。其Mac版本曾經得到2004年的蘋果設計獎[2]。

Graphviz由一種被稱爲DOT語言的圖形描述語言[3] 與一組能夠生成和/或處理DOT文件的工具組成:

命令 說明
dot 一個用來將生成的圖形轉換成多種輸出格式的命令行工具。其輸出格式包括PostScript,PDF,SVG,PNG,含註解的文本等等。
neato 用於sprint model的生成(在Mac OS版本中稱爲energy minimized)。
twopi 用於放射狀圖形的生成
circo 用於圓形圖形的生成。
fdp 另外一個用於生成無向圖的工具。
dotty 一個用於可視化與修改圖形的圖形用戶界面程序。
lefty 一個可編程的(使用一種被EZ影響的語言[4])控件,它能夠顯示DOT圖形,並容許用戶用鼠標在圖上執行操做。Lefty能夠做爲MVC模型的使用圖形的GUI程序中的視圖部分。
  • 官網:http://www.graphviz.org/
  • 官方文檔:http://www.graphviz.org/Documentation.php
  • 下載地址:http://www.graphviz.org/Download..php

1.2 DOT語言

DOT語言是一種文本圖形描述語言它提供了一種簡單的描述圖形的方法,而且能夠爲人類和計算機程序所理解。DOT語言文件一般是具備.gv或是.dot的文件擴展名。不少程序均可以處理DOT文件。

1.3 安裝

Mac下直接經過brew install graphviz了,也能夠直接從官網下載。

1.4 簡單示例

Graphviz畫圖只須要兩步。第一步建立文本文件並命令爲x.dot,輸入如下內容:

graph demo {
    "Browser" -- {"Chrome", "Fiefox", "Safari", "..."}
}

第二步,使用命令將文本內容轉換爲圖片。

dot demo.dot -T png -o demo.png

-T表示輸出類型,能夠指定jpg、gif、svg等等,-o指定輸出文件名,不指定則輸出到標準輸出上。執行上面命令後不出意外則能夠看到以下效果。

上面是用dot命令來生產,也能夠用Graphviz中包含的其餘命令來處理dot文本,好比用下面命令渲染出來的圖片排列方式則不同。

neato demo.dot -T png -o demo.png

本文後面的示例都以dot命令來渲染。

1.5 開發工具

能夠直接經過上面的這種命令生成,缺點就是看不到實時效果。有一些工具能夠實時查看,我這裏使用的是vscodePlantUML插件。AtomSublime應該也都有本身的插件,Eclipse上也能夠安裝PlantUML。Plantuml支持dot須要將開頭的@startuml和結尾的@enduml換成@startdot@enddot,也能夠在前面加個//或者#(#@startdot #@enddot),這樣子dot命令生成的時候也不須要額外註釋掉該部分。

2、DOT用法

2.1 基本用法

DOT中使用圖(digraph/graph/subgraph)、節點(node)和邊(edge)來描述關係圖/流程圖,在配合一些屬性的設置完成繪圖。看下面例子:

#@startdot

digraph demo {
    label="示例"
    bgcolor="beige"

    node[color="grey"]

    father[label="爸爸", shape="box"]
    mother[label="媽媽", shape="box"]
    brother[label="哥哥", shape="circle"]
    sister[label="姐姐", shape="circle"]
    node[color="#FF6347"]
    strangers[label="路人"]

    edge[color="#FF6347"]

    father->mother[label="夫妻", dir="both"]
    father->brother[label="父子"]
    father->sister[label="父子"]
    father->我[label="父子"]

    mother->{brother,sister,我}[label="母子"]

    {rank=same; father, mother}
    {rank=same; brother,sister,我}
}

#@enddot

說明:

  • graph 用來描述無向圖,關係使用 --來描述,digraph用來描述有向圖,關係使用 -> 來描述。
  • demo爲定義的圖片名稱,label和bgcolor爲定義的圖片屬性。
  • fathermotherbrothersisterstrangers爲節點。能夠把前面4個節點理解成變量定義,好比定義了一個變量father,名字叫爸爸,用方形來渲染,後面全部調用father都會按照方形爸爸來渲染,但這裏其實和變量定義是不一樣的,由於這個節點即使不用也會渲染出來,好比strangers,變量思惟爲增強理解。好比沒有定義也是能夠直接使用的。
  • 描述節點與節點的關係理解爲邊。邊有有向(->)和無向(--)兩種。好比fathermother之間的關係爲相互的。
  • 上面有兩個特殊的節點,nodeedge,node用來定義節點的默認屬性,edge用來定義邊的默認屬性。做用域從本次定義到下一次定義截住。特定節點/邊設置的屬性會覆蓋默認值。
  • []內屬性,屬性能夠針對圖、節點、邊來設置。
  • father 與子女的關係爲一條條寫的, mother 的關係則爲直接經過大括號的方式來對應三個節點。
  • rank 定義設置節點處在同一行,輔助渲染出來的圖的效果。
  • 註釋和C語言相似,// 和 # 註釋單行, /* */多行註釋。
  • DOT的寫法不算嚴格,好比結束能夠有分號,屬性能夠沒有引號。

最後看一下生成的效果圖:

該圖最基本的關係用一句話就能夠表達出來了

{"爸爸", "媽媽"}->{"哥哥", "我", "姐姐"}

2.2 經常使用屬性

2.2.1 經常使用圖屬性

屬性名 默認值 說明
label   圖片標籤,如上面示例
bgcolor   背景顏色,顏色文檔點此
fontcolor black 字體顏色,定義上面示例的顏色
fontname Times-Roman 字體
fontsize 14 字體大小
rank   子圖等級限制, same,min,max,source,sink
rankdir TB 排序方向,LR(left to right) or TB(top to bottom)
compound false If true, allow edges between clusters. 配合 lhead 和 ltail 使用

2.2.2 經常使用節點屬性

屬性名 默認值 說明
label node name 節點顯示內容
color black node邊框顏色
fontcolor black 字體顏色
fillcolor   背景色
fontname Times-Roman 字體
fontsize 14 字體大小
shape ellipse 形狀,box、ellipse、circle、diamond、plaintext、point、triangle、invtriangle
style   圖形樣式,eg. bold、dashed、dotted、filled
image   背景圖片地址

shape示例

digraph demo {
    bgcolor="floralwhite"
    "box"[shape=box]
    "polygon"[shape=polygon,sides=7] 
    "ellipse"[shape=ellipse]
    "circle"[shape=circle]
    "point"[shape=point]
    "triangle"[shape=triangle]
    "invtriangle"[shape=invtriangle]
    "plaintext"[shape=plaintext]
    "diamond"[shape=diamond]
}

2.2.3 經常使用邊屬性

屬性名 默認值 說明
label   描述關係
color black 箭頭顏色
fontcolor black 關係文字顏色
dir forward 設置方向:forward,back,both,none
arrowhead normal 箭頭頭部形狀。box、crow、diamond、dot、none、normal、vee。箭頭文檔點此
arrowtail   箭頭尾部形狀
arrowsize 1.0 箭頭大小
style   圖形樣式,eg. bold、dashed、dotted、filled
lhead   當 compound 爲true時,lhead用於指定邊指向的cluster
ltail   與ltail相似

arrowhead示例

digraph demo {
    bgcolor="floralwhite"
    rankdir=LR

    "box"->"crow"[arrowhead=box]
    "crow"->"curve"[arrowhead=crow]
    "curve"->"diamond"[arrowhead=curve]
    "diamond"->"dot"[arrowhead=diamond]
    "dot"->"inv"[arrowhead=dot]
    "inv"->"none"[arrowhead=inv]
    "none"->"normal"[arrowhead=none]
    "normal"->"tee"[arrowhead=normal]
    "tee"->"vee"[arrowhead=tee]
    "vee"->"box"[arrowhead=vee]

    #來個高級的用法
    a->b[arrowhead=lcrowortee]
}

2.3 一些示例

2.3.1 子圖

一個圖能夠包含多個子圖,以及子圖也能夠嵌套子圖。子圖的名字須爲cluster*,不然就直接當節點渲染了。

digraph demo {
    bgcolor="beige"

    subgraph cluster_husband {
        node[color="grey"]
        {"爸爸", "媽媽"} -> "我"
    }

     subgraph cluster_wife {
         {"岳父", "岳母"} -> "老婆"
     }

     "我" -> "老婆"[label="夫妻", dir="both"]
     {rank=same; "我", "老婆"}
}

渲染效果以下:

2.3.2 二叉樹形式

digraph demo {
    bgcolor="beige"
    node [shape="record", height=.1]
    node0[label="<f0> | <f1> G | <f2>"]
    node1[label="<f0> | <f1> E | <f2>"]
    node2[label="<f0> | <f1> B | <f2>"]
    node0:f0 -> node1:f1
    node0:f2 -> node2:f1
}

其中,用 | 隔開的串會在繪製出來的節點中展示爲一條分隔符,用 <> 括起來的串稱爲錨點。效果以下:

記錄形式的節點也能夠是豎形排列的。與橫向排列的記錄的不一樣只是label的形式不一樣,label中內容使用 {} 包圍則是豎形排列的。代碼以下:

digraph demo {
    bgcolor="beige"
    node [shape="record"]
    a [label="{a | b | c}"]
}

2.3.3 直接指向子圖

邊直接指向cluster,須要設置 compound 爲true,並配合 lhead 或 ltail 來實現。代碼以下:

digraph demo {
    bgcolor="beige"
    compound=true
    subgraph cluster0 {
        a
    }
    subgraph cluster1 {
        b
    }
    a -> b [lhead=cluster1];
}


Markdown可讓使用者關注內容的書寫,不太須要關注文檔格式;Graphviz也是同樣,讓使用者更多的關注內容自己之間的關係而不用太在乎展示形式,以文本的方式來繪圖也方便進行版本管理。當關係數據量比較大時(好比從數據庫讀取數據)Graphviz的優點將更明顯。

參考文檔:

相關文章
相關標籤/搜索