graphviz

graphviz is a cool renderer thing for diagrams

table of contents

using it

you can use it online, or, if you install it to your computer, the command to run it will be the layout plugin you want to use, for example dot

examples

this awk script will take output of irc's LINKS command, and convert it to an undirected graphviz graph. change $5 and $6 to where the two server names are in your log format.

sometimes reversing the LINKS output with tac(1) or sorting with sort(1) before feeding it into awk seems to make graphviz have a better layout.

this assumes the letters of servernames are unique since it does weird broken escaping instead of just quoting the servernames. it will break, for example, if you have both me.ow.cat and meow.cat
BEGIN {
	"date -u '+%Y-%m-%d %H:%M:%SZ'" | getline time;
	print "graph l {"
}
{
	from="x" $5;
	to="x" $6;
	gsub("[\\.-]","",from);
	gsub("[\\.-]","",to);
	print from " [label=\"" $5 "\"]; ";
	if (from != to) {
		print to " -- " from ";"
	}
}
END {
	print "curtime [label=\"generated " time "\"; shape=\"box\"];";
	print "}"
}

reference

digraph/graph

graph is for showing relations. it uses -- to connect items.

graph MyNiceGraph {
	kitty -- cat;
}

digraph is for showing directed stuff, like flowcharts. it uses -> to connect items.

digraph MyNiceDigraph {
	cat -> meow;
	cat -> hiss;
}

the word after them is the id, which can be pretty much anything you want. it is for differentiating which graph you want to render if you have multiple in a file. you'll have to quote it if you want to use weird characters.

setting defaults for nodes and edges

there are special names called node and edge that apply to all nodes and all edges respectively by default.

digraph Rectangles {
	node [shape="box"];
	edge [arrowsize=0.5];

	ovals [shape="ellipse"];
	rectangles -> ovals [label="are much nicer than"];
}

make lines less bendy

by default, relations are shown with a spline, which makes a smooth curve when possible. you can set the splines option to change this. the value ortho will force 90° lines, but this often gives horrible results with large gaps and overlaps for non-trivial graphs. polyline uses a sequence of straight lines.

digraph ItsPolyTime {
	splines=polyline;

	h:w -> fox:e;
	fox:n -> h:s;
}

graphviz sadly does not have a "squared" lines option[1]. however, this can be emulated by creating invisible nodes to route lines where you want.

in this example, we also use dir=back backwards line and rank=same trickery to make graphviz' ranking algorithm put nodes in the right places.

digraph TheLetterH {
	splines=polyline;

	hidden [label=""; color=white];
	hidden2 [label=""; color=white];

	{rank=same; h; fox}
	{rank=same; hidden; hidden2}

	fox -> h;
	h -> hidden2:n [arrowhead=none];
	hidden:n -> hidden2:n [arrowhead=none];
	fox -> hidden:n [dir=back];
}

direction

rankdir sets the rank direction, which affects the direction of the graph when using the dot layout

the argument is a combination of T (top), b (bottom), L (left), and R (right). for example LR goes left-to-right

digraph Sideways {
	rankdir=LR;

	left -> right;
}

footnotes

  1. why not? seems like it would be easy to implement, just "ortho without the gaps in the lines" would probably be fine, perhaps with some extra padding