使用JavaPlot / Gnuplot进行3D绘图

时间:2012-07-06 15:56:48

标签: java eclipse gnuplot javaplot

我正在努力做标题所说的。显然gnuplot能够做到这一点,但我想用JavaPlot来调用它。 JavaPlot中的Graph3D class让我认为它是可能的,但由于我没有找到3D示例,并且几乎没有关于JavaPlot的文档,我只是粗略地了解如何解决这个问题。如果有人已经知道如何做到这一点,并且我的努力正在重新发明轮子,请赐教,但是目前我会继续进行,好像之前没有人试图这样做。

查看GNUPlot类,有一个plot方法,但是splot方法被注释掉了,GNUPlotExec类中没有相应的方法。我试图添加一个,但目前它仍然在2D中绘图。为了完全披露,我没有从头开始,而是修改了当前的绘图方法。

这是注释掉的GNUPlot.class splot方法

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

这是从绘图方法

派生的GNUPlotExec.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());
    }

}

这是我正在尝试运行的测试

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,而不是情节

1 个答案:

答案 0 :(得分:4)

想出来。我需要添加

p.new3DGraph();

在p.addPlot(myPlot)之前的main中; 希望这会帮助其他人造成人,JavaPlot上没有任何内容