jGNUPlot - Erfahrungen?

PH1L.tv

Mitglied
Hallo Community,

ich möchte einen einfachen Plot erstellen. Dazu verwende ich jGNUPlot (Link), welches die guten gnuplot-Funktionalitäten in Java einfach zur Verfügung stellt.

Nun habe ich folgendes Problem: Wenn ich einen der Beispiel-Codes (Link) ausführen möchte, wird zwar die Datei erstellt, die an gnuplot übergeben wird. Aber der Aufruf "gnuplot dateinameblabla.txt" erfolgt nicht. Das heißt, das Programm erstellt die Datei mit den gnuplot-Anweisungen, nicht aber die eigentliche Grafik.

Alle Pfade sind richtig gesetzt, wenn ich den Aufruf von Hand im Terminal mache, funktioniert alles. Es werden auch keine Exceptions geworfen oder ähnliches.
Hat jemand eine Idee, wie man jGNUPlot dazu bewegen kann, zum Schluss nun auch noch den eigentlichen Plot zu erstellen?

Freue mich über Anregungen! Danke und viele Grüße,
Philip

Konfiguration: NetBeans, Mac OS X 10.7,
 

Anhänge

  • FMcontroller.java
    917 Bytes · Aufrufe: 8

PH1L.tv

Mitglied
Hier übrigens noch der wesentliche Part der plot()-Implementiereung von jGNUPlot:

Java:
   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.
   }
}
 

PH1L.tv

Mitglied
Hi Parabol,

jepp, der Pfad ist auf dem Mac /usr/local/bin/gnuplot

Check durch Eingabe im Terminal sagt, dass das stimmt. Programm gibt auch keinen Fehler aus.

Das Problem muss irgendwoe hier liegen:

Java:
//Plot.plot() - Zeile 526:
Process aProcess = Runtime.getRuntime().exec(itsGnuplotExecutable + " " + aFileName);

Anscheinend kann die JVM auf dem Mac diesen Befehl nicht korrekt ausführen. Jemand eine Idee, warum?

Danke & VG,
Philip
 

parabool

Bekanntes Mitglied
Du hast den Pfad zum gnuplot - Programmordner gesetzt JAVA]Plot.setGnuplotExecutable("/usr/local/bin/gnuplot")[/code], aber er muss auch direkt auf das Executable zeigen.

Bei mir z.B.:
Java:
Plot.setGnuplotExecutable("D:/gnuplot/bin/gnuplot.exe");
 

PH1L.tv

Mitglied
Wie in meinem letzten Post geschrieben: Ich arbeite auf einem Mac und da ist der angegebene Pfad jener zum Programm. Es gibt auf Unix-Systemen keine Dateiendungen von Programmen.
 

parabool

Bekanntes Mitglied
... ist mir bekannt.

Soweit ich das sehe sind die Programmdateien, Bibliotheken usw. auch bei den Versionen für andere Betriebssysteme im Folder "gnuplot".
Diese sind bei dir also direkt unter "/usr/local/bin/"
Ok.

Vielleicht fehlende Ausführungsrechte? (obwohl - keine Fehlermeldung)


:bahnhof:
 

PH1L.tv

Mitglied
/usr/local/bin/gnuplot ist kein Folder, sondern der Pfad zum Programm.

Anyway, hier ist die Lösung des Problems:

Der korrekte Pfad ist: /opt/local/bin/gnuplot

Wie kommt man drauf? Im Terminal eingeben: type -p gnuplot

Danke an alle, die sich Gedanken gemacht haben!
VG,
Philip
 

Ähnliche Java Themen


Oben