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.
/*
* Main.java
*
* Created on June 12, 2007, 10:56 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
*
* @author sadkaz
*/
public class CreateChart {
/** Creates a new instance of Main */
public CreateChart(final String title) throws SQLException {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
final CreateChart firstchart = new CreateChart("First JFree Chart");
createChart();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
*/
private static void createChart() throws SQLException {
final XYSeries series1 = new XYSeries("First");
series1.add(15, 120);
series1.add(100, 500);
series1.add(15, 35);
series1.add(20, 11);
series1.add(50, 125);
series1.add(100, 35);
series1.add(125, 35);
series1.add(200,375);
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"First JFree Chart", // chart title
"X Axis", // x axis label
"Y Axis", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(1, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
// save the image to an appropriate location : The images folder in your Instant Rails application
try{
BufferedImage bImage = chart.createBufferedImage(500,270);
ImageIO.write(bImage, "PNG", new File("Path of the images folder in the rails heirarchy\\SampleChart.PNG"));
}
catch(IOException e)
{
}
}
}
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.
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!
#in view "report.rhtml"
<html>
<img src ="\images\SampleChart.PNG" align="center" />
</html>