serkef
serkef

Reputation: 319

JgraphT export to dot file

I am building a project on graph theory algorithms and for this I use JGraphT. I have built completely my graph and I work on it the past couple of months. Now I want to export it so I can visualize it in Gephi. I don't want to use JGraph and Java visualization since I already have enough of code and I want to keep it simple. I want to use DOTExporter class from JgraphT. I have reached to a point were I export fine vertices and edges, but not edges weights.

So this is my export function. I don't know how to implement ComponentAttributeProvider interface and cannot find my way out of this mess.

Any ideas what I should put instead of null, null?

static public void exportGraph(){
    StringNameProvider<CustomVertex> p1=new StringNameProvider<CustomVertex>();
    IntegerNameProvider<CustomVertex> p2=new IntegerNameProvider<CustomVertex>();
    StringEdgeNameProvider<CustomWeightedEdge> p3 = new StringEdgeNameProvider<CustomWeightedEdge>();
    DOTExporter export=new DOTExporter(p2, p1, p3, null, null);
    try {
        export.export(new FileWriter("graph.dot"), g);
    }catch (IOException e){}
} 

I have done something like this

ComponentAttributeProvider<CustomWeightedEdge> edgeAttributeProvider =
   new ComponentAttributeProvider<CustomWeightedEdge>() {
        public Map<String, String> getComponentAttributes(CustomWeightedEdge e) {
            Map<String, String> map =new LinkedHashMap<String, String>();
            map.put("weight", Double.toString(g.getEdgeWeight(e)));
            return map;
        }
   };

Upvotes: 3

Views: 4279

Answers (2)

Fillipos Christou
Fillipos Christou

Reputation: 95

For future reference, since question is kind of old and solution no longer working according to Gewure.

If you don't restrict yourself to the DOT format, you can manage it by using the graphML format and the API org.jgrapht.nio.graphml.GraphMLExporter<V,​E>.setExportEdgeWeights(true). Works for jgrapht 1.5.1.

Upvotes: 1

serkef
serkef

Reputation: 319

ok did it. For anyone else looking how to export weighted graph from jgrapht to gephi with dot file

static public void exportGraph(){
    IntegerNameProvider<CustomVertex> p1=new IntegerNameProvider<CustomVertex>();
    StringNameProvider<CustomVertex> p2=new StringNameProvider<CustomVertex>();
    ComponentAttributeProvider<DefaultWeightedEdge> p4 =
       new ComponentAttributeProvider<DefaultWeightedEdge>() {
            public Map<String, String> getComponentAttributes(DefaultWeightedEdge e) {
                Map<String, String> map =new LinkedHashMap<String, String>();
                map.put("weight", Double.toString(g.getEdgeWeight(e)));
                return map;
            }
       };
    DOTExporter export=new DOTExporter(p1, p2, null, null, p4);
    try {
        export.export(new FileWriter("graph.dot"), g);
    }catch (IOException e){}
} 

Upvotes: 8

Related Questions