Graphviz - Why draw when you can code?

by Christopher Roach


As promised, I am back with my second blog post for the day. This one is on an Open Source visualization project called Graphviz that was developed by AT&T's research lab. I found this project in a fit of rage as I was trying to draw a graphical representation of a decision tree for an article I just finished this weekend and I've got to tell you, I'm in love with it so far.




I'm not necessarily the best at using drawing tools for the computer. Programs such as Visio and OmniGraffle, though quite useful, frustrate me to no end. Thus, I tend to get a little bit aggravated at these products from time to time and this week was no different (luckily my apartment's walls are insulated well enough, otherwise, my neighbors would have assumed my vocabulary was made up of only four letter words).




This past week I was using OmniGraffle (which I actually think is a really great program) and it was taking way too long to draw and make changes to a graph of a decision tree for my article. I had spent hours with the application creating my diagram and was only about halfway complete when I reached the limit of how many objects I can place in the diagram (just in case you don't know, the trial version of OmniGraffle has this limit, not the full-fledged version). Since, I'm somewhat allergic to paying for software and I wanted a much easier way to complete my diagram, I decided to take a look for something else I could use, and low and behold, I found it. The holy grail of graphing software for programmers—Graphviz. This program is beautiful. It allows me to create graphical diagrams the way the good lord intended for me to—by scripting them, of course.




Seriously though, Graphviz is a really nice tool for creating diagrams that combines an easy to use textual description language with a set of graphical visualization algorithms. It can export these diagrams to a plethora of different formats (SVG, JPEG, and PDF to name just a few). While I've not had a lot of experience with it, I can definitely tell you that it will be my tool of choice for creating simple diagrams for class, work, articles, you name it. I was able to learn enough of the dot language (dot is the simple text language that you'll use to describe your diagram) in about an hour to complete my diagram. Total time for learning and creating my diagram's description—1.5 hours. Total time to get halfway to completion with OmniGraffle— approximately 2 hours.




Now, while I'm gushing over this new tool, I don't want you to think this is a silver bullet or anything. Like I said earlier, I've had very little experience with Graphviz, but from what I've seen, the more complex the graph gets, the better off you may be using a tool more like Visio or OmniGraffle. Nevertheless, for most of the jobs I need to do, this tool will do me just fine, and as I stated before, its an absolute breeze to use. And, even though this tool was originally created with other operating systems in mind, it does have an OS X port which looks really nice and it was done so well as to have actually won an Apple design award in 2004.




Just to give you an idea of how easy it is to produce a decent looking diagram, I've included below the dot file that I created to generate my decision tree diagram for my article below:





digraph dtree {
node [fontsize=10, shape=box];

age [label=Age, style=filled, fillcolor=grey89];
age1 [label="< 18\n---------\n2-will buy/1-won't buy", shape=plaintext];
age2 [label="18 - 35\n---------\n0-will buy/4-won't buy", shape=plaintext];
age3 [label="36 - 55\n---------\n5-will buy/3-won't buy", shape=plaintext];
age4 [label="> 55\n---------\n5-will buy/0-won't buy", shape=plaintext];
income [label=Income, style=filled, fillcolor=grey89, pos="111,170"];
high [label="high\n---------\n0-will buy/1-won't buy", shape=plaintext];
low [label="low\n---------\n2-will buy/0-won't buy", shape=plaintext];
no1 [label="Won't Buy", style=filled, fillcolor=burlywood];
yes1 [label="Will Buy", style=filled, fillcolor=burlywood];
no2 [label="Won't Buy", style=filled, fillcolor=burlywood];
marital_status [label="Marital Status", style=filled, fillcolor=grey89];
yes2 [label="Will Buy", style=filled, fillcolor=burlywood];
married [label="Married\n----------\n0-will buy/3-won't buy",
shape=plaintext];
single [label="Single\n----------\n5-will buy/0-won't buy",
shape=plaintext];
no3 [label="Won't Buy", style=filled, fillcolor=burlywood];
yes3 [label="Will Buy", style=filled, fillcolor=burlywood];

age -> age1 [arrowhead=none];
age -> age2 [arrowhead=none];
age -> age3 [arrowhead=none];
age -> age4 [arrowhead=none];
age1 -> income [arrowhead=none];
income -> high [arrowhead=none];
income -> low [arrowhead=none];
high -> no1 [arrowhead=none];
low -> yes1 [arrowhead=none];
age2 -> no2 [arrowhead=none];
age3 -> marital_status [arrowhead=none];
age4 -> yes2 [arrowhead=none];
marital_status -> married [arrowhead=none];
marital_status -> single [arrowhead=none];
married -> no3 [arrowhead=none];
single -> yes3 [arrowhead=none];
}




Now, keep in mind that the code listed above was not written to be beautiful, or reused, or for that reason, even legible. I wrote it after only studying the documentation on the dot language and graphviz for about an hour, not to mention that I just wanted to get a usable diagram up and running as soon as possible. That said, I do think it is a good example of just how easy it is to use Graphviz to produce rather good looking diagrams. Below is the product of the script above after being ran through the Graphviz visualization tool and exported to a JPEG.



image


So, that's Graphviz. My recommendation is that you go out and download it and give it try. There's a very good chance that it may save you some time on your next project or research paper. However, before I sign off on this article there is one more thing that I want to share with you all. If you are an Emacs user (as I am) then it might be nice to have a dot-mode that allows you to edit *.dot files in Emacs with syntax highlighting and all. You can find the emacs lisp file here. Below is the line you need to add to your .bashrc file (if your using bash as your shell) if you want to be able to open *.dot files from Emacs.






(load-file "PATH_TO_FILE/graphviz-dot-mode.el")





That's it, go out and download everything and give it a try. If nothing more, it is rather fun to play around with. In the meantime I'll play around with Graphviz myself and see if I can't put together a more in depth article for MacDevCenter.




Stick around for my next posting on Python documentation.


11 Comments

otto
2005-10-31 00:03:02
Even better: Import your .dot file into OmniGraffle
The concept of Graphviz and dot are great for authoring, but to be honest, OmniGraffle's diagrams just look better - a lot better. To get the best of both worlds, just use them together: Import your .dot file into OmniGraffle, use the Layout Inspector to fine-tune the layout, and finally apply some beautiful styles. See http://paco.to/blog/000040.html for an introduction.
ediathome
2005-10-31 00:36:20
Even better: Import your .dot file into OmniGraffle
I totally agree. The looks of OmniGraffle-diagrams is just awesome and thereīs no other apllication to compete with it. I use OmniGraffle almost every day to design worksheets for school. Itīs my absolute favourite app.
jimothy
2005-10-31 05:23:16
Allergic?
I don't mean to venture far off topic, but you're "somewhat allergic to paying for software"? I hope you're likewise allergic to being paid for the work you do, or I'm afraid you'll fall into the the hypocritical camp.


If you think OmniGraffle is a fine program that you use often, you owe it the developers to pay for a license. It's much more reasonably priced that the abomination that is Visio.


Graphviz may be free, and it may, in some cases, be a better alternative to drawing. But it hardly has the polish of commercial software such as OmniGraffle (heck, most other commercial hardly has the polish of OmniGraffle), and as pointed out in other comments, OmniGraffle can complement Graphviz.

leejoramo
2005-10-31 08:21:11
Graphviz -> OmniGraffle
I agree with the other comments. For me, Graphviz and OmniGraffle are more complementary than replacements for each other. In fact, I first learned Graphviz's dot language four years ago as a way to automate graph creation in Graffle.
eddieparker
2005-10-31 09:42:53
GraphViz and DokuWiki
There's a great plugin to integrate some good Wiki software (DokuWik) with GraphViz, that I use a lot in my design documents. It's fantastic!


http://wiki.splitbrain.org/ <-- DokuWiki
http://wiki.ioslo.net/dokuwiki/graphviz <-- Integration

christopher_roach
2005-10-31 11:25:48
Even better: Import your .dot file into OmniGraffle
I'll have to offer you my thanks for this post, since I had no idea that you could import dot files into OmniGraffle. This may just be the thing that pushes me over the edge and gets me to purchase the full version of OmniGraffle since I too agree that the OmniGraffle output does look a bit better. Although, I don't think that Graphviz is anything to scoff at (my image above is not a good example of Graphviz's ability, since I had to shrink the actual image to get it to fit within the limits that O'Reilly's weblog app imposes).
christopher_roach
2005-10-31 11:36:40
Allergic?


First let me state that I am most likely a hypocrite, but that's mainly becuase I am a software engineer with student loan debt up to the "you know what" who still lives off Ramen like he was back in college. Once I get the ol' school loans paid off (or if pigs learn to fly and they start paying software engineers enough to keep up with the cost of living) I'll be more likely to endeavour into the realm of paid software. Hey, at least I don't steal the software I do use and I do pay for all my music downloads via iTMS, that's a start right?



Anyway, I do agree with you that OmniGraffle is very reasonably priced and a good product, the problem is that I just don't use it enough for me to justify buying it. I've really only had a use for it a handfull of times in the last year and my demands are so minimal that, typically, graphviz will do just fine for me. But, as was pointed out to me below, you can import dot files into OmniGraffle, very cool! If this works seamlessly, then I'll probably pony up the cash for OmniGraffle and use it much more often.

jimothy
2005-10-31 13:27:13
Allergic?
You might already have a licensed copy of OmniGraffle. It's included on PowerBooks and PowerMacs, though I don't think it's included with iBooks and iMacs.
christopher_roach
2005-10-31 13:46:50
Allergic?
You know, that's what I thought. I could have swore that I had a fully licensed copy at one point in time, but then after I upgraded my pbook to Tiger--wham--unlicensed copy. I think I'm going to have to go back through the install disks that came with my laptop and see if I can't find a licensed version on there somewhere.


Thanks for reading the blog and for posting your comments, though. They are always most appreciated.

otto
2005-11-03 00:00:15
Allergic?
I think I'm going to have to go back through the install disks that came with my laptop and see if I can't find a licensed version on there somewhere.


Last time I checked, they weren't stored in some Library folder where I expected them, but in a hidden directory (name starting with a dot) at the top level of the disk.

tmo9d
2005-11-04 08:37:44
Yes, allergic, to paying for software
Than call me a hypocrite as well, I'm very allergic to paying for software as well AND I get paid to write software. Seriously? Is it a crime to not want to spend money on a tool when there is a good open source alternative.


Tell me, what happens if I want to customize OmniGraffle? Can I port OmniGraffle to other platforms? What if I want to use OmniGraffle to generate graphs on the server-side, will they ask me to pay per user?


And, what's this "polish" comment you wrap up with? Let people interested in "polish" pay for polish. I guess emacs doesn't have the "polish" or BBEdit, and since I've used BBEdit and think it is a good tool, by your logic I'm impelled by the magical forces of commerce to purchase a license for it.