We programmers in the work and life, there are many occasions to draw diagrams, such as the diagram in PPT, some mnemonic diagrams of learning notes, and the most common, a large number of flow charts used in work.
Under Windows, we have many useful tools, such as Visio, EA and so on. These programs work well, but they all have the disadvantage of being too complex. We need some basic art skills and a lot of software to draw a simple flow chart.
And, more importantly, once the requirements change, it is very difficult to modify, often affecting the whole. So often when hearing the need to change, Xu shivers down his spine…
Later, at the great god’s introduction, Liang Xu began to use an artifact. This artifact does not require you to know art, nor do you need to know software operation, the process of drawing is similar to the process of writing software, just a few lines of code, you can express the ideas in your head.
And, don’t worry about layout, don’t worry about modification, don’t even use the mouse, but also to produce quite beautiful works!
This artifact is the dot command!
There is also a graphical version of this artifact called Graphviz, but Yoshi is used to the command line and is usually done on the command line.
So let’s see what it can do. Here are a few images from its website:
These are just a few of the images, but you can find more on its website:
http://www.graphviz.org
Copy the code
The software is very powerful and, if mastered well, can produce a very good picture without fear of the product manager changing the requirements. However, in most cases, we do not need its advanced functions, and often some very basic functions can handle more than 80% of the requirements of our work.
Installing the software is as simple as executing the following commands:
sudo apt install graphviz
Copy the code
After a short wait, the installation is successful. Then, you can play happily
Let’s take a look at a Hello World level product.
First, create a test.dot file (you can call it anything else) in any location with the following contents:
graph g{
"Hello" -- "world"
}
Copy the code
Then, execute the following command:
dot -Tpng -o test.png test.dot
Copy the code
After that, this is the simplest image in the current directory, isn’t it very simple?
Let’s take a look at the syntax of the DOT command. In the test.dot file, graph means that the graph is undirected, that is, the connection line has no arrows. The counterpart is a digraph, which represents a digraph, where the connecting lines have arrows.
The description of the picture is carried out in {}, and also supports comments, comments style and C language similar, // for single-line comments, /**/ for multi-line comments.
As mentioned earlier, the DOT command is very powerful. Here are just some of its basic uses, but learning the basic commands can handle about 80% of the work. See its website for more advanced uses.
node
Similar to scripting languages, nodes can be used without declaration. For nodes, we generally set the following properties:
- Shape shape
- The label tag
- Style type, fill or unfill
- Color line color
- Fillcolor fillcolor
These properties are set in square brackets [] after the node. In many cases, our node properties are exactly the same, so we can define a node and set it so that all the node properties in the graph are the same as node. If a node wants to be special, it can be set separately.
Graph g{node [shape = "box", style = "filled", color = "red", fillcolor = "green"] Fill a [shape = "ellipse", fillcolor = "yellow", Label = "Hello"] b [label = "world"] a -- b a -- c Label = "cicle"] //d set attribute c -- d}Copy the code
cable
Connecting lines are divided into directed and undirected edges according to the presence or absence of arrows. Its common properties are as follows:
- Style type, solid or dashed
- Color Indicates the color of the connection cable
- The label tag
- Labelfontcolor labelfontcolor
- Headlabel Start label content
- Taillabel End label content
- There are wiring annotations between the connector tag and the connector
For directed edges, you can also set the starting point and ending point, using E, S, W, n to represent southeast, northwest, or a combination of them. For details, see the following example.
Like nodes, wires can be set to default properties, represented by edge. You can also customize attributes if you do not use the default attributes.
Digraph edge_settings {edge [color = "green", ribbon = false] // Set the default properties of edge. [color = "dotted"] a -> a [color = "dotted"] a -> a [color = "dotted"] W [headLabel = "end", taillabel = "start"] // Set edge from "southeast" of B, from "west" of C, Label {c, f} -> {d, e} [label = "multi-lines", dedicate = true]Copy the code
figure
DOT language can describe both undirected graph and directed graph. Graph refers to undirected graph and Digraph refers to directed graph. For graph properties, the following are commonly used:
- The size size
- The label tag
- Labelloc label position, usually set to T (top), or B (bottom)
- Labeljust label alignment, such as left, right, center, and so on
- Bgcolor Background color
- Rankdir layout, such as left to right, or top to bottom
Diagrams can also contain subgraphs, which must start with cluster as a prefix. For example, the graph on the homepage of the official website, namely the first graph of this article, contains sub-graph in the graph, and its source code is as follows:
digraph graph_settings { start [shape = "Mdiamond"] end [shape = "Msquare"] subgraph cluster_sub1 { label = "process #1" labelloc = "t" bgcolor = "gray55" node [style = "filled", color = "white"] a0 -> a1 -> a2 -> a3 -> a0 } subgraph cluster_sub2 { label = "process #2" labelloc = "t" color = "blue" node [style = "filled", color = "black", fillcolor = "gray55"] b0 -> b1 -> b2 -> b3 } start -> {a0, b0} a1 -> b3 b2 -> a3 {a3, b3} -> end }Copy the code
summary
Using the dot command to draw, very easy, very flexible, for the novice designers, is not too convenient! Its function is very powerful, this article introduces it is very common, but also very practical some basic functions, master these functions can deal with most of the work of the scene. However, if you want to make it more cool, you need to study the information on its website.