Search code examples
javagraphgraph-visualization

how to plot a graph from collections contents in java


i have parsed a raw xml file and fetched many vertices and the edges that i would need, but i cant able to actually plot it... i.e, suppose i have the hashmap containing the folowing key-->value pair... the HashMap is of following type pair "String,ArrayList of String"

A--------->[B, C]

B--------->[H, J]

Y--------->[Z]

for the above contents, i want a graph similar to this: enter image description here i can't able to find the solution for this anywhere.


Solution

  • Try using the dot language, it is a simple text format.

    But there is even a pure Java lib for it: https://github.com/nidi3/graphviz-java

    import static guru.nidi.graphviz.model.Factory.*
    Graph g = graph("example1").directed().with(node("a").link(node("b")));
    Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1.png"));
    

    Adapt this example ... and it should work.

    There are other solutions see:

    Java graph library for dynamic visualisation

    In the worst case just generate dot text and render outside of java using graphviz.