diff --git a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java index 9c9d2e3..efbea83 100644 --- a/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java +++ b/base/src/main/java/com/panayotis/gnuplot/GNUPlot.java @@ -354,6 +354,23 @@ public void setPersist(boolean ispersist) { exec.setPersist(ispersist); } + /** + * Run gnuplot in interactive mode + * + * @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! + * + 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/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('_', '-') + "'"; } } 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..2fb020f 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,24 @@ 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. + * + * @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. + * + * @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 fe5e1c3..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; /** @@ -56,10 +60,11 @@ public static void main(String[] args) { // defaultTerminal(path); // EPSTerminal(path); // SVGTerminal(path); - JPlotTerminal(path); + //JPlotTerminal(path); //serialization(defaultTerminal(path)); //file(); - + //interactive(); + labels(); } /* This is a very simple plot to demonstrate JavaPlot graphs */ @@ -189,4 +194,55 @@ 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(); + } + + /* 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(); + } }