[英]How to add custom vertex labels in JUNG graph visualization?
How to use custom vertex labels in JUNG graph visualization? 如何在JUNG图形可视化中使用自定义顶点标签?
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. 我遵循Jung 2.0 Tutorial ,我发现setVertexLabelTransformer()
可用于标记顶点,但据我所知,这些标签无法自定义。
For example, the below code produces three vertices, having vertex-labels 1,2,4: 例如,下面的代码生成三个顶点,顶点标签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"? 如何添加“q0”等标签?
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
. 由于您已将SparseMultigraph<V, E>
的泛型定义为SparseMultigraph<Integer, String>
,其中顶点的泛型V
为整数,边的泛型E为String
,因此每个顶点的标签值都在Integer
,每个边的标签在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");
因此,如果您希望每个顶点都有q1,v2等名称,请使用String
作为泛型V
,这样就可以传递一个顶点名称,如g.addVertex("q1");
If you have a custom class for nodes, I will give an example from my project. 如果您有节点的自定义类,我将从我的项目中给出一个示例。 I have a class Node which is like : 我有一个类Node,如:
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. //它将screenName属性显示为图表中每个节点的标签。 // I hope this is what you are looking for. //我希望这就是你要找的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.