Adding Charts to Web-Based J2EE Applications

Business applications typically manage data. Therefore, developers often need to provide components that enable users to analyze that data. One way to do this is by providing users access to text-based reports. Another option is to provide a view to the data through a series of graphical charts. Charts can be an extremely useful tool because they allow users to visually compare data elements, spotting trends and patterns not easily discernable through reports when dealing with large amounts of data. Much of today's business software is Web-based and delivered through a browser. Nevertheless, requirements for these applications, namely data analysis requirements, remain the same. Luckily, if you are developing software with J2EE technologies, there are some open-source options to assist you. This article will demonstrate how J2EE developers can quickly add visually-appealing, graphical charts to their work.

JFreeChart and Cewolf—A Powerful Combination

JFree.org is a Web site that hosts many useful open-source projects. JFreeChart is just one such project. It is managed by David Gilbert. JFreeChart consists of a number of Java libraries that generate graphical charts based upon a set of data.

Cewolf is another open-source project. Cewolf comes with its own set of libraries, as well as a collection of JSP tags. The Cewolf project, founded by Guido Laures, gives developers an easy way to display JFreeChart-built charts in a JSP page. In other words, Cewolf is a layer that rests upon JFreeChart. Cewolf provides data input to JFreeChart, then displays the resulting image in a browser, as dictated by the JSP tags. Both projects' libraries are necessary to achieve the desired goal. The most recent version of Cewolf, version 0.9.8, supports JFreeChart up through, not by coincidence, version 0.9.8.

Concepts and Terminology

Graphical charts display one or more data 'series'. In some cases, these series may then be grouped into one or more 'categories'. A series is simply a collection of data values that is defined by name. For example, if we were to poll individuals on their favorite season, then 'Spring', 'Summer', 'Fall', and 'Winter' become the series names. In charts, each series is assigned a unique color or pattern and it usually appears in a legend. It is possible to group series into categories. If our previously mentioned poll were to be conducted over the course of two calendar years, year becomes a category that groups two sets of data series, one for each year. Deciding on a particular chart to represent your data (pie, line, bar, and so forth) often depends not only on how you want to display your data, but also on the complexity of the data you need to represent.

Charts display data, usually from a database, so JFreeChart requires a data source to perform its work. JFreeChart defines a base Java interface, called Dataset, to provide this data. Two of the most common Datasets are the CategoryDataset and the PieDataset. As you would expect, a PieDataset provides data to create pie charts. Pie charts are the simplest type of chart. They display a value for data series, and how each value contributes to the total value for all series. CategoryDataset is an interface for a more complex set of data that represents one or more series grouped by categories.

Cewolf provides an interface called DatasetProducer. Developers implement this interface to provide data to JFreeChart from a Web application. It is a DatasetProducer's responsibility to provide requested data, in the form of a JFreeChart Dataset, so that a chart may be constructed.

Getting Started

In this section, you will focus on the technical details necessary to add a pie chart to an application. The first requirement for this task is some sample data. I have created employee and department tables for the examples. Here is a bit of SQL used to create data in an hsql database:

Related Articles

Employees may be hired into one of four departments. The date of hire for each employee is captured in the emp table. Here is the entire SQL script if you would like to use it to compare against the charts you will derive from it.

In your Web application, you will need to include a number of libraries, including those from Cewolf and JFreeChart. By downloading the Cewolf distribution, you will receive all the necessary jar files. You should not need to download additional JFreeChart files. Place these jar files in your WEB-INF/lib folder:

jfreechart-0.9.8-demo.jar

Jfreechart-0.9.8.jar

Jcommon-0.8.0.jar

Commons-logging.jar

Cewolf.jar

Batik-xml.jar

Batik-util.jar

Batik-syggen.jar

Batik-dom.jar

Batik-awt-util.jar

Cewolf relies on a Java servlet, called CewolfRenderer, to render charts. This servlet accepts two input parameters. I will discuss these parameters below. For now, include the following servlet and servlet mapping definitions in your WEB-INF/web.xml: