0

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

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

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

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

这是从 plot 方法派生的 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

当然应该说是情节,而不是情节

4

1 回答 1

4

弄清楚了。我需要添加

p.new3DGraph();

在 p.addPlot(myPlot); 希望这会帮助其他人,因为人,JavaPlot 上什么都没有

于 2012-07-06T16:10:36.090 回答