如何在JavaPlot中保存svg或esp文件

时间:2014-09-11 08:32:23

标签: java svg plot gnuplot javaplot

我尝试了用于保存找到here的png文件的代码,但它确实有效。 但我想保存矢量图像,所以我将终端更改为:PostscriptTerminal和SVGTerminal。对于SVGTerminal,输出为空,当我使用PostscriptTerminal时代码挂起p.plot();

ImageTerminal png = new ImageTerminal();
    PostscriptTerminal eps = new PostscriptTerminal();
    SVGTerminal svg = new SVGTerminal();

    File file = new File("D:/plot.eps");
    try {
        file.createNewFile();
        eps.processOutput(new FileInputStream(file));
    } catch (FileNotFoundException ex) {
        System.err.print(ex);
    } catch (IOException ex) {
        System.err.print(ex);
    }

    PlotStyle myPlotStyle = new PlotStyle();
    myPlotStyle.setStyle(Style.POINTS);
    myPlotStyle.setPointType(7); // 7 - circle
    myPlotStyle.setLineType(3); // blue color

    JavaPlot p = new JavaPlot();
    p.setPersist(false);
    p.setTerminal(eps);
    //p.setTitle(algorithm_name+" - "+problem_name, "Arial", 20);
    p.getAxis("x").setLabel("X axis", "Arial", 15);
    p.getAxis("y").setLabel("Y axis","Arial", 15);
    p.setKey(JavaPlot.Key.TOP_RIGHT);

    double[][] front = this.writeObjectivesToMatrix();
    DataSetPlot s = new DataSetPlot(front);
    s.setPlotStyle(myPlotStyle);
    s.setTitle("");
    p.addPlot(s);
    p.plot(); //code hangs here if I use PostscriptTerminal 

    try {
        //ImageIO.write(png.getImage(), "png", file); //works
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write (eps.getTextOutput());
        //Close writer
        writer.close();
    } catch (IOException ex) {
        System.err.print(ex);
    }

1 个答案:

答案 0 :(得分:0)

我设法保存了一个.eps文件。我只需要在构造函数中添加一个文件路径,它就可以了。

PostscriptTerminal eps = new PostscriptTerminal("output.eps");

    PlotStyle myPlotStyle = new PlotStyle();
    myPlotStyle.setStyle(Style.POINTS);
    myPlotStyle.setPointType(7); // 7 - circle
    myPlotStyle.setLineType(3); // blue color

    JavaPlot p = new JavaPlot();
    p.setPersist(false);
    p.setTerminal(eps);
    p.getAxis("x").setLabel("X axis", "Arial", 15);
    p.getAxis("y").setLabel("Y axis","Arial", 15);
    p.setKey(JavaPlot.Key.TOP_RIGHT);

    double[][] front = this.writeObjectivesToMatrix();
    DataSetPlot s = new DataSetPlot(front);
    s.setPlotStyle(myPlotStyle);
    s.setTitle("");
    p.addPlot(s);
    p.plot();