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.

No comments:

Post a Comment