Adding Charts to Web-Based J2EE Applications, Page 3
More Advanced Charts
You have thus far seen examples of pie charts. More complex types of charts can show one or more series within one or more categories. JFreeChart's CategoryDataset interface represents data containing series grouped within categories.
I have created a second Java class in the file EmployeesByDeptAndQtr.java. Like the previous class, EmployeesByDeptAndQtr also implements Cewolf's DatasetProducer interface. As its name implies, this class executes a database query that will display head counts for each department, categorized by the quarter of the employee's hire date. Here's the query sent to the database:
SELECT COUNT(emp_id), QUARTER(emp.hire_date) quarter, dept.name FROM emp, dept WHERE emp.dept_id = dept.dept_id GROUP BY dept.name,QUARTER(emp.hire_date);
With the returned DefaultCategoryDataset, you can create a number of chart types. Each of these charts consists of an x and a y plot. The x plot plots data horizontally; the y plot plots data vertically. Depending on the type of chart used, one plot will display value points, the other categories. As with the pie chart, the data series will be color coded and included in a chart legend. You will display three of them: a horizontal bar, a vertical bar, and a line chart. The bar charts differ in how they map their x and y coordinates. A line chart displays a different type of graphic. It identifies distinct data points, connected by a line that runs across the chart.
The content of categorychart.jsp should look familiar as it uses many of the same constructs as piechart.jsp. It declares a DatasetProducer instance, and then lists a set of JSP tags to manage the charts. This JSP file contains separate tags for chart types 'horizontalBar3D', 'verticalBar3D', and 'line'. All three charts represent the same CategoryDataset (provided by the DatasetProducer). Only two new tag attributes are introduced: xaxislabel and yaxislabel. Figure 2 shows the resulting Web page. Below are the three chart images produced in greater detail:
Figure 2. Various Category Charts
This article's intended purpose was to provide you with an introduction to creating graphic charts using JFreeChart and Cewolf. I strongly encourage you to visit each site and explore the additional chart types and chart enhancements that are available to you. Extensive documentation of JFreeChart is available for purchase. Both projects have community forums to help answer questions. Also, both sites offer on-line Javadocs that document source code. By combining JFreeChart and Cewolf, J2EE developers have a useful, and easy to use, tool that can be used to satisfy user's data analysis requirements.
About the Author
Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 7 years in IT, specializing in J2EE and Oracle analysis and development.