Java 如何使用 Graphviz 从点文件格式生成 PNG 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23301276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:45:26  来源:igfitidea点击:

How to generate a PNG image from a dot file format with Graphviz

javaqueuegraphviz

提问by

I have a java class that implements a priority queue. Then I have a class test that generates a graph like this:

我有一个实现优先级队列的 java 类。然后我有一个类测试,它生成一个这样的图表:

digraph G {
Milan  (0.0)       ->     Turin  (1.2)       
Milan  (0.0)       ->     Montreal  (7.0)       
Turin  (1.2)       ->     Paris  (5.8)       
Turin  (1.2)       ->     Tokyo  (2.2)       
}

This graph is saved in a file called "queue".

该图保存在名为“队列”的文件中。

Now I wish that this graph was displayed in a PNG image using Graphviz. So the last call of my test files (after you have created and filled the queue with priority) is:

现在我希望使用 Graphviz 将此图形显示在 PNG 图像中。所以我的测试文件的最后一次调用(在你创建并优先填充队列之后)是:

queue.toString("queue");

All right. The toString method is the following:

好的。toString 方法如下:

public void toString(String fileDot){
    try {
        FileOutputStream file = new FileOutputStream(fileDot); 
        PrintStream Output = new PrintStream(file); 
        Output.print(this.printQueue()); 
        Output.close(); 
        File f = new File(fileDot); 
        String arg1 = f.getAbsolutePath(); 
        String arg2 = arg1 + ".png"; 
        String[] c = {"dot", "-Tpng", arg1, "-o", arg2};
        Process p = Runtime.getRuntime().exec(c); 
        int err = p.waitFor(); 
    }
    catch(IOException e1) {
        System.out.println(e1);
    }
    catch(InterruptedException e2) {
        System.out.println(e2);
    }
}

private String printQueue() throws IOException {
    String g = new String("");
    char c = '"';
    g = g.concat("digraph G {\n");
    if(isEmpty()) 
        g = g.concat("    " + "Empty priority queue.");
    else {
        for(int i = 0; i < lastIndex; i++) {
            if(heap[2 * i] != null) { 
                g = g.concat("" + heap[i].elem + "  (" + heap[i].prior + ")   " + "   " + " -> " + "    " + "" + heap[i * 2].elem + "  (" + heap[i * 2].prior + ")       \n" );
                if(heap[2 * i + 1] != null) 
                    g = g.concat("" + heap[i].elem + "  (" + heap[i].prior + ")   " + "   " + " -> " + "    " + "" + heap[i * 2 + 1].elem + "  (" + heap[i * 2 + 1].prior + ")       \n" ); 
            } 
        } //end for
    } //end else  
    g = g.concat("}");
    return g;   
}

Why is not generated image .png? Where am I wrong? Of course I installed Graphviz. Thanks

为什么不是生成的图像 .png?我哪里错了?当然我安装了Graphviz。谢谢

采纳答案by Simon

When I ran the .dotfile above through dotat the command line, I got:

当我在命令行运行.dot上面的文件dot时,我得到:

$ dot -Tpng queue.dot -oqueue.png
Warning: queue.dot:2: syntax error in line 2 near '('

Thus, the parenthesised numbers in the node names are not valid in dotsyntax. If you remove them, I expect the .pngfile would be created successfully. If you need the parenthesised numbers in your output, I suggest looking up node labels in the GraphViz documentation.

因此,节点名称中带括号的数字在dot语法上无效。如果您删除它们,我希望该.png文件会成功创建。如果您需要输出中带括号的数字,我建议您在 GraphViz 文档中查找节点标签。

I'd also note that toString()does not seem like a particularly clear name for a function that creates a .pngfile so changing the name of the function might be advisable.

我还注意到,toString()对于创建.png文件的函数来说,这似乎不是一个特别清晰的名称,因此建议更改函数的名称。

回答by SSteve

Try using dot's -Ooption instead of -o. According to dot -?here's what it does:

尝试使用dot's-O选项而不是-o. 根据dot -?这里是它的作用:

  • Automatically generate an output filename based on the input filename with a .'format' appended. (Causes all -ofile options to be ignored.)
  • 根据输入文件名自动生成输出文件名并附加 .'format'。(导致所有 -ofile 选项被忽略。)

So you could change

所以你可以改变

String[] c = {"dot", "-Tpng", arg1, "-o", arg2};

to

String[] c = {"dot", "-Tpng", arg1, "-O"};