java - 如何使用 JUNG 显示树布局,但具有更整齐的间距和结构

标签 java graph graph-visualization graphml jung2

我正在尝试使用 JUNG 在树布局中布置一些节点,但希望将它们以整齐的间距布置,类似于 -

Tidy tree layout example

我已经从https://github.com/jrtom/jung下载了JUNG项目并找到了 edu.uci.ics.jung.samples.TreeLayoutDemo 和 edu.uci.ics.jung.samples.GraphFromGraphMLDemo 的示例,但仍然难以实现我所追求的目标。

GraphML 文件内容如下所示 -

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns  http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
   <graph id="G" edgedefault="undirected">
      <node id="n0" />
      <node id="n1" />
      <node id="n2" />
      <node id="n3" />
      <node id="n4" />
      <node id="n5" />
      <node id="n6" />
      <node id="n7" />
      <node id="n8" />
      <node id="n9" />
      <node id="n10" />
      <edge source="n0" target="n2" />
      <edge source="n1" target="n2" />
      <edge source="n2" target="n3" />
      <edge source="n3" target="n5" />
      <edge source="n3" target="n4" />
      <edge source="n4" target="n6" />
      <edge source="n6" target="n5" />
      <edge source="n5" target="n7" />
      <edge source="n6" target="n8" />
      <edge source="n8" target="n7" />
      <edge source="n8" target="n9" />
      <edge source="n8" target="n10" />
   </graph>
</graphml>

我已经能够生成树布局。

private Supplier<MutableNetwork<Number, Number>> graphFactory;
private Supplier<Number> nodeFactory;
private Supplier<Number> edgeFactory;
private GraphMLReader<MutableNetwork<Number, Number>, Number, Number> gmlreader;

private void initializeTools() throws ParserConfigurationException, SAXException {
    graphFactory =
            () -> NetworkBuilder.directed()
                    .allowsSelfLoops(true)
                    .allowsParallelEdges(true)
                    .build();
    nodeFactory =
            new Supplier<Number>() {
                int n = 0;

                public Number get() {
                    return n++;
                }
            };
    edgeFactory =
            new Supplier<Number>() {
                int n = 0;

                public Number get() {
                    return n++;
                }
            };
    gmlreader =
            new GraphMLReader<>(nodeFactory, edgeFactory);
}

private MutableNetwork<Number, Number> getGraph() {
    MutableNetwork<Number, Number> graph = null;

    String xml =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                    + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                    + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">"
                    + "   <graph id=\"G\" edgedefault=\"undirected\">\n" +
                    "      <node id=\"n0\" />\n" +
                    "      <node id=\"n1\" />\n" +
                    "      <node id=\"n2\" />\n" +
                    "      <node id=\"n3\" />\n" +
                    "      <node id=\"n4\" />\n" +
                    "      <node id=\"n5\" />\n" +
                    "      <node id=\"n6\" />\n" +
                    "      <node id=\"n7\" />\n" +
                    "      <node id=\"n8\" />\n" +
                    "      <node id=\"n9\" />\n" +
                    "      <node id=\"n10\" />\n" +
                    "      <edge source=\"n0\" target=\"n2\" />\n" +
                    "      <edge source=\"n1\" target=\"n2\" />\n" +
                    "      <edge source=\"n2\" target=\"n3\" />\n" +
                    "      <edge source=\"n3\" target=\"n5\" />\n" +
                    "      <edge source=\"n3\" target=\"n4\" />\n" +
                    "      <edge source=\"n4\" target=\"n6\" />\n" +
                    "      <edge source=\"n6\" target=\"n5\" />\n" +
                    "      <edge source=\"n5\" target=\"n7\" />\n" +
                    "      <edge source=\"n6\" target=\"n8\" />\n" +
                    "      <edge source=\"n8\" target=\"n7\" />\n" +
                    "      <edge source=\"n8\" target=\"n9\" />\n" +
                    "      <edge source=\"n8\" target=\"n10\" />\n" +
                    "   </graph>" +
                    "</graphml>";

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter("temp.graphml"));
        writer.write(xml);
        writer.close();
        graph = graphFactory.get();
        gmlreader.load("temp.graphml", graph);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return graph;
}

照这样使用 -

    initializeTools();

    final MutableNetwork<Number, Number> graph = getGraph();

    // create a simple graph for the demo
    TreeLayoutAlgorithm<Number> layoutAlgorithm = new TreeLayoutAlgorithm<>();
    vv = new VisualizationViewer<>(graph, layoutAlgorithm, new Dimension(800, 800));

我无法让它完全按照我的预期或正在尝试的方式进行布局。这就是它的样子 - default tree layout

最佳答案

这是该特定“更整洁”树布局的实现: tidier tree

您可能可以将其改进为您正在使用的 jung 版本。

关于java - 如何使用 JUNG 显示树布局,但具有更整齐的间距和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58504070/

相关文章:

java - JDBC 更新未显示错误但不起作用?

java - 忽略 JAX-WS 客户端的 XML 验证

java - 为什么这个 java 字符串例程不打印答案?

java - 如何将字段索引为建议类型?

graph - Gremlin 关系建议

javascript - c3 chart.load 多个图表

javascript - 在图片上画线 - Javascript?

matlab - 如何在Matlab中绘制网络?

graphviz - 如何在graphviz中隐藏节点边界?

javascript - JSON 对象的可视化(关联数组)