我正在构建一个关于图论算法的项目,为此我使用了JGraphT。我完全构建了我的图表,并在过去的几个月中完成了它的工作。现在我想导出它,以便我可以在Gephi中将其可视化。 我不想使用JGraph和Java可视化,因为我已经有足够的代码,我想保持简单。我想使用DOTExporter class from JgraphT。我已达到一个点,我导出精细的顶点和边,但没有边缘权重。
所以这是我的导出功能。我不知道如何实现ComponentAttributeProvider
界面,无法找到解决这个问题的方法。
我应该把什么想法代替null,null?
static public void exportGraph(){
StringNameProvider<CustomVertex> p1=new StringNameProvider<CustomVertex>();
IntegerNameProvider<CustomVertex> p2=new IntegerNameProvider<CustomVertex>();
StringEdgeNameProvider<CustomWeightedEdge> p3 = new StringEdgeNameProvider<CustomWeightedEdge>();
DOTExporter export=new DOTExporter(p2, p1, p3, null, null);
try {
export.export(new FileWriter("graph.dot"), g);
}catch (IOException e){}
}
我做过类似的事情
ComponentAttributeProvider<CustomWeightedEdge> edgeAttributeProvider =
new ComponentAttributeProvider<CustomWeightedEdge>() {
public Map<String, String> getComponentAttributes(CustomWeightedEdge e) {
Map<String, String> map =new LinkedHashMap<String, String>();
map.put("weight", Double.toString(g.getEdgeWeight(e)));
return map;
}
};
答案 0 :(得分:5)
static public void exportGraph(){
IntegerNameProvider<CustomVertex> p1=new IntegerNameProvider<CustomVertex>();
StringNameProvider<CustomVertex> p2=new StringNameProvider<CustomVertex>();
ComponentAttributeProvider<DefaultWeightedEdge> p4 =
new ComponentAttributeProvider<DefaultWeightedEdge>() {
public Map<String, String> getComponentAttributes(DefaultWeightedEdge e) {
Map<String, String> map =new LinkedHashMap<String, String>();
map.put("weight", Double.toString(g.getEdgeWeight(e)));
return map;
}
};
DOTExporter export=new DOTExporter(p1, p2, null, null, p4);
try {
export.export(new FileWriter("graph.dot"), g);
}catch (IOException e){}
}
答案 1 :(得分:1)
供将来参考,因为问题有点老,根据 Gewure 的解决方案不再有效。
如果您不限制自己使用 DOT 格式,则可以使用 graphML 格式和 API org.jgrapht.nio.graphml.GraphMLExporter<V,E>.setExportEdgeWeights(true)
进行管理。适用于 jgrapht 1.5.1。