Saturday, December 27, 2008

Free Premium JFreeCharts Info.

I had to use JFreeCharts all of a sudden, kind of in a rush. I had no previous information about it until I could get hold of a free JFreeCharts Developer Guide, I am lucky that I could use it since my company paid for it.

After accomplishing a few tasks using JFreeCharts, I wanted to summarize what I discovered about JFreeCharts. Well, without any further due I am going to get right into it:
To start with, it is an API to give a "face" to your data, so in simpler terms JFreeCharts helps you turn numbers into diagrams and the output diagrams can be be piped into as a PDF(using iText) or SVG(using batik) file. It fits well in a situation when you as a developer have to convey the meaning of the data in the database to a separate team like business or maybe the architecture team.

We have a tool that we developed in-hosue, the tool basically saves time since it makes querying the database a lot easier. There are tables in the database that tell how many times queries were fired and how many people were accessing the database and doing the four holy things that we always do with the database(add, edit, get, delete). Now, using the JFreeCharts methods I could give a meaningful representation to the logs in these tables, in terms of Pie Charts, Bar Charts, Line Charts, Scatter Plots, Time Series Charts etc.

I will get into details of my accomplishments with JFreeCharts but before thast just for a simple overview, the following is a list of packages into which all of the class libraries are bundled, for a resonoably advanced reader, the hierarchial names should give an initial insight into what can be accomplished with the classes and inturn the methods inside them.

(for brevity I have put everything that can be dotted in the parenthesis)
org.jfree.chart (annotations, axis, block, entity, event, imagemap, labels, needle, plot, renderer, renderer.category, renderer.xy, servlet, title, ui, urls,

(for brevity I have put everything that can be dotted after "data" in parenthesis)
org.jfree.chart.data (category, contour, function, gantt, general, jdbc, statistics, time, xml, xy)

Pie Chart can be generated using data which can be cast to the PieDataset interface and the same chart can be displayed with a 3D effect.

Data which confirms to the CategoryDatset interface can be displayed as Bar Chart and/or Line Chart

Similarly, data that confirms to the XYDataset interface can be used to generate a range of chart types. By default lines are drawn between datasets. So one can give just the X, Y data points and the interface will draw the line to connect them by default. Extentions of XYDataset such as HighLowDataset and IntervalXYDataset can be used to high-low-open-close data and histograms respectively.

Area Charts and Stacked Area Charts can be generated using data of CategoryDataset and XYDataset interface types. The list of kids of Charts that can be generated using JFreeCharts goes on and on, you name it.

There are three clear-cut steps that goes into getting data, presenting data and displaying the data into diagrams, the steps are as follows:

The steps:

1. Create the appropriate dataset using the data in hand(technically in the tables of your database system)

2. "Presenting the Data" created(or cast) in step 1. Involves creating a JFreeChart object which will draw the chart, eg. ChartFactory can draw the data in step 1.

3. The final step is "Displaying Chart" for eg. displaying the chart in a frame on the screen.


// Step 1 (creating the dataset, in other terms casting your data into a JFreeChart Dataset type)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Amar" , 30.4);
dataset.setValue("Akbar" , 19.6);
dataset.setValue("Anthony" , 85.5);

//Step 2 (creating the chart)
JFreeChart chart = ChartFactory.createPieChart("The Chart", dataset, true, true, false);//starting from the frist boolean: legend, tooltips, urls

//Step 3 (creating and displaying a frame)
ChartFrame frame = new ChartFrame("Example", chart);
frame.pack();
frame.setVisible(true);




Once a chart object is created in step 2, a plethora of further customization eg. setting background color, getting the plot object fromt he chart object for further customization and in-turn a "renderer" object can be obtained from the the plot object to customize colors and other like attributes.

Thanks for reading and Thanks for stopping by!!

~Nirmal

Keep your friends close and your enemies closer.
Sun-Tzu

0 comments: