Graphviz
(相關詳細教程請點擊進入官網)是基於dot語言的繪圖工具,可以畫有向圖,無向圖,關係圖,目錄圖,流程圖等。在使用過程中,發現 Graphivz
對 中文的支持還是不夠,容易出現各種各樣的問題。
- 中文亂碼問題
這種問題,很多人都遇到過,因爲 Graphivz
默認是不支持中文的,所以如果直接寫中文的畫,會顯示成亂碼,一般這種情況可以修改文件的編碼 格式爲 UTF-8
, 然後將 fontname
設置爲系統中存在的中文字體格式就可以解決。具體可以參考這篇博文 Graphviz使用簡介(中文亂碼的問題) ,我就不在贅敘了。
- 中文無法顯示
這是我使用過程中確確實實遇到的問題。比如,我們使用如下 dot
代碼來繪製一個 hash
表圖。
可能這個問題跟系統環境有關,首先,我先貼一下我的環境
- 編輯器: GNU Emacs 25.2.1 (x8664-pc-linux-gnu, GTK+ Version 3.20.9) of 2017-02-25
- 系統: Linux version 4.4.39-gentoo (root@johans) (gcc version 4.9.4 (Gentoo 4.9.4 p1.0, pie-0.6.4) ) #1 SMP Wed Mar 8 09:21:39 CST 2017
digraph idp_modules{
rankdir = TB;
fontname = "Microsoft YaHei";
fontsize = 12;
node [ fontname = "Microsoft YaHei", fontsize = 12, shape = "record" ];
edge [ fontname = "Microsoft YaHei", fontsize = 12 ];
subgraph cluster_sl{
label="IDP支持層";
bgcolor="mintcream";
node [shape="Mrecord", color="skyblue", style="filled"];
network_mgr [label="網絡管理器"];
log_mgr [label="日誌管理器"];
module_mgr [label="模塊管理器"];
conf_mgr [label="配置管理器"];
db_mgr [label="數據庫管理器"];
};
subgraph cluster_md{
label="可插拔模塊集";
bgcolor="lightcyan";
node [color="chartreuse2", style="filled"];
mod_dev [label="開發支持模塊"];
mod_dm [label="數據建模模塊"];
mod_dp [label="部署發佈模塊"];
};
mod_dp -> mod_dev [label="依賴..."];
mod_dp -> mod_dm [label="依賴..."];
mod_dp -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
mod_dev -> mod_dm [label="依賴..."];
mod_dev -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
mod_dm -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
}
導致這一情況的有兩種原因:
- 設置的字體在你係統中不存在,在這裏,我的系統中不存在
Microsoft YaHei
這種字體 - 在
Label
中包含中文的時候, 英文引號和中文之間需要有空格分分隔。
但是很奇怪,這種情況只發生在擁有內嵌子圖時 subgraph(子圖)
中的 label
上, 而不會發生在 digraph
中的 label
上。
解決方法:
- 將中文字體設置爲你係統存在的中文字體
- 在
Label
中的中文和引號之間用空格隔開。
digraph idp_modules{
rankdir = TB;
fontname = "FZFangSong-Z02";
fontsize = 12;
node [ fontname = "FZFangSong-Z02", fontsize = 12, shape = "record" ];
edge [ fontname = "FZFangSong-Z02", fontsize = 12 ];
subgraph cluster_sl{
label="IDP支持層";
bgcolor="mintcream";
node [shape="Mrecord", color="skyblue", style="filled"];
network_mgr [label=" 網絡管理器 "];
log_mgr [label=" 日誌管理器 "];
module_mgr [label=" 模塊管理器 "];
conf_mgr [label=" 配置管理器 "];
db_mgr [label=" 數據庫管理器 "];
};
subgraph cluster_md{
label="可插拔模塊集";
bgcolor="lightcyan";
node [color="chartreuse2", style="filled"];
mod_dev [label=" 開發支持模塊 "];
mod_dm [label=" 數據建模模塊 "];
mod_dp [label=" 部署發佈模塊 "];
};
mod_dp -> mod_dev [label="依賴..."];
mod_dp -> mod_dm [label="依賴..."];
mod_dp -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
mod_dev -> mod_dm [label="依賴..."];
mod_dev -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
mod_dm -> module_mgr [label="安裝...", color="yellowgreen", arrowhead="none"];
}