Skip to content

Commit

Permalink
run gnuplot in interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wesley2012 committed Sep 7, 2017
1 parent 83bf9a1 commit 4a0854c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
21 changes: 21 additions & 0 deletions base/src/main/java/com/panayotis/gnuplot/GNUPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,27 @@ public void setPersist(boolean ispersist) {
exec.setPersist(ispersist);
}

/**
* Run gnuplot in interactive mode
*
* @author wesley
*
* @param interactive
*/
public void setInteractive(boolean interactive) {
exec.setInteractive(interactive);
}

/**
* Close an interactive plot.
* Currently this method can only end the gnuplot process, but is unable to close the graph window. Need your help!
*
* @author wesley
*/
public void close(){
exec.close();
}

/**
* Set gnuplot parameters to another set of parameters.
*
Expand Down
38 changes: 37 additions & 1 deletion base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ class GNUPlotExec {
private static final transient String DEFAULT_PATH = FileUtils.findPathExec();
private transient String gnuplotexec;
private boolean ispersist;
private boolean interactive;
private final static String[] persistcommand = {"path", "file", "-persist"};
private final static String[] persist_interactive = {"path", "file", "-", "-persist"};
private final static String[] nopersist = {"path", "file"};

private Process proc;
private String tmpfile;

/**
* Create a new GNUPlotExec object with defaultΩ gnuplot path. Under POSIX
* environment, it is able to automatically find gnuplot executable in the
Expand Down Expand Up @@ -125,7 +130,7 @@ void plot(GNUPlotParameters par, GNUPlotTerminal terminal) throws GNUPlotExcepti
*/
String[] command;
if (ispersist)
command = persistcommand;
command = interactive ? persist_interactive : persistcommand;
else
command = nopersist;
command[0] = getGNUPlotPath();
Expand Down Expand Up @@ -177,6 +182,12 @@ public void run() {
};
out_thread.start();

if (interactive){
this.proc = proc;
this.tmpfile = command[1];
return;
}

try {
proc.waitFor(); // wait for process to finish
out_thread.join(); // wait for output (terminal related) thread to finish
Expand Down Expand Up @@ -218,6 +229,31 @@ void setPersist(boolean persist) {
ispersist = persist;
}

public void setInteractive(boolean interactive) {
this.interactive = interactive;
}

public void close(){
if (proc == null){
return;
}
try {
proc.getOutputStream().close();
proc.getInputStream().close();
proc.getErrorStream().close();
} catch (IOException e) {
e.printStackTrace();
}
try {
proc.waitFor(); // wait for process to finish
} catch (InterruptedException ex) {
throw new GNUPlotException("Interrupted execution of gnuplot");
}
new File(tmpfile).delete();
proc.destroy();
proc = null;
}

private class Messages {

String output = "";
Expand Down
17 changes: 15 additions & 2 deletions base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public static void main(String[] args) {
// defaultTerminal(path);
// EPSTerminal(path);
// SVGTerminal(path);
JPlotTerminal(path);
//JPlotTerminal(path);
//serialization(defaultTerminal(path));
//file();

interactive();
}

/* This is a very simple plot to demonstrate JavaPlot graphs */
Expand Down Expand Up @@ -189,4 +189,17 @@ private static void file() {
ex.printStackTrace();
}
}

private static void interactive() {
JavaPlot p = new JavaPlot(true);
p.addPlot("sin(x)*y");
p.setInteractive(true);
p.plot();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
p.close();
}
}

1 comment on commit 4a0854c

@teras
Copy link
Owner

@teras teras commented on 4a0854c Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pull request. I will have a look at it. About a first impression: I don't like the @author tag in the sorce code. Better create an authors file instead.

Please sign in to comment.