public final void plot() throws IOException, InterruptedException {
if (!itsPlotable) {
throw new IllegalArgumentException(
"Set data before calling plot method");
}
String aFileName = itsPlotDirectory + File.separator
+ String.valueOf(System.currentTimeMillis()) + "-plt.txt";
/*
* BufferedWriter aOutputDataFile = new BufferedWriter(new
* FileWriter(aFileBaseName + ".dat")); Enumeration aDataEnum =
* itsData.elements(); while (aDataEnum.hasMoreElements()) {
* aOutputDataFile.write((String)aDataEnum.nextElement() + "\n"); }
* aOutputDataFile.close();
*/
itsOutputPlotFile = new BufferedWriter(new FileWriter(aFileName));
if (itsTitle != null) {
itsOutputPlotFile.write("set title \"" + itsTitle + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsDataFileSeparator != null) {
itsOutputPlotFile.write("set datafile separator \""
+ itsDataFileSeparator + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsGrid == null) {
itsOutputPlotFile.write("unset grid");
}
else {
itsOutputPlotFile.write("set grid");
if (!itsGrid.equals("")) {
itsOutputPlotFile.write(" " + itsGrid);
}
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
if (itsKey == null) {
itsOutputPlotFile.write("unset key");
}
else {
itsOutputPlotFile.write("set key " + itsKey);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
switch (itsParametric) {
case State.ON:
itsOutputPlotFile.write("set parametric");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
break;
case State.OFF:
itsOutputPlotFile.write("unset parametric");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
break;
default:
throw new IllegalArgumentException("Invalid parametric state");
}
switch (itsPolar) {
case State.ON:
itsOutputPlotFile.write("set polar");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
break;
case State.OFF:
itsOutputPlotFile.write("unset polar");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
break;
default:
throw new IllegalArgumentException("Invalid polar state");
}
if (itsSamples != null) {
itsOutputPlotFile.write("set samples " + itsSamples);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsDummy != null) {
itsOutputPlotFile.write("set dummy " + itsDummy);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsClip != null) {
itsOutputPlotFile.write("set clip");
if (!itsClip.equals("")) {
itsOutputPlotFile.write(" " + itsClip);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsXLabel != null) {
itsOutputPlotFile.write("set xlabel \"" + itsXLabel + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsYLabel != null) {
itsOutputPlotFile.write("set ylabel \"" + itsYLabel + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsX2Label != null) {
itsOutputPlotFile.write("set x2label \"" + itsX2Label + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsY2Label != null) {
itsOutputPlotFile.write("set y2label \"" + itsY2Label + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsXTics != null) {
itsOutputPlotFile.write("set xtics");
if (!itsXTics.equals("")) {
itsOutputPlotFile.write(" " + itsXTics);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsYTics != null) {
itsOutputPlotFile.write("set ytics");
if (!itsYTics.equals("")) {
itsOutputPlotFile.write(" " + itsYTics);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsX2Tics != null) {
itsOutputPlotFile.write("set x2tics");
if (!itsX2Tics.equals("")) {
itsOutputPlotFile.write(" " + itsX2Tics);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsY2Tics != null) {
itsOutputPlotFile.write("set y2tics");
if (!itsY2Tics.equals("")) {
itsOutputPlotFile.write(" " + itsY2Tics);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
else {
if (itsYTics == null && itsY2Label != null) {
// TODO should be according to all used axes
itsOutputPlotFile.write("set ytics nomirror");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
itsOutputPlotFile.write("set y2tics nomirror");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
}
if (itsMXTics != null) {
itsOutputPlotFile.write("set mxtics " + itsMXTics);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsMYTics != null) {
itsOutputPlotFile.write("set mytics " + itsMYTics);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsTimeFormat != null) {
itsOutputPlotFile.write("set timefmt '" + itsTimeFormat + "'");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsXData != null) {
itsOutputPlotFile.write("set xdata " + itsXData);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsYData != null) {
itsOutputPlotFile.write("set ydata " + itsYData);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsTerminal != Terminal.NOT_SPECIFIED && itsOutputFileName != null) {
itsOutputPlotFile.write("set terminal " + itsTerminal);
if (itsSize != null) {
itsOutputPlotFile.write(" size " + itsSize);
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
itsOutputPlotFile.write("set output \"" + itsOutputFileName + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
Enumeration<String> aEnum = ((Vector) itsExtra).elements();
while (aEnum.hasMoreElements()) {
itsOutputPlotFile.write((String) aEnum.nextElement());
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsLogscale == null) {
itsOutputPlotFile.write("unset logscale");
}
else {
itsOutputPlotFile.write("set logscale");
if (!itsLogscale.equals("")) {
itsOutputPlotFile.write(" " + itsLogscale);
}
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
if (!itsAutoscaleAfterRanges) {
if (itsAutoscale != null) {
itsOutputPlotFile.write("set autoscale");
if (!itsAutoscale.equals("")) {
StringTokenizer aST = new StringTokenizer(itsAutoscale, ",");
boolean aFirst = true;
while (aST.hasMoreElements()) {
if (aFirst) {
aFirst = false;
}
else {
itsOutputPlotFile.write(";set autoscale");
}
itsOutputPlotFile.write(" " + (String) aST.nextElement());
}
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
}
if (itsXRange != null) {
itsOutputPlotFile.write("set xrange " + itsXRange);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsX2Range != null) {
itsOutputPlotFile.write("set x2range " + itsX2Range);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsYRange != null) {
itsOutputPlotFile.write("set yrange " + itsYRange);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsY2Range != null) {
itsOutputPlotFile.write("set y2range " + itsY2Range);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsTRange != null) {
itsOutputPlotFile.write("set trange " + itsTRange);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
if (itsAutoscaleAfterRanges) {
if (itsAutoscale != null) {
itsOutputPlotFile.write("set autoscale");
if (!itsAutoscale.equals("")) {
StringTokenizer aST = new StringTokenizer(itsAutoscale, ",");
boolean aFirst = true;
while (aST.hasMoreElements()) {
if (aFirst) {
aFirst = false;
}
else {
itsOutputPlotFile.write(";set autoscale");
}
itsOutputPlotFile.write(" " + (String) aST.nextElement());
}
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
}
itsOutputPlotFile.write("plot");
if (itsRanges != null) {
itsOutputPlotFile.write(" " + itsRanges);
}
if (itsGraphs.size() == 0) {
itsOutputPlotFile.write("\t\"" + itsDataFileName + "\"");
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
else {
itsOutputPlotFile.write("\n");
}
}
else {
for (int i = 0; i < itsGraphs.size(); i++) {
Graph aGraph = (Graph) itsGraphs.elementAt(i);
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write(" ");
}
else {
itsOutputPlotFile.write("\t");
}
if (aGraph.isFunction()) {
// function
itsOutputPlotFile.write(aGraph.getFunction());
}
else {
// data file
itsOutputPlotFile.write("\""
+ duplicateDoubleBackSlashes(aGraph.getDataFileName())
+ "\"");
// columns to use
if (aGraph.getUsing() != null) {
itsOutputPlotFile.write(" using " + aGraph.getUsing());
}
// data modifiers
if (aGraph.getDataModifiers() != null) {
itsOutputPlotFile.write(" " + aGraph.getDataModifiers());
}
}
// axes to use
if (aGraph.getAxes() != null) {
itsOutputPlotFile.write(" axes " + aGraph.getAxes());
}
// title / name
if (aGraph.getName() != null) {
if (aGraph.getName().equals("")) {
itsOutputPlotFile.write(" notitle");
}
else {
itsOutputPlotFile.write(" title \"" + aGraph.getName() + "\"");
}
}
// style
if (aGraph.getStyle() != Style.NOT_SPECIFIED) {
itsOutputPlotFile.write(" with "
+ Style.toString(aGraph.getStyle()));
if (aGraph.getLineType() != LineType.NOT_SPECIFIED) {
itsOutputPlotFile.write(" lt " + aGraph.getLineType());
}
if (aGraph.getPointType() != PointType.NOT_SPECIFIED
&& (aGraph.getStyle() == Style.POINTS || aGraph.getStyle() == Style.LINESPOINTS)) {
itsOutputPlotFile.write(" pt " + aGraph.getPointType());
}
}
if (i != itsGraphs.size() - 1) {
itsOutputPlotFile.write(",");
}
if (!itsCommandsOnOneLine) {
if (i != itsGraphs.size() - 1) {
itsOutputPlotFile.write("\\");
}
itsOutputPlotFile.write("\n");
}
}
}
if (itsCommandsOnOneLine) {
itsOutputPlotFile.write("; ");
}
if (itsTerminal == Terminal.NOT_SPECIFIED && itsOutputFileName == null) {
itsOutputPlotFile.write("pause -1 \"Hit return to continue\"\n");
}
else {
itsOutputPlotFile.write("\n");
}
if (itsData != null) {
if (!itsCommandsOnOneLine) {
throw new IllegalArgumentException(
"Commands should be on one line when data has been set");
}
itsOutputPlotFile.write(itsData);
}
itsOutputPlotFile.close();
if (itsOutputFileName != null) {
File aFile = new File(itsOutputFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
throw new IOException("Could not create directory "
+ aFile.getParent());
}
}
}
Process aProcess = Runtime.getRuntime().exec(
itsGnuplotExecutable + " " + aFileName);
Thread.currentThread().sleep(1000);
aProcess.waitFor();
// TODO Remove temporary -plt.txt file and make it configurable for
// debug purposes via system properties. Better and preferred is to
// directly stream it into gnuplot.
}
}