--- tags: Tool_Learning --- # Graphviz 練習中遇到的問題 ## Graphviz 是什麼 * 擷取自 [Graphviz](https://graphviz.org/) 的網站上「What is Graphviz?」的描述: > Graphviz is open source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks. ## 問題描述: ```graphviz digraph linked_list { rankdir=LR; node [shape=box]; n1 [label="顏色"]; node [shape=record]; n2 [label="<r>red|<y>yellow|<g>green"]; { rank=same; n1 -> n2; } } ``` :::danger 當 n2 的 shape 為 record 時,進行 `rank=same` 的操作的話,會無法在該子圖中建立 n1 與 n2 的邊,且系統顯示`Error: lost n1 n2 edge`訊息。 ::: :::success 若改 n2 的 shape 為 box,進行 `rank=same` 的操作的話,則成功建立 n1 與 n2 的邊。 ::: ### 問題 * ~~請問這是否為 Bug,或是我的操作失誤呢?~~ ### 解答 * 根據 [https://graphviz.org/doc/info/shapes.html](https://graphviz.org/doc/info/shapes.html) 中 **Record-based Nodes** 區塊的解釋: > NOTE: Please see the note about record-based nodes at the top of this page. Also note that there are problems using non-trivial edges (edges with ports or labels) between adjacent nodes on the same rank if one or both nodes has a record shape. 此段解釋的大意為:「在同一 rank 的 nodes 中,若包含至少一 node 為 record 型態,且嘗試以 record 型態的 node 與其他 node 建立邊,則會發生問題。」 * 上述解釋中的 `the note about record-based nodes at the top of this page` 所對應的另一則 note 為: > The record-based shape has largely been superseded and greatly generalized by HTML-like labels. That is, instead of using shape=record, one might consider using shape=none, margin=0 and an HTML-like label. 大意為:「通常 record-based 的 nodes 會很大程度地被以 HTML 標注的 label 來代替」,其中一部分就是上述所遇到的問題。 --- ## 額外遇到的問題 :::info 而 Graphviz 語法之所以我只貼發生錯誤版本的,是因為在我測試過後,若上面有其他 Graphviz 語法有錯誤訊息,則接下來的一則語法也會發生錯誤,因此沒有貼正確版本的圖。 ::: * 錯誤訊息的例子 以下應為可正確顯示的圖,但因為上面我所貼的錯誤語法,導致以下正確語法也跟著出錯: ```graphviz digraph linked_list { rankdir=LR; node [shape=box]; n1 [label="顏色"]; node [shape=box]; n2 [label="<r>red|<y>yellow|<g>green"]; { rank=same; n1 -> n2; } } ``` 應可顯示成: ![](https://i.imgur.com/9QrLNGT.png)