Wednesday, April 28, 2010

JFree Charts

                                                                                                                     -Adam            

Graphs and Charts is the most efficient method for displaying information in a simple manner. Using this form of representation helps it's viewers to understand and interpret the information more easily and efficiently, which otherwise could be a very difficult and tedious process.


All forms of Graphs and Charts embodies some Visual content in the representation

JFreeChart is a free open source java chart library. It is used to generate the charts like Area and Line Charts, Pie charts, Time Series, Bar Charts, Bubble Charts, Gantt Charts, 3D. Developers get a better choice to add professional quality charts in swing and web based application by JFreeChart. JFreeChart provides many interactive features like tooltips etc. JFreeChart includes some of the following features as given below.

• JFreeChart have well documented API, which supports the wide range of charts.
• JFreeChart supports Swing components, vector graphics file format (like PDF, SVG and EPS) and image files (like JPEG and PNG).
• JFreeChart is easy to extend and it can be used for developing client side and server side applications.

• JFreeChart requires the JCommon class library.

• interactive zooming;

• events

• tooltips

Downloading and Installing JFreeChart


JFreeChart can be downloaded from http://www.object-refinery.com/jfreechart/index.html
There are two versions of the JFreeChart download:
Files                                                               Description

jfreechart-0.9.1.tar.gz                               JFreeChart for Linux/Unix.
jfreechart-0.9.1.zip                                   JFreeChart for Windows.

Unzip the jar file; this will extract all the source, run-time and documentation files
Into a new directory called jfreechart-0.9.1.

JFreeChart is written entirely in Java, and should run on any implementation of the Java 2 platform (JDK1.3 or later recommended).

Setting Idea Work Space

Create a new Java project, include J2ee environment.

Click in File ---Settings--Project Settings--Libraries--Create New project Library---Attach Jar directory --include the Jfree Jars from the exploded jfreechart-0.9.1 directory.
Now you can create samples.
Sample Charts

Pie Charts

JFreeChart can create pie charts using any data that conforms to the PieDatasetInterface:


Bar Charts
A range of bar charts can be created with JFreeChart, using any data that conforms to the CategoryDataset interface.




The example is a horizontal bar chart

XY Plots


A third type of dataset, the XYSeriesCollectionDataset, is used to generate further chart types. The standard XY plot has numerical x and y axes. By default, lines are drawn between each data point:



JFreeChart supports time series charts with TimeSeries:

The Basic Structure of Jfree Chart
Chart and Dataset:


• In JFreeChart project, you have to create a Dataset for creating a chart. It contains the data, which displays in the chart. JFreeChart have many Dataset objects that are used to create different types of charts. After creating the Dataset object actual chart can be created.

The JFreeChart class coordinates the entire process of drawing charts. One method :public void draw (Graphics2D g2, Rectangle2D area);

...instructs the JFreeChart object to draw a chart onto a specific area on a graphics device.

In broad terms, JFreeChart achieves this by obtaining data from a Dataset, and delegating the drawing to a Plot object (which, in turn, delegates the drawing of individual data items to a CategoryItemRenderer or a XYItemRenderer, depending on the plot type).

We can set background images to chart with plot subclass objects.
CategoryPlot plot = (CategoryPlot) chart.getPlot ();

BufferedImage image = ImageIO.read (new File ("C:\\Documents and Settings\\shaikad\\Desktop\\Team Pics\\DSC09424.JPG"));

plot.setBackgroundImage(image);

The JFreeChart class can work with many different Dataset implementations, and even more Plot subclasses.

The following table summarizes the combinations that are currently available

Dataset Compatible                                                            Plot Types:

PieDataset                                                                          PiePlot
CategoryDataset                                                                CategoryPlot subclasses with various renders
XYDataset                                                                         XYPlot with various renderers.
IntervalXYDataset                                                              XYPlot with a VerticalXYBarRenderer.
HighLowDataset                                                                 XYPlot with a HighLowRenderer.
CandleStickDataset                                                             XYPlot with a CandleStickRenderer.

There are a lot of combinations, but don’t worry, just keep in mind that a chart usually has one Dataset and one Plot

Creating Your First Chart

To illustrate, let’s create a pie chart. First, we need to create a dataset that Implements the PieDataset interface. The DefaultPieDataset class in the JCommon Class Library4 is designed just for this purpose:
DefaultPieDataset pieDataset = new DefaultPieDataset ();
pieDataset.setValue ("Adam", new Integer (40));
pieDataset.setValue ("Madhav", new Integer (30));
pieDataset.setValue ("Sekhar", new Integer (30));

Next, we need to create a chart. A convenient way to do this in JFreeChart is to use the ChartFactory class:
// create a chart...
JFreeChart chart = ChartFactory.createPieChart ("Work Distribution ", pieDataset, true);

Notice how we have passed a reference to the dataset to the factory method. The chart object retains this reference so that it can obtain data later on when it is drawing the chart.

Now we have a chart, but we don’t yet have anywhere to draw it. Let’s create a frame to display the chart in. The ChartFrame class contains the machinery required to display charts:

// create and display a frame...
JFreeChartFrame frame = new JFreeChartFrame ("Test", chart);
frame.pack();
frame.setVisible (true);

And that’s all there is to it...here is the complete program, so that you know which packages you need to import:

class PeiChartExample {
public static void main (String[] args) {
// create a dataset...
DefaultPieDataset pieDataset = new DefaultPieDataset ();
pieDataset.setValue ("Adam", new Integer(40));
pieDataset.setValue ("Madhav", new Integer(30));
pieDataset.setValue ("Sekhar", new Integer(30));

// create a chart...
JFreeChart chart = ChartFactory.createPieChart ("Work Distribution", data, true);
// create and display a frame...
ChartFrame frame = new ChartFrame ("Test", chart);
frame.pack ();
frame.setVisible (true);
}
}

Now Run this example, you will see the output.

Hopefully this has convinced you that it is not difficult to create and display charts with JFreeChart.
You can always Google more about Jfree.
Cewolf library to display the charts
Cewolf can be used inside a Servlet/JSP based web application to embed complex graphical charts of all kinds (e.g. line, pie, bar chart, plots, etc.) into a web page. Therefore it provides a full featured tag library to define all properties of the chart (colors, strokes, legend, etc.). Thus the JSP which embeds the chart is not polluted with any java code. Everything is described with XML conform tags.

Cewolf is based on JFreeChart and uses it's rendering engine to render the final chart image into the clients response stream. No files are created on server side. Everything is based on lightweight session objects and dynamic data analysis. Cewolf consists of one Servlet which handles the chart rendering and a tag library which translates the chart definition included in the JSP into an HTML Img tag which consults the rendering Servlet for retrieval of the appropriate chart.

Useful links:
http://www.jfree.org/jfreechart/
http://www.jfree.org/phpBB2/viewforum.php?f=3
http://cewolf.sourceforge.net/new/index.html

No comments:

Post a Comment