Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How represent edge weight via JGraphT visualization?

Tags:

java

jgrapht

I tried create a Java JGraphT visualization with Weight, for example:

http://i.stack.imgur.com/KXRrc.png

Now I want to change the edge label (ex: (a:b) to its edge weight (ex: 2). I tried to search in Javadoc but didn't see anything. Please help me thank you!

like image 976
Kyris Avatar asked Oct 07 '22 00:10

Kyris


1 Answers

Those edge labels came from the toString method of the edge object. Therefore you may create your own edge class and override the toString method in such a way that it yields the edge weight.

You may create your custom edge like this:

class MyWeightedEdge extends DefaultWeightedEdge {
  @Override
  public String toString() {
    return Double.toString(getWeight());
  }
}

And then create your graph like this which will make the graph create edges as instances of MyDefaultEdge and therefore display just edge weight in the visualization:

SimpleWeightedGraph<String,MyWeightedEdge> graph
  = new SimpleWeightedGraph<String,MyWeightedEdge>(MyWeightedEdge.class);
like image 180
Karel Horak Avatar answered Oct 10 '22 00:10

Karel Horak