2012-07-06 89 views
0

我正在嘗試做什麼標題說。顯然gnuplot能夠做到這一點,但我想用JavaPlot來調用它。 JavaPlot中的Graph3D class使我認爲它是可能的,但由於我沒有找到3D例子,並且幾乎沒有關於JavaPlot的文檔,所以我只有一個大概的想法。如果有人已經知道如何做到這一點,而我的努力正在重新發明輪子,請賜教,但目前我會繼續進行,好像以前沒有人嘗試過這樣做。使用JavaPlot/Gnuplot進行3D製圖

看看GNUPlot類,有一個plot方法,但splot方法被註釋掉了,並且在GNUPlotExec類中沒有相應的方法。我試圖添加一個,但目前它仍然以二維方式繪製。爲了充分披露,我沒有從頭開始,而是修改了當前的繪圖方法。

這是被註釋掉

public void splot() throws GNUPlotException { 
    exec.splot(param, term); 
} 

的GNUPlot.class splot方法,這是從劇情方法

void splot(GNUPlotParameters par, GNUPlotTerminal terminal) throws GNUPlotException { 
    try { 
     final GNUPlotTerminal term = terminal; // Use this thread-aware variable instead of "terminal" 
     final String comms = getCommands(par, term); // Get the commands to send to gnuplot 
     final Messages msg = new Messages(); // Where to store messages from output threads 

     /* Display plot commands to send to gnuplot */ 
     GNUPlot.getDebugger().msg("** Start of splot commands **", Debug.INFO); 
     GNUPlot.getDebugger().msg(comms, Debug.INFO); 
     GNUPlot.getDebugger().msg("** End of splot commands **", Debug.INFO); 

     /* It's time now to start the actual gnuplot application */ 
     String[] command; 
     if (ispersist) { 
      command = persistcommand; 
     } else { 
      command = nopersist; 
     } 
     command[0] = getGNUPlotPath(); 
     { 
      String cmdStr = ""; 
      for (String cmd : command) { 
       cmdStr += cmd + " "; 
      } 
      GNUPlot.getDebugger().msg("exec(" + cmdStr + ")", Debug.INFO); 
     } 
     final Process proc = Runtime.getRuntime().exec(command); 

     /* Windows buffers DEMAND asynchronus read & write */ 

     /* Thread to process the STDERR of gnuplot */ 
     Thread err_thread = new Thread() { 

      public void run() { 
       BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 
       StringBuffer buf = new StringBuffer(); 
       String line; 
       try { 
        while ((line = err.readLine()) != null) { 
         line = parseErrorLine(line, "gnuplot> splot"); 
         line = line.replace("input data ('e' ends) >", "").trim(); // Remove entries having the "input data" prompt 
         if (line.equals("^")) { 
          line = ""; 
         } // Ignore line with error pointer 
         if (!line.equals("")) {  // Only take care of not empty lines 
          if (line.indexOf(GNUPlotParameters.ERRORTAG) >= 0) { 
           msg.error = "Error while parsing \'splot\' arguments."; // Error was found in plot command 
           break; 
          } 
          buf.append(line).append('\n'); 
         } 
        } 
        err.close(); 
        msg.output = buf.toString(); // Store output stream 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }; 
     /* Thread to process the STDOUT of gnuplot */ 
     err_thread.start(); 
     Thread out_thread = new Thread() { 

      public void run() { 
       msg.process = term.processOutput(proc.getInputStream()); // Execute terminal specific output parsing 
      } 
     }; 
     out_thread.start(); 

     /* We utilize the current thread for gnuplot execution */ 
     OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); 
     out.write(comms); 
     out.flush(); 
     out.close(); 


     try { 
      proc.waitFor(); // wait for process to finish 
      out_thread.join(); // wait for output (terminal related) thread to finish 
      err_thread.join(); // wait for error (messages) output to finish 
     } catch (InterruptedException ex) { 
      throw new GNUPlotException("Interrupted execution of gnuplot"); 
     } 

     /* Find the error message, if any, with precendence to the error thread */ 
     String message = null; 
     if (msg.error != null) { 
      message = msg.error; 
     } else { 
      message = msg.process; 
     } 

     /* Determine if error stream should be dumbed or not */ 
     int level = Debug.VERBOSE; 
     if (message != null) { 
      level = Debug.ERROR; 
     } 
     GNUPlot.getDebugger().msg("** Start of error stream **", level); 
     GNUPlot.getDebugger().msg(msg.output, level); 
     GNUPlot.getDebugger().msg("** End of error stream **", level); 

     /* Throw an exception if an error occured */ 
     if (message != null) { 
      throw new GNUPlotException(message); 
     } 

    } catch (IOException ex) { 
     throw new GNUPlotException("IOException while executing \"" + getGNUPlotPath() + "\":" + ex.getLocalizedMessage()); 
    } 

} 

得到的GNUPlotExec.class splot法這是我的測試我m試圖運行

public static void main(String[] args) { 

    GNUPlot p = new GNUPlot("path goes here"); 

    FunctionPlot myPlot = new FunctionPlot("tan(x)"); 

    p.addPlot(myPlot); 

    p.splot(); 
} 

我相信由gnuplot執行的命令是

gnuplot> _gnuplot_error = 1 
gnuplot> plot tan(x) title 'tan(x)' ; _gnuplot_error = 0 
gnuplot> if (_gnuplot_error == 1) print '_ERROR_' 
gnuplot>   undefined function: if 

當然應該說splot和,不積

回答

4

想通了。我需要添加

p.new3DGraph(); 

in main before p.addPlot(myPlot); 希望這會幫助別人造成男人,JavaPlot上沒有任何東西