简体   繁体   中英

Labels for vertices using Jung library

I started to use the Jung library for my visualization of a graph. Main problem currently is that I don't know how to set labels/metadata for my vertices. Depending on a specific attribute of a vertex, I would like to color the vertices differently. The object of class Node contains an additional Integer-value, I would like to add as an additional attribute (by getGroup()) for the vertices. The following code only visualizes the getId()-String of each node.

Any recommendation?

This is my following code in the main class:

Graph<String,Double> g = new SparseGraph<String,Double>();
    List<Link> linkList = new ArrayList<Link>();
    List<Node> nodeList = new ArrayList<Node>();
    linkList = f.getLinks();
    nodeList = f.getNodes();
    for(Node nodeElement:nodeList){
        g.addVertex(nodeElement.getId());
    }
    for(Link linkElement:linkList){
        g.addEdge(linkElement.getValue(), linkElement.getSource(), linkElement.getTarget());
    }

    VisualizationImageServer vs =
              new VisualizationImageServer(
                new SpringLayout(g), new Dimension(500, 500));

    vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller() {
        public String transform(Object v) {
            return Integer.toString(((Node)v).getGroup());
        }
    });

    JFrame frame = new JFrame("");
    frame.getContentPane().add(vs);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

You didn't specify which version of JUNG you're using, but you've got the right general idea: call setVertexLabelTransformer() with a function that converts nodes to the string that you want to render.

That said, there are a few issues with your code as written and I'm not sure I understand how you're getting the reported behavior with the code as quoted above.

(0) I don't understand why you're adding the node ID as the vertex, rather than just the Node . Are the endpoints of your Link s node IDs or Node s? In any event, if you don't add the Node , then it's not clear from your code as written how you expect the vertex Group to be available.

(1) The vertices of the graph are node IDs, not Node s, but you're casting the vertex object passed to transform() to a Node . I would expect this to throw a ClassCastException .

(2) Even supposing that your vertices were actually Node objects, I'm surprised that you're reporting that the vertex ID (rather than the Group) is what's showing up on the label, because the only thing that you're asking for in the vertex label is the Group; I wouldn't expect the ID to be showing up at all.

That said, if the vertices are Node objects, and you want multiple elements to show up, it's pretty easy; just provide a function that does what you want on the Node object.
* If that function is toString() , then you can use ToStringLabeller (and that should actually be the default, so you shouldn't even need to specify it); more on that below. * If that function is (say) getLabel() , then this should work (in Java 8):

// either of these should work
setVertexLabelTransformer(Node::getLabel())
setVertexLabelTransformer(v -> v.getLabel())    

or you can do the anonymous inner class thing if you're not using Java 8.

If you don't want your vertices to be Node objects, then you'll need to provide a way of mapping vertex objects (node IDs) to nodes, such as a Map<String, Node> , and then supply that map to the function that converts vertices to labels:

setVertexLabelTransformer(v -> labelToNode.get(v).getLabel())

Note: ToStringLabeller() shouldn't generally have its transform() method overridden; it just specifies that you want to use the toString() of the object itself as the label.

// These two are equivalent
setVertexLabelTransformer(v -> v.toString())
setVertexLabelTransformer(new ToStringLabeller())

(Needless to say, ToStringLabeller() predates Java 8. :) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM