Search code examples
jgraphtgraph-drawingjgraph

Graph Layout Algorithms for JGraphT


I'm using JGraphT to create a graph and I want to be able to visualise and manipulate it. There are some examples that show how to visualise the graph, but it seems that it involves quite a lot of manual code to layout the vertices, etc. I was wondering if there was any graph layout algorithm that could automate this process already in JGraph with a small example. Mostly the graphs I'm drawing are Directed Acyclic Graphs. I have already drawn the graphs by exporting them to .dot format and display it using dot, but I need a little interaction now.


Solution

  • Since JGraph seems to now be mxGraph, but JGraphT embed JGraph 5.13, it's not that easy but I've found this doc and the following piece of code is working:

        // this a a JGraphT graph
        ListenableDirectedGraph<TableColumn, DefaultEdge> dependencyGraph = getDependencyGraph();
    
        JGraphModelAdapter adapter = new JGraphModelAdapter(dependencyGraph);
    
        JGraph jgraph = new JGraph(adapter);
    
    
        JGraphLayout layout = new JGraphHierarchicalLayout(); // or whatever layouting algorithm
        JGraphFacade facade = new JGraphFacade(jgraph);
        layout.run(facade);
        Map nested = facade.createNestedMap(false, false);
        jgraph.getGraphLayoutCache().edit(nested);
    
        JScrollPane sp = new JScrollPane(jgraph);
        this.add(sp);