Graphviz data structure graphing tool is widely used. Many data structures can be drawn using GV, such as common C language structures, Pointers, linked lists, etc. Can also be used to draw flow charts and other conventional diagrams, easy to use.

Install graphviz

MAC OS can be installed via BREW

brew install graphviz
Copy the code

You can then use the dot command line tool, which is used to generate images

The dot language

Graphviz generates graphs from dot. The syntax of dot is very simple, and there are no complex branch judgment statements.

Declare a graph

For example, 4.gv contains a “Hello” graphic

digraph {
    hello
}
Copy the code

Then run dot 4.gv -tpng -o 4.png && open 4.png to generate an image. The result is as follows

You can see that GV uses ellipses by default to explain the diagram to be drawn

Adjust shape

Gv support many shapes, complete list you can refer to https://www.graphviz.org/doc/info/shapes.html by category can be divided into two categories, one is square, round, such as geometry, another kind is a form (Record Node)

shape=box

digraph {
    hello [shape=box]
}
Copy the code

Results the following

form

digraph {
    hello [shape=record label="first|second|last"]}Copy the code

Results the following

Relationship between nodes

digraph {
    node1 [shape=box]
    node2 [shape=box]
}
Copy the code

The following shows the relationship between Node1 and node2

Unidirectional node1 -> node2

Bidirectional node1 -> node2 Node2 -> node1

No clear direction node1 — node2

graph {
    node1 [shape=box]
    node2 [shape=box]

    node1 -- node2
}
Copy the code

Case – bidirectional linked list

digraph G {
    rankdir="RL"
    value1 [
        shape="circle"
        label="value1"
    ]
    value2 [
        shape="circle"
        label="value2"
    ]
    item1 [
        shape="record"
        label="item1| 
      
        *next : struct item\l| 
       
         *prev : struct item\l| 
        
          *data : void* \l"
        
       
      
    ]
    item2 [
        shape="record"
        label="item2| 
      
        *next : struct item\l| 
       
         *prev : struct item\l| 
        
          *data : void* \l"
        
       
      
    ]
    item1:f1 -> null
    item1:f0 -> item2
    item1:f2 -> value1
    item2:f1 -> item1
    item2:f0 -> null
    item2:f2 -> value2
}
Copy the code

Editor support

Typora, MacDown and other text editors already support wySIWYG editing of Graphviz images directly

typora

macdown

Can follow my public number, the first time to get the latest updates!

A couple of points to note

Comments in dot are the same as those in C: // single-line comments and /* */ multi-line comments

pdot() {
    dot $1.gv -Tpng -o $1.png && open $1.png
}
Copy the code

The resources

  1. www.tonyballantyne.com/graphs.html
  2. Github.com/vgrigoriu/g…
  3. www.worthe-it.co.za/programming…
  4. Graphviz. Gitlab. IO / _pages/doc /…
  5. www.graphviz.org/doc/info/at…
  6. Zh.wikipedia.org/wiki/DOT language