Skip to content

Commit 4a0854c

Browse files
author
wesley2012
committed
run gnuplot in interactive mode
1 parent 83bf9a1 commit 4a0854c

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

base/src/main/java/com/panayotis/gnuplot/GNUPlot.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ public void setPersist(boolean ispersist) {
354354
exec.setPersist(ispersist);
355355
}
356356

357+
/**
358+
* Run gnuplot in interactive mode
359+
*
360+
* @author wesley
361+
*
362+
* @param interactive
363+
*/
364+
public void setInteractive(boolean interactive) {
365+
exec.setInteractive(interactive);
366+
}
367+
368+
/**
369+
* Close an interactive plot.
370+
* Currently this method can only end the gnuplot process, but is unable to close the graph window. Need your help!
371+
*
372+
* @author wesley
373+
*/
374+
public void close(){
375+
exec.close();
376+
}
377+
357378
/**
358379
* Set gnuplot parameters to another set of parameters.
359380
*

base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ class GNUPlotExec {
3737
private static final transient String DEFAULT_PATH = FileUtils.findPathExec();
3838
private transient String gnuplotexec;
3939
private boolean ispersist;
40+
private boolean interactive;
4041
private final static String[] persistcommand = {"path", "file", "-persist"};
42+
private final static String[] persist_interactive = {"path", "file", "-", "-persist"};
4143
private final static String[] nopersist = {"path", "file"};
4244

45+
private Process proc;
46+
private String tmpfile;
47+
4348
/**
4449
* Create a new GNUPlotExec object with defaultΩ gnuplot path. Under POSIX
4550
* environment, it is able to automatically find gnuplot executable in the
@@ -125,7 +130,7 @@ void plot(GNUPlotParameters par, GNUPlotTerminal terminal) throws GNUPlotExcepti
125130
*/
126131
String[] command;
127132
if (ispersist)
128-
command = persistcommand;
133+
command = interactive ? persist_interactive : persistcommand;
129134
else
130135
command = nopersist;
131136
command[0] = getGNUPlotPath();
@@ -177,6 +182,12 @@ public void run() {
177182
};
178183
out_thread.start();
179184

185+
if (interactive){
186+
this.proc = proc;
187+
this.tmpfile = command[1];
188+
return;
189+
}
190+
180191
try {
181192
proc.waitFor(); // wait for process to finish
182193
out_thread.join(); // wait for output (terminal related) thread to finish
@@ -218,6 +229,31 @@ void setPersist(boolean persist) {
218229
ispersist = persist;
219230
}
220231

232+
public void setInteractive(boolean interactive) {
233+
this.interactive = interactive;
234+
}
235+
236+
public void close(){
237+
if (proc == null){
238+
return;
239+
}
240+
try {
241+
proc.getOutputStream().close();
242+
proc.getInputStream().close();
243+
proc.getErrorStream().close();
244+
} catch (IOException e) {
245+
e.printStackTrace();
246+
}
247+
try {
248+
proc.waitFor(); // wait for process to finish
249+
} catch (InterruptedException ex) {
250+
throw new GNUPlotException("Interrupted execution of gnuplot");
251+
}
252+
new File(tmpfile).delete();
253+
proc.destroy();
254+
proc = null;
255+
}
256+
221257
private class Messages {
222258

223259
String output = "";

base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public static void main(String[] args) {
5656
// defaultTerminal(path);
5757
// EPSTerminal(path);
5858
// SVGTerminal(path);
59-
JPlotTerminal(path);
59+
//JPlotTerminal(path);
6060
//serialization(defaultTerminal(path));
6161
//file();
62-
62+
interactive();
6363
}
6464

6565
/* This is a very simple plot to demonstrate JavaPlot graphs */
@@ -189,4 +189,17 @@ private static void file() {
189189
ex.printStackTrace();
190190
}
191191
}
192+
193+
private static void interactive() {
194+
JavaPlot p = new JavaPlot(true);
195+
p.addPlot("sin(x)*y");
196+
p.setInteractive(true);
197+
p.plot();
198+
try {
199+
Thread.sleep(10000);
200+
} catch (InterruptedException e) {
201+
e.printStackTrace();
202+
}
203+
p.close();
204+
}
192205
}

0 commit comments

Comments
 (0)