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="< 18n---------n2-will buy/1-won't buy", shape=plaintext];
    age2 [label="18 - 35n---------n0-will buy/4-won't buy", shape=plaintext];
    age3 [label="36 - 55n---------n5-will buy/3-won't buy", shape=plaintext];
    age4 [label="> 55n---------n5-will buy/0-won't buy", shape=plaintext];
    income [label=Income, style=filled, fillcolor=grey89, pos="111,170"];
    high [label="highn---------n0-will buy/1-won't buy", shape=plaintext];
    low [label="lown---------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="Marriedn----------n0-will buy/3-won't buy",
             shape=plaintext];
    single [label="Singlen----------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.