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 {
}
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
#in view "report.rhtml"
<html>
<img src ="\images\SampleChart.PNG" align="center" />
</html>
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 {
}
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
#in view "report.rhtml"
<html>
<img src ="\images\SampleChart.PNG" align="center" />
</html>