Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

java - Vertex label in JUNG graph visualization

I wrote a little graph visualizer class:

    public void simpleGraph(SparseMultigraph<Vertex,SEdge> graph, String name) {

    Layout<Vertex, SEdge> layout = new ISOMLayout(graph);
    layout.setSize(new Dimension(800,800));
    BasicVisualizationServer<Vertex, SEdge> vv = new BasicVisualizationServer<Vertex, SEdge>(layout);
    vv.setPreferredSize(new Dimension(850,850)); //Sets the viewing area size

    JFrame frame = new JFrame(name);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv); 
    frame.pack();
    frame.setVisible(true);                 
}

How can I add labels for vertices and edges? The Values are stored in my custom vertex class. Can I iterate over all vertices in the Layout or BasicVisualizationServer and add labels?

Thanks for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to call a label transformer for your vertex/edge:

        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());

This is something you'd find pretty often in the samples. It uses the toString() method of your vertex class to specify the label.

A slightly more involved example:

        vv.getRenderContext().setEdgeLabelTransformer(new Transformer<MyEdge, String>() {
            public String transform(MyEdge e) {
                return (e.toString() + " " + e.getWeight() + "/" + e.getCapacity());
            }
        });

You don't need to iterate over the edges; the EdgeLabelTransformer or VertexLabelTransformer will label your edges as and when their properties are updated, and the VisualizationViewer will update them on the fly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...