Ruby on Rails
HowtoGenerateJFreeCharts

JFree Charts

Introduction

JFreeChart is a free Java chart library that makes it easy for developers to display professional quality charts in their applications.

JFreeChart supports pie charts (2D and 3D), bar charts (horizontal and vertical, regular and stacked), line charts, scatter plots, time series charts, high-low-open-close charts, candlestick plots, Gantt charts, combined plots, thermometers, dials and more.

JFreeChart can be used in applications, applets, servlets and JSP.

This article shows how to generate a JFree Chart(e.g : XYLine Chart) with Rails.

The existing plugins in Ruby on Rails for creating graphs like Gruff or Sparklines have certain limitations and hence for more professional looking and customised graphs we chose JFree Charts.
This document explains how to create the graph using JFree Charts and then call the java program through Ruby On Rails.

Steps Involved :

What we do now is, use rails to execute this java program.
This will create the chart and store it in the images folder from where the rails code will pick it up and display it in the browser window.
JFree Charts is integrated into Rails by execution of a simple Java application through an IO pipe that creates a JFree Chart and stores it in the public\images folder in the rails application directory.

The whole interface consists of the following parts:

I added a directory jfree to my rails application root. It contains two subfolders

The Controller code :

  #in controller
def CreateChart
       pipe = IO.popen "java -cp C:\\InstantRails\\rails_apps\\project\\jfree\\src;C:\\InstantRails\\rails_apps\\project\\jfree\\lib\\jcommon-1.0.0-rc1.jar;C:\\InstantRails\\rails_apps\\project\\jfree\\lib\\jfreechart-1.0.0-rc1.jar; CreateChart" 
       pipe.close
       redirect_to "/graph/report" 
    end

Anonymous Coward wonders: Why are you using IO.popen() when you’re not reading/writing the standard input/output streams of the child process? In this particular case, a simple call to the Kernel::system() method will suffice!