Jul
22
2008

Using JFreeChart from Rhino

Today I wanted to play around with JFreeChart a bit. As I did not want to repeat the edit-compile-link cycle all the time, I decided that it would be fun to use the Rhino scripting engine for playing around.

Unfortunately, this did not seem to work at first:

torsten@pulsar:~$ rhino
Rhino 1.7 release 1 2008 03 22
js> importClass(org.jfree.chart.JFreeChart)
js: "<stdin>", line 2: Function importClass must be called with a class;
had "[JavaPackage org.jfree.chart.JFreeChart]" instead. at <stdin>:2

Basically, this error tells us that Rhino could not find the JFreeChart classes. The error message is not really helpful though…

Anyway, after a bit of looking around, I stumbled across a remark in the older “scripting Java” treatment:

[...] simply use the command

java -jar js.jar

Unfortunately the -jar option to java will overwrite your existing classpath.

It turns out that /usr/bin/rhino contains just that command. So to start Rhino with most libraries available, you can just run

export CLASSPATH=`echo /usr/share/java/*.jar|tr ' ' :`
torsten@pulsar:~$ java org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 1 2008 03 22
js> importClass(org.jfree.chart.JFreeChart)

The first command will define the Java class path to contain all the installed .jar files in /usr/share/java. The second command starts the Rhino shell, which has gained lots of power now. Look at the following example:

JFreeChart plot created in Rhino

importPackage(org.jfree.chart);
importPackage(org.jfree.data.xy);
importPackage(org.jfree.chart.plot);
importPackage(javax.swing);

series = new XYSeries(“First”);
for (i = 0; i < 100; i++)
series.add(i, i*i);
dataset=new XYSeriesCollection();
dataset.addSeries(series);

chart = ChartFactory.createXYLineChart(“Line chart”,
“X”, “Y”, dataset, PlotOrientation.VERTICAL, true, true, false);
frame = new JFrame(“Plot”);

chartpanel = new ChartPanel(chart);
frame.add(chartpanel);
frame.pack();
frame.show();

posted in Uncategorized by admin

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

You must be logged in to post a comment.

 
Powered by Wordpress and MySQL. Theme by openark.org