Friday 03 August 2007

5: JRuby and JFreeChart

At last! Ruby and Java together! What a magic combination!

I've been a Java application developer for a decade now, and my Perl and other scripting activities have almost always been a completely separate activity. Although I tried JPython a few years ago, and it was fun and productive, it didn't stick. But now I've tried JRuby and got far more done preparing this snippet than expected, so I'm definitely impressed!

JRuby

The context for the first part of this article is that our development team works on Java desktop applications and have often considered having an embedded scripting environment for faster customization of the applications for specific needs, and custom system integration work. Our first try with JPython a few years back, while still hanging around in the code-base, is not used nearly as much as we had hoped. I have more hope for JRuby. Consider these quotes from doing a google of 'jruby':
  • I used to think that the killer feature of JRuby was making it easier to sneak Ruby into companies via a Java back-door. …JRuby’s killer feature is its brilliant integration with Java code. (Curt Hibbs –O’Reilly)
  • You will begin to hear reports of Rails running on the JVM in both development and production environments with very little disruption to existing code bases. This will be followed by blog post after blog post extolling the virtues of being able to drop down into Java for performance critical pieces of code and how nifty JFreeChart is. (Ryan Tomayko - Dec’06 Predictions for 2007)
  • But in the past year, it's become apparent that we could actually exceed Ruby's performance for specific cases in the near term, and for general cases over time. So the target market for JRuby seems to be changing from "Ruby users that must use Java VMs, libraries, and servers" to "Ruby users that want a better-performing, more scalable implementation". (Charles Nutter – blog 16-4-07)

JFreeChart

The second item in this talks title is JFreeChart. This arose from a need to add charting capabilities to the user interface of our pure java application. While it took me over two hours of Internet searching and reading of descriptions and reviews to find JFreeChart, it took only a further one hour to add the necessary charts to our application. Considering that this hour included trying out some of their demo code, and reading some basic JFreeChart documentation, I was amazed at how quickly it was possible to code for this amazingly complete charting library.

And it is truly a comprehensive library, as evident from this screenshot from the JFreeChart homepage:



JRuby & JFreeChart

As I arrived at the office on the day I planned to give this talk, I met a colleague in the stairwell, and told him that I had the idea of writing a JRuby application in less than ten lines that loads an Alcatel Switch data file into a JFreeChart bar-graph giving a cool live animation. I wanted to see how simple it would be to write an entire application in Ruby, including dynamic user interface including charts and relevance to the telecoms data and problem domain our team works with. Well, it ended up taking 20 lines of code (26 if you include comments and blank lines), but that is still impressively short considering all it does:



OK. It is a bit longer than most ruby snippets I usually like to present, but it is very easy to read, with three sections clearly separated by comment lines. The first sets up the required application swing components, the second prepares the JFreeChart chart and dataset, and the third is the actual plain ruby code for reading the telecoms switch data file, parsing it and adding the relevant statistics to the chart, while conveniently modifying the chart title dynamically. As usual, the same application in pure java is at least three times as many lines.



It is hard to appreciate the coolness of seeing this chart dynamically update itself while the data is loaded from disk. The chart animates, with the title and the bars changing smoothly. Of course, dynamic animation has little engineering value to our customers, and we did not use that feature in our actual commercial application, but I definitely had jaws dropping in the room when I gave this talk :-)

My final impression:- Ruby is a lovely language, and Java is the industry standard platform with the widest 3rd party library support, and this combination is absolutely a killer combination. So although I started this talk with the idea of adding ruby scripting to java applications, I'm now thinking more of writing Ruby applications that simply use Java libraries.

And of course, I can't wait to try Rails on JRuby!

Links:

4 comments:

slonkak said...

Have you tried Rails with jRuby yet? Specifically, using JFreeChart with jRuby on Rails? I'm a noob when it comes to both Java and Ruby, so I'd be interested in any examples you may have using JFC/jRoR.

Craig Taverner said...

Sorry for the slow response, I had not enabled notification for comments on this blog. And sorry again, I have not tried Rails on JRuby. I have always used the standard ruby with my rails apps. This is also partly due to the fact that I've done no JRuby since this first trial. I am scheduled to do a lot of JRuby in July, so perhaps this will change.

But I could also comment that since rails is a server-side framework, I'm not sure I see the value of GUI libraries like JCF/Swing/JFreeChart in rails.

Libertario said...

You can use JFreeChart to generate your chart, create a JPEG or GIF image of it, then and serve it using a (j?)RoR app. No?

Craig Taverner said...

I've still not used JRuby with Rails, but I have used normal Ruby on Rails and JFreeChart in many Java contexts, and can see no reason why it should not work great for JRuby on Rails.

What I have used for Ruby on Rails is one of the many Flash based charting options, mainly because I liked the client side interactivity and higher print resolutions.