How do I run Gnuplot Scripts from a Java environment?

2.8k views Asked by At

All of the information I'm finding about using JavaPlot is about how to create plots and run them from a Java code-level. This is fine, but I already have some script files used that do what I want them to outside of Jave when a user executes them. Right now, I can open Gnuplot, and type the commands

gnuplot> load "script.plt"

And it will output me a nice .png graphic of the plot I have. In the script, one of the lines called is

plot "graphData.dat" title "Line Title"

The file 'graphData.dat' stores the co-ordinate information needed to plot my graph.

This works fine when the user double-clicks the 'script.plt' file in its folder (I'm running in a Windows environment). This is pretty flawless (minus the fact that the gnuplot process doesn't end properly, which is not what I'm asking about but if it rings any bells, I'd love to hear how to fix that, too).

The problem I'm facing is that I need to be able to run the script through gnuplot through a java environment. My program needs to automatically export a .png graphic using gnuplot. I've done some looking around, and JavaPlot seems to be the library of choice for Java Support in Gnuplot, but I can't get it to do what I want: simply running an instance of gnuplot that calls the aforementioned load command.

Any help or advice I can get would be appreciated, I've tried forsaking the script file, I've tried to dynamically call the load command (but not from the correct place, apparently), and I've tried plotting the .dat file directly, which it really doesn't like. As long as I can get that script running, I'm set.

2

There are 2 answers

0
mgilson On BEST ANSWER

One thing that I notice right away: simply loading a script from the gnuplot prompt (i.e.)

gnuplot> #We've already entered gnuplot from the commandline
gnuplot> load "script.plt"

is completely equivalent to passing the script name as the first argument to gnuplot on the commandline (i.e.)

gnuplot script.plt

This is also equivalent to gnuplot -e 'load "script.plt"' from the commandline. This simplifies your problem because now you don't need to figure out how to communicate with gnuplot via pipes, you only need to figure out how to issue a system call in Java -- A task which I would assume is reasonably simple for someone who codes in java. In python, I could do import os; os.system("gnuplot script.plt").

I think the second, third and fourth answers in the link posted by @andersoj gives an examples and links.

0
Changwang Zhang On

JavaGnuplotHybrid library is what you need: hybrid programming with Java and Gnuplot.

  1. Hybrid programming with Java and Gnuplot
  2. Very light weight (just three core Classes)
  3. Use tags in Gnuplot code to execute functions or get fields' values in Java.
  4. Support both synchronized and asynchronized running of Gnuplot in Java. (synchronized: your java program will wait until you close the popped Gnuplot window; asynchronized: you java program will not wait.)
  5. Capture error/normal text output of Gnuplot to the java terminal
  6. Read Gnuplot code from xml files
  7. Support Gnuplot code template.

For more details:

  1. Project page: https://github.com/mleoking/JavaGnuplotHybrid
  2. Examples: https://github.com/mleoking/JavaGnuplotHybrid/blob/master/javagnuplothybrid/doc/examples.md