Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interactive mode and rgb variable #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 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,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.
*
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public enum NamedPlotColor implements PlotColor {
VIOLET,
DARK_VIOLET,
PLUM,
PURPLE;
PURPLE,
VARIABLE;

/**
* Get the representation of this color
Expand All @@ -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('_', '-') + "'";
}
}
32 changes: 31 additions & 1 deletion base/src/main/java/com/panayotis/gnuplot/style/PlotStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
60 changes: 58 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 @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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 */
Expand Down Expand Up @@ -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<String> items = new ArrayList<String>(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();
}
}