From 4a0854c537b4eb068ffc85e38a4527ec533bd504 Mon Sep 17 00:00:00 2001 From: wesley2012 Date: Thu, 7 Sep 2017 09:11:12 +0800 Subject: [PATCH 1/4] run gnuplot in interactive mode --- .../java/com/panayotis/gnuplot/GNUPlot.java | 21 ++++++++++ .../com/panayotis/gnuplot/GNUPlotExec.java | 38 ++++++++++++++++++- .../com/panayotis/gnuplot/demo/BaseDemo.java | 17 ++++++++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java index 9c9d2e3..c5d6b55 100644 --- a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java +++ b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java @@ -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. * diff --git a/base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java b/base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java index f49a76b..a7ed143 100644 --- a/base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java +++ b/base/src/main/java/com/panayotis/gnuplot/GNUPlotExec.java @@ -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 @@ -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(); @@ -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 @@ -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 = ""; diff --git a/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java b/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java index fe5e1c3..3a276fc 100644 --- a/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java +++ b/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java @@ -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 */ @@ -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(); + } } From 9417f46edba5da8accdf8a7449ed24a9623245d2 Mon Sep 17 00:00:00 2001 From: wesley2012 Date: Thu, 7 Sep 2017 15:45:38 +0800 Subject: [PATCH 2/4] support "rgb variable" --- .../java/com/panayotis/gnuplot/style/NamedPlotColor.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/base/src/main/java/com/panayotis/gnuplot/style/NamedPlotColor.java b/base/src/main/java/com/panayotis/gnuplot/style/NamedPlotColor.java index 290aea1..a5e5dce 100644 --- a/base/src/main/java/com/panayotis/gnuplot/style/NamedPlotColor.java +++ b/base/src/main/java/com/panayotis/gnuplot/style/NamedPlotColor.java @@ -106,7 +106,8 @@ public enum NamedPlotColor implements PlotColor { VIOLET, DARK_VIOLET, PLUM, - PURPLE; + PURPLE, + VARIABLE; /** * Get the representation of this color @@ -115,6 +116,9 @@ public enum NamedPlotColor implements PlotColor { */ @Override public String getColor() { + if (this.equals(VARIABLE)){ + return "rgb variable"; + } return "rgb '" + name().toLowerCase().replace('_', '-') + "'"; } } From cc063b6370044bd50842326c3ab9fbbcc538ee37 Mon Sep 17 00:00:00 2001 From: wesley2012 Date: Thu, 7 Sep 2017 23:13:13 +0800 Subject: [PATCH 3/4] support label offset and point type --- .../panayotis/gnuplot/style/PlotStyle.java | 36 ++++++++++++++- .../com/panayotis/gnuplot/demo/BaseDemo.java | 45 ++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java b/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java index 07751a6..85dc1e9 100644 --- a/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java +++ b/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java @@ -29,6 +29,9 @@ public class PlotStyle extends PropertiesHolder { private Style type; private FillStyle fill; + private int labelOffsetX; + private int labelOffsetY; + private int labelPointType = -1; /** * Creates a new instance of PlotStyle with default parameters @@ -68,8 +71,15 @@ public void setStyle(Style style) { public void appendProperties(StringBuilder buf) { if (type != null) { buf.append(" with ").append(type.name().toLowerCase()); + if (type.equals(Style.LABELS)){ + if (!(labelOffsetX == 0 && labelOffsetY == 0)) { + buf.append(" offset ").append(labelOffsetX).append(',').append(labelOffsetY); + } + if (labelPointType != -1) { + buf.append(" point pt ").append(labelPointType); + } + } super.appendProperties(buf); - if (fill != null && type.filled) fill.appendProperties(buf); } @@ -151,4 +161,28 @@ public void setPointType(int type) { public void setFill(FillStyle fillstyle) { this.fill = fillstyle; } + + /** + * Set the offset of label relative to the point. Used when style is LABELS. + * + * @author wesley + * + * @param x + * @param y + */ + public void setLabelOffset(int x, int y){ + this.labelOffsetX = x; + this.labelOffsetY = y; + } + + /** + * Set the point type of label. Used instead of setPointType() when style is LABELS. + * + * @author wesley + * + * @param pointType + */ + public void setLabelPointType(int pointType){ + this.labelPointType = pointType; + } } diff --git a/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java b/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java index 3a276fc..ba0e7bc 100644 --- a/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java +++ b/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java @@ -18,6 +18,8 @@ import com.panayotis.gnuplot.GNUPlotParameters; import com.panayotis.gnuplot.JavaPlot; import com.panayotis.gnuplot.dataset.FileDataSet; +import com.panayotis.gnuplot.dataset.GenericDataSet; +import com.panayotis.gnuplot.dataset.parser.DataParser; import com.panayotis.gnuplot.layout.StripeLayout; import com.panayotis.gnuplot.plot.AbstractPlot; import com.panayotis.gnuplot.plot.DataSetPlot; @@ -33,6 +35,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.List; import javax.swing.JFrame; /** @@ -59,7 +63,8 @@ public static void main(String[] args) { //JPlotTerminal(path); //serialization(defaultTerminal(path)); //file(); - interactive(); + //interactive(); + labels(); } /* This is a very simple plot to demonstrate JavaPlot graphs */ @@ -202,4 +207,42 @@ private static void interactive() { } p.close(); } + + /* This is a simple plot to demonstrate plotting labels beside points with different colors */ + private static void labels(){ + GenericDataSet dataSet = new GenericDataSet(new DataParser() { + public boolean isValid(String s, int i) { + return true; + } + }); + + //fill dataset + for (int i = 0; i < 100; i++){ + double x = Math.random() * 1000; + double y = Math.random() * 1000; + double z = Math.random() * 1000; + String label = Character.toString((char)(int)(Math.random() * 26 + 65)); + int rgbColor = (int)(Math.random() * 0x1000000); + + List items = new ArrayList(5); + items.add(String.valueOf(x)); + items.add(String.valueOf(y)); + items.add(String.valueOf(z)); + items.add(label); + items.add(String.format("0x%x", rgbColor)); + dataSet.add(items); + } + + JavaPlot p = new JavaPlot(true); + p.addPlot(dataSet); + + AbstractPlot plot = (AbstractPlot)p.getPlots().get(0); + PlotStyle style = plot.getPlotStyle(); + style.setStyle(Style.LABELS); + style.setLabelPointType(7); + style.setLabelOffset(0, 1); + style.setLineType(NamedPlotColor.VARIABLE); + + p.plot(); + } } From 7e620ff5acb8f935bf22cf650024a834271d1412 Mon Sep 17 00:00:00 2001 From: wesley2012 Date: Tue, 12 Sep 2017 21:38:27 +0800 Subject: [PATCH 4/4] remove @author --- base/src/main/java/com/panayotis/gnuplot/GNUPlot.java | 4 ---- base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java | 4 ---- 2 files changed, 8 deletions(-) diff --git a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java index c5d6b55..efbea83 100644 --- a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java +++ b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java @@ -357,8 +357,6 @@ public void setPersist(boolean ispersist) { /** * Run gnuplot in interactive mode * - * @author wesley - * * @param interactive */ public void setInteractive(boolean interactive) { @@ -369,8 +367,6 @@ public void setInteractive(boolean 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(); } diff --git a/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java b/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java index 85dc1e9..2fb020f 100644 --- a/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java +++ b/base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java @@ -165,8 +165,6 @@ public void setFill(FillStyle fillstyle) { /** * Set the offset of label relative to the point. Used when style is LABELS. * - * @author wesley - * * @param x * @param y */ @@ -178,8 +176,6 @@ public void setLabelOffset(int x, int y){ /** * Set the point type of label. Used instead of setPointType() when style is LABELS. * - * @author wesley - * * @param pointType */ public void setLabelPointType(int pointType){