Sunday Dec 13, 2009

Graphs, GXL, dot and Graphviz

Sometimes you may want to quickly generate graphs programmatically and view/analyze those. Examples include, inheritance/type relation diagrams of an object oriented program, function call graphs and any other domain specific graphs (reporting chain of your organization chart for example). I find GXL very useful for this. GXL stands for Graph eXchange Language. It is a simple XML format to specify graphs. A simple graph stating that "JavaFX" language is related to "Java" language is as follows:

File: Test.gxl

<gxl>

<!-- edgemode tells this is directed or undirected graph -->
<graph id="langs" edgemode="directed">

<!-- write your nodes -->
<node id="java"/>
<node id="javafx"/>

<-- now write your edges -->
<edge from="java" to="javafx"/>

</graph>
</gxl>

You can also add number of "attributes" to nodes and edges - like color of the edge, style of the edge and so on. For example, "red" color can be specified for an edge as follows:


<edge from="java" to="javafx">
  <attr name="color"><string>red</string></attr>
</edge>

Now that we have written a simple graph with two nodes and a single edge between them, we may want to view it. There are number of tools/libraries to view GXL documents -- I've used Graphviz. Graphviz displays it's own native format called ".dot". Graphviz comes with a set of command line tools. One such tool is "gxl2dot", which as you'd have guessed, can be used to convert a .gxl file to a .dot file.


    gxl2dot Test.gxl > Test.dot

Once converted the .dot file can be opened in Graphviz GUI and we can export it to .pdf/.jpg/.png and so on. This way you can email the graphs to others and/or publish in your blogs/webpages easily.

The converted .pdf file for the above simple graph is here: test.pdf

I've used GXL graphs in a recent debugging tool related to JavaFX compiler. More on that later...

About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder