2012-07-06 56 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上没有任何东西