paulgorman.org/technical

Graphviz

Graphviz is an open source graph drawing tool. It takes a text-based graph description, and outputs graphs in a variety of formats, like SVG, PDF, PNG, or image map.

# apt-get install graphviz

Strangely, ASCII text is not one of the Graphviz output formats. However, a number of other packages can do so, including graph-easy (Debian package ‘libgraph-easy-perl’).

Graphviz source files are generally named with ‘.gv’ or ‘.dot’ extensions.

$ dot -Tsvg map.dot > map.svg

Graphviz offers a number of layout engines: dot, neato, sfdp, fdp, circo, twopi, nop, nop1, osage, patchwork, lefty. Try different ones, as some will work better than others for particular graphs.

Graphviz supports C-style comments: /* A comment! */

graph "Mess Hall" {
	layout = "sfdp";
	label = "Mess Hall";
	outside [label="Outside", shape=none];
	dining [label="1. Dining Room"];
	shroom [label="2. Shroom Garden"];
	kitchen [label="3. Kitchen"];
	cellar [label="4. Secret Cellar"];
	distillery [label="5. Distillery"];
	larder [label="6. Larder"];
	outside -- dining;
	outside -- kitchen;
	dining -- kitchen;
	kitchen -- shroom;
	dining -- shroom;
	larder -- shroom;
	larder -- kitchen;
	distillery -- shroom;
	distillery -- dining;
	distillery -- cellar [label="Hidden Stairs"];
}

It is somewhat possible to specify the direction an edge leaves one node and connects to another by specifying “ports” on nodes with cardinal directions (n, e, w, s, ne, nw, se, sw).

graph "Ports Map" {
	layout="dot";
	node [shape="box"];
	label="Ports Map";
	ntower [label="North Tower"];
	wroom [label="West Room"];
	hall [label="Grand Hall"];
	stower [label="South Tower"];
	store [label="Store Room"];
	barracks [label="Barracks"];
	ntower:s -- hall:n;
	hall:s -- stower:n;
	wroom:e -- hall:w;
	ntower:se -- store:nw;
	stower:e -- barracks:w;
	{rank=same; hall; wroom; store;}
	{rank=same; stower; barracks;}
}

Directed graphs work similarly:

digraph TrafficFlow {
	node [shape=box];  zeus; athena; hera; mercury;
	node [shape=circle]; drop; accept;
	zeus->mercury;
	athena->mercury;
	hera->zeus;
	mercury->accept;
	zeus->drop;
	hera->drop;
	overlap=false
	label="TrafficFlow\nlayed out by Graphviz"
	fontsize=12;
}

There’s a nice viewer program called xdot.

# apt-get install xdot