问题描述
我写了一个小图形可视化类:
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);
}
如何为顶点和边添加标签?值存储在我的自定义顶点类中.我可以遍历 Layout 或 BasicVisualizationServer 中的所有顶点并添加标签吗?
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?
感谢您的帮助!
推荐答案
你需要为你的顶点/边调用一个标签转换器:
You need to call a label transformer for your vertex/edge:
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
这是您经常在示例中发现的内容.它使用顶点类的 toString() 方法来指定标签.
This is something you'd find pretty often in the samples. It uses the toString() method of your vertex class to specify the label.
一个稍微复杂的例子:
vv.getRenderContext().setEdgeLabelTransformer(new Transformer<MyEdge, String>() {
public String transform(MyEdge e) {
return (e.toString() + " " + e.getWeight() + "/" + e.getCapacity());
}
});
你不需要遍历边缘;EdgeLabelTransformer 或 VertexLabelTransformer 将在它们的属性更新时标记您的边缘,而 VisualizationViewer 将即时更新它们.
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.
这篇关于JUNG 图形可视化中的顶点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!