Showing posts with label dot. Show all posts
Showing posts with label dot. Show all posts

1/04/2012

DOT Language

Dot is an abstract grammar defining language. We can use dot to represent graphs also. In this post, I will show how different graphs can be generated with different codes from dot language. I will still use the Java api for dot language which I mentioned in GraphViz Java post.
Since these are just a few examples, you can find other attributes and styles from Graphviz dot attr page or the dot manual.

Simple Directed Graph
digraph G {
    A
    B
    A -> B
    A -> C;
}
Output
Notes
You see that you don't have to introduce each node at the beginning. Any node in the edges will automatically inserted.

Simple Graph without Edges
digraph G {
    A
    B
    C
    D
}
Output
Notes
From this example, we see that we have to introduce all edges if there are no edges that consists of these nodes.

Simple Undirected Graph
graph G {
    A
    B
    A -- B
    A -- C
}
Output


Different Shape and Colors for Nodes
graph G {
    A [shape=box, peripheries=2,color=black, style=filled, fontcolor=white]
    B [style=filled, color=gray84, fillcolor=gray84,fontcolor=white]
    C [color=red, style=filled, fontcolor=white]
    D [shape=polygon,sides=4,distortion=.7]
    A -- B
    A -- C
}
Output
Notes
You can find many different node styles in this manual.

Different Edge Styles
digraph G {
    A
    B
    C
    D
    A -> B [arrowhead=obox, style=dotted, color=red]
    A -> C [taillabel="tail"]
    A -> D [label="g'", weight=4]
}
Output
Notes
Heavier weight means shorter, straighter and more vertical the edge is.

11/13/2011

GraphViz Java

I will introduce Java api for GraphViz. In order to use this api, these are the steps you have to follow:

  • You have to first download graphviz application for your OS.
    You will need dot.exe location in order to run the application correctly.
  • In the java api link, you should download the source file. (GraphViz.java)
  • Include this source to the project.
  • You should change the following variables according to your preference and installation.
    TEMP_DIR may be any folder.
    DOT is the location of the dot.exe in graphviz installation folder.
  • Write this main method in any class and test your installation and output.
    import java.io.File;
    
    public class GraphVizExample {
        public static void main(String[] args) {
            GraphViz gv = new GraphViz();
            gv.addln(gv.start_graph());
            gv.addln("A -> B;");
            gv.addln("A -> C;");
            gv.addln(gv.end_graph());
    
            String type = "gif";
            File out = new File("D:/out." + type);
            gv.writeGraphToFile(gv.getGraph(gv.getDotSource(), type), out);
        }
    }
    
  • This application will generate a graph to the file D:/out.gif
  • The output image should be:
  • You can generate more complex graphs by applying the dot language specification.
You should change some functions for example GraphViz.start_graph() function to change the graph type.