Programmer easy drawing artifact

Time:2021-9-12

In our work and life, programmers need to draw charts on many occasions, such as the charts in PPT, some mnemonic charts for learning notes, and the most common flow charts widely used in work.

Under window, we have many useful tools, such asVisioEAwait. These software are also very easy to use, but they all have one disadvantage, that is, they are too complex. We need a certain art foundation and a lot of software operation to draw a very simple flow chart.

Moreover, what’s more, once the demand changes, it’s hard to modify, which often affects the whole body. Therefore, Liang Xu often cools his back when he hears the demand change

Later, with the introduction of the great God, Liang Xu began to use an artifact. This artifact doesn’t need you to understand art and software operation. The process of drawing is similar to that of writing software. Just a few lines of code can show the ideas in your mind.

Moreover, you don’t have to worry about layout, modification, or even mouse. You can make quite exquisite works!

This artifact is:dotCommand!

This artifact also has a graphical interface version calledGraphvizHowever, Liang Xu is used to the command line, which is usually completed under the command line.

Let’s see what it can do first. The following pictures are selected from its official website:

Programmer easy drawing artifact

Programmer easy drawing artifact

Programmer easy drawing artifact

Programmer easy drawing artifact

These are just a few of them. More pictures can be viewed on its official website:

http://www.graphviz.org

This software is very powerful. If you master it well, you can draw very good-looking works, and you are not afraid of the needs of the product manager. However, in most cases, we don’t need to use its advanced functions. Often, some very basic functions can meet more than 80% of our needs.

The installation of this software is very simple. Just execute the following commands:

sudo apt install graphviz

After a short wait, the installation is successful. Then you can play happily~

Let’s take a look at a work of Hello world level.

First, create one anywheretest.dotFile (of course, it can also be called other names). The contents of the file are as follows:

graph g{
     "Hello" -- "world"
}

Then, execute the following command:

dot -Tpng -o test.png test.dot

After that, the following simplest picture is generated in the current directory. Is it very simple?

Programmer easy drawing artifact

Let’s briefly introduce the syntax of dot command. In the test.dot file,graphIt shows that this picture isUndirected graphThat is, the connecting line has no arrow. The corresponding isdigraph, indicatingDirected graph, the connecting line has an arrow.

And the picture description is in{}It also supports annotation. The annotation style is similar to that of C language,//For single line notes,/**/Used for multiline comments.

As mentioned earlier, the dot command is very powerful. Here we only introduce its most basic usage, and learning these most basic commands can deal with about 80% of the work. For more advanced usage, please refer to its official website.

node

Similar to scripting language, nodes can be used directly without declaration. For nodes, we generally set the following attributes:

  • Shape shape
  • Label label
  • Style type, filled or unfilled
  • Color line color
  • Fillcolor fill color

These attributes are a square bracket after the node[]Set in. In many cases, our node attributes are exactly the same, so we can define onenode, and set it, then all node attributes in the graph are the same as node. If a node wants to be special, just set it separately.

graph g{
    Node [shape = "box", style = "filled", color = "red", fillcolor = "green"] // set the default shape, type, color and fill color of the node
    A [shape = "ellipse", fillcolor = "yellow", label = "hello"] // set a node separately
    b [label = "world"]   
    a -- b
    A -- C // do not set C, use the default attribute
    D [shape = "circle", label = "circle"] // D set attributes separately
    c -- d
}

Programmer easy drawing artifact

Connecting line

Connecting lines are divided into directed edges and undirected edges according to the presence or absence of arrows. Its common properties are as follows:

  • Style type, solid or dashed
  • Color connector color
  • Label label
  • Labelfontcolor label font color
  • Headlabel start label content
  • Tailabel end label content
  • There is a connecting line label between the decorate label and the connecting line

For a directed edge, you can also set the position of the start point and the end pointe, s, w, nexpressall directions, which can also be represented by combination. Please see the following examples for details.

Similar to nodes, connector lines can also set default properties, usingedgeexpress. If you do not use the default attribute, you can also customize the attribute.

digraph edge_settings {
    Edge [color = "green", correct = false] // set the default attribute of the edge
    node [shape = "polygon", sides = 4, color = "blue"]
    A - > b [style = "dotted", color = "red", label = "a to B"] // set style, color and label
    b: Se - > C: w [headlabel = "end", tailabel = "start"] // set the edge starting from the "Southeast" of B and ending from the "West" of C, and set the label of the starting point and ending point of the edge
    {C, f} - > {D, e} [label = "multi lines", decorate = true] // multiple edges can be drawn simultaneously in this way
}

Programmer easy drawing artifact

chart

Dot language can describe undirected graph and directed graph. Graph identifies undirected graph and digraph identifies directed graph. For the attribute settings of the graph, the following are commonly used:

  • Size size
  • Label label
  • Labelloc label position, usually set to t (top), or B (bottom)
  • Label just label alignment, such as left alignment, right alignment, center, and so on
  • Bgcolor background color
  • Rankdir layout, such as from left to right, or from top to bottom

A graph can also contain subgraphs. Subgraphs must beclusterStart as a prefix. For example, the figure on the home page of the official website, that is, the first figure of this article, contains subgraphs. 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
}

Summary

Using dot command to draw is very easy and flexible. It’s not very convenient for designing rookies! Its functions are very powerful. This paper introduces some basic functions that are very common but also very practical. Mastering these functions can deal with most scenes in the work. But if you want to make a more cool effect, you still need to study the information on its official website.


Finally, recently, many friends came to me for helpLinux learning RoadmapSo I stayed up late in my spare time for a month and sorted out an e-book according to my own experience. Whether you are interviewing or self-improvement, I believe it will help you!

Free to everyone, just ask everyone to give me a praise!

E-book | learning roadmap for Linux Development

I also hope some friends can join me and make this e-book more perfect!

Have you got anything? I hope the old fellow will take three strikes to show this article to more people.

Recommended reading:

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]