aporto
aporto

Reputation: 31

How make output png file in JavaPlot

i using ">gnujavaplot and i not found a way to make outputs image files. I just plot in gnuplot window, i need the plot output in png file.

I need something like: $ a set terminal png $ set output "filename.png" in gnujavaplot.

THanks.

Upvotes: 3

Views: 3044

Answers (3)

Barbatos Rex
Barbatos Rex

Reputation: 11

I was looking for an answer for this problem as well, but I was unable to find one. However, after seeing an example on how to export to a ".eps" file, I adapted it to PNG. Below is an example of a simple graph of a parabola when x in [0,10]

//PNG export
import java.io.File;
import com.panayotis.gnuplot.terminal.FileTerminal;
import com.panayotis.gnuplot.terminal.GNUPlotTerminal;
//JavaPlot Styling
import com.panayotis.gnuplot.JavaPlot;
import com.panayotis.gnuplot.plot.AbstractPlot;
import com.panayotis.gnuplot.style.PlotStyle;
import com.panayotis.gnuplot.style.Style;



public class HOPE {

    public static void main(String[] args) {
        String className="parabola";
        String title="teste";
        //Generates a file in .png
        File file = new File("IMG/statistics_"+ title + "_" + className + ".png");
    //Creates a plot
        JavaPlot plot = new JavaPlot();
    //Creates a terminal class that interacts with Gnuplot without showing the graph
        GNUPlotTerminal terminal = new FileTerminal("png","IMG/statistics_" + title + "_" + className + ".png");
    plot.setTerminal(terminal);
        //Configurations of axis labels
    plot.set("xlabel", "\"LEI-ISEP\"");
    plot.set("ylabel", "\"" + title + "\"");
    plot.set("autoscale", "ymax");
        //Generate the parabola data
    int[][] data = new int[10][2];
    for (int i = 0; i < 10; i++) {
        data[i][0] = i;
        data[i][1] = i*i; //Could hava used Math.pow(i,2)
    }
        //Appends the parabola to the graphic
    plot.addPlot(data);
        //Defines the style of the graph
    PlotStyle stl = ((AbstractPlot) plot.getPlots().get(0)).getPlotStyle();
    stl.setStyle(Style.LINESPOINTS);
        //Hides the label on the line
        plot.setKey(JavaPlot.Key.OFF);
        //Executes the plot (without showing) and saves the image in IMG directory in your project directory
    plot.plot();
    }
}

Hope this works for you.

Upvotes: 1

WillBD
WillBD

Reputation: 1919

It's worth an important note, that in addition to the code presented by christo, when you switch the terminal of javaplot, it will block and crash the thread, since gnuplot is waiting for the user to close it, which since it's set to a png file object, there is no way to do. The way to fix this (at least in windows 7) is to set the persist attribute to false, like so:

p.setPersist(false);

That will ensure that everything goes nice and quickly and has no trouble.

Upvotes: 1

christo
christo

Reputation: 71

i just copy my actual code, withoout editing, so there is a few overhead, but i think you should get the thing. The interesting part starts at

ImageTerminal png = new ImageTerminal();

and dont miss the end of the code!

greetings

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.panayotis.gnuplot.JavaPlot;
import com.panayotis.gnuplot.plot.DataSetPlot;
import com.panayotis.gnuplot.style.NamedPlotColor;
import com.panayotis.gnuplot.style.PlotStyle;
import com.panayotis.gnuplot.style.Style;
import com.panayotis.gnuplot.terminal.ImageTerminal;

public class main {
public static void main(String[] args) {
    double[][] values = new double[3][2];
    values[0][0] = 0.1;
    values[0][1] = 0.3;
    values[1][0] = 0.4;
    values[1][1] = 0.3;
    values[2][0] = 0.5;
    values[2][1] = 0.5;

    double[][] values2 = new double[3][2];
    values2[0][0] = 0.2;
    values2[0][1] = 0.0;
    values2[1][0] = 0.7;
    values2[1][1] = 0.1;
    values2[2][0] = 0.6;
    values2[2][1] = 0.5;

    PlotStyle styleDeleted = new PlotStyle();
    styleDeleted.setStyle(Style.POINTS);
    styleDeleted.setLineType(NamedPlotColor.GRAY80);

    PlotStyle styleExist = new PlotStyle();
    styleExist.setStyle(Style.POINTS);
    styleExist.setLineType(NamedPlotColor.BLACK);

    DataSetPlot setDeleted = new DataSetPlot(values);
    setDeleted.setPlotStyle(styleDeleted);
    setDeleted.setTitle("deleted EMs");

    DataSetPlot setExist = new DataSetPlot(values2);
    setExist.setPlotStyle(styleExist);
    setExist.setTitle("remaining EMs");

    ImageTerminal png = new ImageTerminal();
    File file = new File("/home/testuser/plot.png");
    try {
        file.createNewFile();
        png.processOutput(new FileInputStream(file));
    } catch (FileNotFoundException ex) {
        System.err.print(ex);
    } catch (IOException ex) {
        System.err.print(ex);
    }

    JavaPlot p = new JavaPlot();
    p.setTerminal(png);

    p.getAxis("x").setLabel("yield");
    p.getAxis("y").setLabel("biomass");
    p.getAxis("x").setBoundaries(0.0, 1.0);
    p.getAxis("y").setBoundaries(0.0, 1.0);
    p.addPlot(setDeleted);
    p.addPlot(setExist);
    p.setTitle("remaining EMs");
    p.plot();

    try {
        ImageIO.write(png.getImage(), "png", file);
    } catch (IOException ex) {
        System.err.print(ex);
    }
}

}

Upvotes: 7

Related Questions