7

How to use custom vertex labels in JUNG graph visualization?

I am following Jung 2.0 Tutorial where I found that setVertexLabelTransformer() can be used to label the vertices, but these labels cannot be customized, to my knowledge.

For example, the below code produces three vertices, having vertex-labels 1,2,4:

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import java.awt.Dimension;
import javax.swing.JFrame;

public class SimpleGraphView {
    Graph<Integer, String> g;

    public SimpleGraphView() {       
        g = new SparseMultigraph<Integer, String>();
        g.addVertex((Integer)1);
        g.addVertex((Integer)2);
        g.addVertex((Integer)4); 
    }

    public static void main(String[] args) {
        SimpleGraphView sgv = new SimpleGraphView(); 
        Layout<Integer, String> layout = new CircleLayout(sgv.g);
        layout.setSize(new Dimension(800,800));  
        BasicVisualizationServer<Integer,String> vv =
            new BasicVisualizationServer<Integer,String>(layout);
        vv.setPreferredSize(new Dimension(850,850)); 

        JFrame frame = new JFrame("Simple Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv); 
        frame.pack();
        frame.setVisible(true);       
    }
}

How do I add labels like "q0"?

Dilini
  • 777
  • 8
  • 22
  • 1
    Please cite the tutorial you are following. See also [*Initial Threads*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Feb 19 '12 at 16:41
  • 1
    Since you have defined the generics of `SparseMultigraph` as `SparseMultigraph` where the generic V for vertex as Integer and the generic E for edge as String, hence each vertex's label value is in Integer and each edge's label in String. So, if you want each vertex by names like q1, v2, etc., use String for generic V, so you can pass a vertex name like this `g.addVertex("q1");` – ecle Feb 19 '12 at 18:04
  • To change default vertex label transformer in a JUNG rendering context, search for the keyword `getRenderContext().setVertexLabelTransformer` – ecle Feb 19 '12 at 18:22
  • More in SO: http://stackoverflow.com/questions/3288886/vertex-label-in-jung-graph-visualization. Take note that it depends on your definition of generics for vertices and edges. If the sample doesn't work for you, check the generic types used in the sample and modify it according to yours. – ecle Feb 19 '12 at 18:26
  • @eee Using `String` for `V` in `SparseMultigraph` solved the problem. Thank You. – Dilini Feb 19 '12 at 19:01
  • See the answer to http://stackoverflow.com/questions/9352531/how-to-add-two-edges-having-the-same-label-but-different-endpoints-in-jung. That also applies to vertex labels. – Joshua O'Madadhain Feb 19 '12 at 23:49
  • @eee Could you please post your answer which is put above as a comment ,so that I can accept it? – Dilini Mar 04 '12 at 20:09
  • @Dilini, check this and please answer. https://stackoverflow.com/questions/70663744/ssl-error-when-connecting-to-mysql-database-in-my-siddhi-project – Onur Özkır Jan 17 '22 at 06:21

2 Answers2

5

If you have a custom class for nodes, I will give an example from my project. I have a class Node which is like :

public class Node 
{

public long tweetId = 0L;
public long nodeId = 0L;
public String screenName = "";
public Date reTweetDate = new Date();
public boolean isMainNode = false;
public int size = 0;

public Node()
{
}

}

// You just need to override transform like below:

vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller() {
                @Override
                public String transform(Object v) {

                    return ((Node)v).screenName;
                }});

// It will show the screenName property as a label for each node in the graph. // I hope this is what you are looking for.

mgokhanbakal
  • 1,679
  • 1
  • 20
  • 26
5

Since you have defined the generics of SparseMultigraph<V, E> as SparseMultigraph<Integer, String> where the generic V for vertex as Integer and the generic E for edge as String, hence each vertex's label value is in Integer and each edge's label in String. So, if you want each vertex by names like q1, v2, etc., use String for generic V, so you can pass a vertex name like this g.addVertex("q1");

ecle
  • 3,952
  • 1
  • 18
  • 22