DOT language for graph
GraphViz package is a set of tools to describe, automatically layout and manipulate graphs, which is of great significance in connection and relationship analysis and illustration. The core module of GraphViz is the layout and render engines which generate neat and elegant visualization of graph in various common formats from a descriptive small language (the graph file language or the DOT language). This post will represent a brief introduction of the DOT language and show some applications that utilize the power of GraphViz.
GraphViz & DOT: Hello World
First section of this post, let’s create a simple graph via GraphViz in DOT language.
Create a plain text file with the following content and save it as g.dot
.
|
|
Then use dot
command to convert the dot file to a visual graph. The dot
command
is part of the graphviz
package which can easily be installed via package managers in
most of Linux Distributions. (For example, use sudo apt install graphviz
in Ubuntu or its derivations.)
|
|
By default, the graph of hello-world.svg
will look like the following one.
This is much easier to make simple graph in this method then drawing them manually on either a piece of paper with pencil or canvas of a drawing program with mouse.
Syntax
After the first example, let’s have a more specific introduction about the DOT language by creating some simple graphs.
The syntax of the DOT language is quite simple, since all you need to specify in order to fully describe a graph are the nodes (the vertices) as well as the edges in between. Thus in DOT language, a typical graph is also specifying these entities.
|
|
Configuration & Customization
This section lists some simple attributes used to adjust nodes and edges. A complete list can refer to the official document.
Node properties
shape=name
: the shape of the node. a full list is available at Node List;width=d
andheight=d
: specify the size of node;style=filled
: whether the node is filled, usestyle=""
to clear the fill;label=text
: the label of the node, can also be simple HTML code for a flexible content;color=c
: the color or the outline of the shape or the edge, likecolor="#0091cc"
orcolor=red
;fontcolor
: the color of text;fillcolor
: the filled color of the node;
Edge properties
style=dotted|dashed|bold|solid|none
: the line style of an edge;arrowType=normal|vee|dot|odot|empty
: the arrow style of an directed edge, a full list is available at Arrow List;label
: the label of the node;color
: the color of the edge;
Layout and layout engine
Layout is the relative position of nodes in a graph.
layout properties
Some common options for the graph can be used to adjust the layout.
size="x,y"
: the maximum size of the rendered graph in inch;margin=f
: the margin of the rendered graph, accounts for the size of the result (more like thepadding
property in CSS);rankdir=LR|RL|BT|TB
: drawing left to right (LR
), right to left (RL
), bottom to top (BT
) or top to bottom(TB
the default option);nodesep=f
: the minimum separation between nodes.
layout engine
So far, all of the graphs are converted via dot
command. Apart from that, GraphViz also provides some other layout engines. A common set of engines is listed as the following table,
while more engines may available within your installation package.
Name | Description |
---|---|
dot |
drawing directed graphs |
neato |
drawing undirected graphs |
twopi |
radial layouts of graphs |
circo |
circular layout of graphs |
fdp |
drawing undirected graphs |
sfdp |
drawing large undirected graphs |
patchwork |
tree maps |
The layout engine can be selected by either the name of the command or
the -K
command option (the latter is prior). This can also be selected via graph property
layout
(such as layout=dot
).
Application
In this section, let’s explore some of tricks and applications of GraphViz.
Export to common image files
GraphViz in implemented as a open and extensible architecture. Thus, it can render the graph to various of targets specified by plugins. Besides, as a command line tool, the output of the application can be piped to other utilities for further processing.
Traditionally, GraphViz support the following types: ps
(post script), svg
/svgz
(scalable
vector graphics), png
/gif
(bitmap file), etc. However, most packaging of the GraphViz will
contain several extensions providing more export varieties. For a list of supported export
target of your installation, use the -T
option with a invalid name:
|
|
For a single file type, there may also be several supported plugins, which can be specified by
append the variety name leading by a comma, like dot -Tpng:cairo:gd
.
Similarly, a list of supported items can be retrieved by providing a invalid variety.
|
|
Jupyter Notebook
As mentioned in previous post, there are several handy bindings of GraphViz in the world of
Python. This sections illustrate a example of using GraphViz package for representing a
decision tree constructed by a tree model sklearn
package (a machine learning package).
|
|
The decision tree trained from the iris
data set is just represented within the jupyter notebook
Actually the variable is a plain string contains the source of the dot file. The source for the graph illustrated above is listed as follows (sklearn
use HTML
format to fill the content of graph):
Besides, the PyEDA
package also utilize the GraphViz engine to visualize binary decision
diagram (BDD) and reduced ordered BDD (ROBDD). Thus a nice visualization can instantly
represented in Jupyter notebook when handling boolean expressions, as well as its conversions
and simplifications. The official document of PyEDA
also provides a detailed tutorial, which
is available at here.
Port to Web Platform
Modern web platform provides a rich and growing set of APIs that empowers variety of fancy application. One of the recent added API is Web Assembly which expose a more native interface for web application to utilize the computation capability of client. With assist of some compiler backends (such as emscripten), the layout engine of GraphViz can be compiled into Web Assembly format and execute directly within user browser. A ready-to-use library named Viz.js is available at GitHub (mdaines/viz.js), which also provides am online demo at http://viz-js.com/.
The API exposed by Viz.js is quite simple and clear. It just consumes the source of a graph and generates the image. However, this process maybe kinds of time-consuming, and will block the UI thread (the interaction of the web page), which is quite annoying, especially on mobile devices with limited computing capability. A solution to this is problem is using a dedicate thread for calculating the layout, which can be implemented as a Web Worker.
I have also made a simple demo for representing DFA converted from regular expressions, which use this approach to automatically layout the nodes within a modern browser. You can refer to the source for integration of Viz.js to webworker and packaging every piece of code with rollup.js.