Vidar Hokstad V2.0

Home Blog

2008-03-30 12:59 UTC Making Graphviz output pretty with XSL

Posted in: , , , , ,
A couple of years back I posted some about an XSL transformation I used to clean up Graphviz output to make my diagrams prettier. These days I use Omni Graffle for most of my diagramming needs, and making diagrams look nice with it is trivial, but Graphviz is still my tool of choice whenever I need to generate diagrams from code automatically, because its automated layout is reasonably good.

Last year I got an e-mail from Ryan Shea who asked about the stylesheet I'd used, and we went back and forth and fixed various problems and made it a lot cleaner. Just the other day I wanted to generate a Graphviz graph and looked up the stylesheet again, and figured it was about time I actually posted it so others can benefit.

Here's a "hello world" Graphviz diagram:

digraph G {
        Hello->World

Hello [style=filled fillcolor=lightblue] World [style=filled fillcolor=lightgrey] }

Here's how Graphviz renders it (with "dot -Tsvg"). The image on the left (if it shows up for you) is the SVG file. The image on the right is a screenshot in case your browser doesn't support SVG:


Fair enough, but we can do a lot better:


The XSL is pretty simple and can be found here. NOTE: The Graphviz SVG output has changed quite a few times - if the XSL linked here doesn't work for you, I'd very much appreciate if you post what version of Graphviz you've tested it with in the comments, and/or a sample of the SVG, so I can fix the XSL

The one caveat is that for any color you want turned into a gradient, you need to add an extra "linearGradient" tag to the XSL file like the ones that are already there.

You will need an XSL processor to convert the files. Personally I use "xsltproc" on my Linux box, which is from libxslt RPM if you're on Fedora Core or Redhat EL - chances are it's already installed if you use Linux. If so, you convert your Graphviz files like this (assuming your dot file is names "hello.dot" and you've saved the XSL file as notugly.xsl):

dot -Tsvg hello.dot >hello.svg
xsltproc notugly.xsl hello.svg >hello-notugly.svg


Comments

2008-04-01 00:56 UTC
Mark Thomas
Nifty! My graphviz output is usually black and white so this is a huge improvement...
2008-04-18 16:42 UTC
Hi Vidar,

Thanks a million for posting this. I find graphviz very useful for generating diagrams instead of drawing them "by hand", and it's great to have a way of prettying up the output.

Thank you !

Cheers, Freddy

2008-04-27 13:48 UTC
Michael Kennedy

This is great stuff. In the interest of helping others who find this blog post useful, I've created an updated version of the XSL that contains all of the possible GraphViz colours, and made it available here:

http://s3.digitalfort.com/posts/notugly-full.xsl

Enjoy, and thanks for sharing.

MK

2008-05-01 23:50 UTC
Andrew
When I try this using my old reliable Saxon9, I get this:

C:\>type graphviz.DOT.txt | "C:\Program Files\ATT\Graphviz\bin\dot.exe" -Tsvg -o graphviz.svg

C:\>java.exe -jar C:\saxon9.jar -s:C:\graphviz.svg -xsl:C:\notugly.xsl -o:C:\graphviz-notugly.svg Warning: at xsl:stylesheet on line 3 of file:/C:/notugly.xsl: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor Error java.net.ConnectException: Connection timed out: connect Transformation failed: Run-time errors were reported

My SVG from DOT is fine and displays correctly. I'm also able to manually reach each HTTP url in the .xsl Any ideas?

2008-05-02 05:02 UTC
Vidar Hokstad
I've never used Saxon, but my intial thought is that regardless your best bet would be to try to turn off whatever option makes it try to retrieve the urls - having your parser go out to retrieve the namespace URI's is really completely pointless, as it can't use whatever information it finds there anyway.

Post a Comment

Basic HTML allowed.

About me

E-mail: vidar@hokstad.com
Skype: vhokstad
View my LinkedIn profile

I was born April 21st, 1975, in Oslo, Norway. Since 2000 I've been living in London, UK. I'm married.

I'm working for Aardvark Media as Director of Technology. I'm also currently on the board of SpatialQ, a startup in the GIS space, and an advisor to Skoach, a startup doing a time management app for people with ADD.

Recent posts to my blog

Categories

StumbleUpon My link page

(Links I have stumbled and like)