LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to Linux Subscribe to Newsletters
Linux & Unix > Excerpts >

An Introduction to GraphViz and dot

by Michele Simionato
05/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.

Learning Lab TigerLinux/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.

'Hello, World!' from GraphViz
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.

a stylish greeting
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

Next Pagearrow




Tagged Articles

Post to del.icio.us

This article has been tagged:

graphviz

Articles that share the tag graphviz:

An Introduction to GraphViz and dot (3 tags)

View All

visualization

Articles that share the tag visualization:

A Bright, Shiny Service: Sparklines (15 tags)

Styling RDF Graphs with GSS (8 tags)

Big Lists in Small Spaces (3 tags)

Deploying BIRT (3 tags)

Adding Data Visualization to Python for Producing Graphs (3 tags)

View All

dot

Articles that share the tag dot:

An Introduction to GraphViz and dot (2 tags)

View All

Recommended for You

  1. Cover of UNIX in a Nutshell
    UNIX in a Nutshell
    Print: $29.95
  2. Cover of SpamAssassin
    SpamAssassin
    Print: $29.99
    Ebook: $23.99
  3. Cover of Essential CVS
    Essential CVS
    Print: $39.95
  4. Cover of GNU Emacs Pocket Reference
    GNU Emacs Pocket Reference
    Print: $9.95
    Ebook: $4.99

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com