An Introduction to GraphViz and dot
by Michele Simionato05/06/2004
You must give a presentation tomorrow and you haven't prepared any figures
yet; you must document your last project and you need to plot your most hairy
class hierarchies; you are asked to provide ten slightly different variations
of the same picture; you are pathologically unable to put your finger on a
mouse and draw anything more complex than a square. In all these cases,
don't worry! dot can save your day!
What is dot?
dot is a tool to generate nice-looking diagrams with a minimum
of effort. It's part of GraphViz, an open source project
developed at AT&T and released under an MIT license. It is a high-quality
and mature product, with very good documentation and support, available on all
major platforms, including Unix/Linux, Windows, and Mac. There is an official
home page and a supporting mailing list.
What Can I Do with dot?
First of all, let me make clear that dot is not just another
paint program, nor a vector graphics program. dot is a scriptable,
batch-oriented graphing tool; it is to vector drawing programs as
LaTeX is to word processors. If you want to control every single
pixel in your diagram, or if you are an artistic person who likes to draw free hand, then dot is not for you. dot is a tool for the
lazy developer, the one who wants the job done with the minimum effort and
without caring too much about the details.
Since dot is not a WYSIWYG tool—even if it comes
with a WYSIWYG tool, dotty—it is not primarily an
interactive tool. Its strength is the ability to generate diagrams
programmatically. To fulfill this aim, dot uses a simple
but powerful graph description language. Give dot very high level
instructions and it will draw the diagrams for you, taking into account all the
low level details. Though you have a large choice of customization options and
can control the final output in many ways, it is not at all easy to force
dot to produce exactly what you want, down to the
pixel.
Linux/Unix System Administration Certification
-- Would you like to polish your system
administration skills online and receive credit from the University of Illinois? Learn how to administer Linux/Unix systems and gain real experience with a root access account. The four-course series covers the Unix file system, networking, Unix services, and scripting.
It's all at the O'Reilly Learning Lab.
|
Expecting that would mean to fight with the tool. You should think of
dot as a kind of smart boy, who likes to do things his own way and
who is very good at it, but becomes nervous if the master tries to put too much
pressure on him. The right attitude with dot (just as with LaTeX)
is to trust it and let it to do the job. At the end, when dot has
finished, you can always refine the graph by hand. (dotty, the
dot diagram interactive editor, comes with GraphViz and can read
and generate dot code.) In most cases, you do not need to do
anything manually, since dot works pretty well. The best approach
is to customize dot options, so that you can programmatically
generate one or one hundred diagrams with the least effort.
dot is especially useful in repetitive and automatic tasks,
since it easy to generate dot code. For instance,
dot comes in handy for automatic documentation of code. UML tools
can also do this work, but dot has an advantage over them in terms
of ease of use, a flatter learning curve, and greater flexibility. On top of
that, dot is very fast and can generate very complicated diagrams
in fractions of second.
Hello World from dot
dot code has a C-ish syntax and is quite readable even to
people who have not read the manual. For instance, this dot
script:
graph hello {
// Comment: Hello World from ``dot``
// a graph with a single node Node1
Node1 [label="Hello, World!"]
}
generates the image shown in Figure 1.
Figure 1. "Hello, World!" from GraphViz
Save this code in a file called hello.dot. You can then
generate the graph and display it with a simple one-liner:
$ dot hello.dot -Tps | gv -
The -Tps option generates PostScript code, which is then piped
to the ghostview utility. I've run my examples on a Linux machine with
ghostview installed, but dot works equally well under Windows, so
you may trivially adapt the examples.
If you're satisfied with the output, save it to a file:
$ dot hello.dot -Tps -o hello.ps
You'll probably want to tweak the options, for instance adding colors and changing the font size. This is not difficult:
graph hello2 {
// Hello World with nice colors and big fonts
Node1 [label="Hello, World!", color=Blue, fontcolor=Red,
fontsize=24, shape=box]
}
This draws a blue square with a red label, shown in Figure 2.

Figure 2. A stylish greeting
You can use any font or color available to X11.
Editor's note: or presumably to Windows, if you're not running an X server.
dot is quite tolerant: the language is case insensitive and
quoting the options color="Blue", shape="box" will work too.
Moreover, in order to please C fans, you can use semicolons to terminate
statements; dot will ignore them.
Pages: 1, 2 |
