Digraph /graph/subgraph, node and edge are used to describe the diagram/flow chart in DOT, and the drawing is completed with the setting of some properties.

1. Basic usage of graphs

The basic syntax of DOT is as follows:

Digraph /graph/subgraph graphName{label=" example "; bgcolor="beige" ...... }Copy the code

Digraph /graph/subgraph represents an undirected graph, a directed graph, and a subgraph, respectively. GraphName is the name of an image.

  • digraphUsed to describe directed graphs, used in relation->To describe the
  • graphUsed to describe undirected graphs, used in relation--To describe the
  • labelandbgcolorIs a defined image property
  • graphNameDefine the name of the image
  • Comments are similar to C,//#Comment a single line,/ * * /Multi-line comments.
  • DOT can be written with a semicolon at the end and no quotes at the property.

Common graph properties

The property name The default value instructions
label Image labels, as aboveThe sample
bgcolor Background color, color documentClick here to
fontcolor black Font color, defined aboveThe sampleThe color of the
fontname Times-Roman The font
fontsize 14 The font size
rank Subgraph level limits, same,min, Max, Source,sink
rankdir TB LR(left to right) or TB(top to bottom)
compound false If true, allow edges between clusters. Use with LHEAD and LTAIL

2. Node (nodeThe use of the)

Digraph demo {label=" example "bgcolor="beige" // Define node default property node[color="grey"] // define node root[label=" root ", Shape ="box"] left[label=" left child ", shape="box"] node[color="#FF6347"] right[label=" right child ", Shape ="ellipse"] root -> {left,right}[label=" parent "] left -> subnode[label=" parent "] {rank=same; left, right} }Copy the code
  • root,left,right,subnodeFor the node. Nodes can be defined in advance as followsRoot [label=" root ", shape="box"], can also be used directly when defining edge relationshipsLeft -> subnode[label=" parent "].
  • Node is used to define the default properties of a node, scoped from this definition to the next. The default values are overridden by properties set for a particular node.
  • []Properties that can be set for graphs, nodes, and edges.
  • rankDefinition sets the effect of nodes on the same line to assist in rendering the image.

Common node attributes

The property name The default value instructions
label node name Node display content
color black Node border color
fontcolor black The font color
fillcolor The background color
fontname Times-Roman The font
fontsize 14 The font size
shape ellipse Shapes,box, Ellipse, Circle, diamond, Plaintext, Point, triangle, invtriangle
style Pattern, eg. Bold, dashed module, dotted box, filled
image Background image address

Shape sample

digraph demo {
    bgcolor="floralwhite"
    "box"[shape=box]
    "polygon"[shape=polygon,sides=7] 
    "ellipse"[shape=ellipse]
    "circle"[shape=circle]
    "point"[shape=point]
    "triangle"[shape=triangle]
    "invtriangle"[shape=invtriangle]
    "plaintext"[shape=plaintext]
    "diamond"[shape=diamond]
}
Copy the code

3, The use of edge

Digraph demo {label=" example "bgcolor="beige" // Define node default property node[color="grey"] // define node root[label=" root ", Shape ="box"] left[label=" left child ", shape="box"] node[color="#FF6347"] right[label=" right child ", Shape ="ellipse"] root -> {left,right}[label=" parent "] edge[color="#FF6347"] left -> subnode[label=" parent "]  {rank=same; left, right} }Copy the code
  • Edge is used to define the default property of an edge. Similar to node, the scope is intercepted from one definition to the next. Properties set for a particular edge override the default values.

  • Essentially, node and edge are also nodes, special nodes reserved to describe node attributes and edge attributes

  • Undirected graph relations use — description, directed graph relations use -> description

Common edge attributes

The property name The default value instructions
label Describe the relationship between
color black The arrow color
fontcolor black Relational text color
dir forward Set directions: forward,back,both, None
arrowhead normal Arrow head shape. Box, crow, Diamond, dot, None, normal, vee Arrow in the documentClick here to
arrowtail Arrow tail shape
arrowsize 1.0 Size of the arrow
style Pattern, eg. Bold, dashed module, dotted box, filled
lhead When compound is true, lhead is used to specify the cluster to which the edge points
ltail Similar to ltail

Arrowhead sample

digraph demo { bgcolor="floralwhite" rankdir=LR "box"->"crow"[arrowhead=box] "crow"->"curve"[arrowhead=crow] "curve"->"diamond"[arrowhead=curve] "diamond"->"dot"[arrowhead=diamond] "dot"->"inv"[arrowhead=dot] "inv"->"none"[arrowhead=inv] "none"->"normal"[arrowhead=none] "normal"->"tee"[arrowhead=normal] [arrowhead=lcrowortee]} [arrowhead=lcrowortee]}Copy the code

4. Some examples

subgraph

A graph can contain multiple subgraphs, and subgraphs can also nest subgraphs. The subgraph name must be Cluster *, otherwise it will be rendered directly as a node.

digraph demo {
    bgcolor="beige"
    subgraph cluster_husband {
        node[color="grey"]
        {"Dad"."Mother"} - >"我"
    }
     subgraph cluster_wife {
         {"Father"."Mother-in-law"} - >"Wife"
     }
     "我" -> "Wife"[label="Husband and wife", dir="both"]
     {rank=same; "我"."Wife"}}Copy the code

Render as follows:

The list

digraph demo { bgcolor="beige" rankdir=LR; node [shape="record", Height = 0.1] node0 [label = "{< f1 > G | < f2 > next}"] node1 [label = "{< f1 > E | < f2 > next}"] node0: f2 - > node1: f1}Copy the code
  • rankdir=LRDescribe the image as horizontal
  • Separated by | served in mapped nodes show as a delimiter
  • Strings enclosed in <> are called anchor points
  • node0[label="{<f1> G | <f2> next}"], {} describes multiple segmentation bits and in the same noderankdirconsistent

B tree

digraph demo { bgcolor="beige" node [shape="record", height=.1] node0[label="<f1> A | <f2> B | <f3> C | <f4> "] node1[label="<f1> D | <f2> E | <f3> F"] node2[label="<f1> G |  <f2> H | <f3> I"] node0:f1 -> node1:f1 node0:f2 -> node2:f1 }Copy the code