简体   繁体   中英

How to save svg or esp file in JavaPlot

I tried the code for saving png files found here and it works. But I want to save a vector image, so I changed the terminal to: PostscriptTerminal and SVGTerminal. For the SVGTerminal the output in empty and when I use the PostscriptTerminal the code hangs at 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);
    }

I managed to save an .eps file. I only needed to add a file path in constructor and it worked.

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM