JFreeChart is an open source and 100% java based. Using it is easy and helps to display quality charts.
Here version 2 of struts is used.This Example helps to develop Bar chart output when corresponding values are supplied.Here the values are hard coded.
Prerequisites
jfreeChart.jar
jcommon.jar
these jars can be downloaded from the following site:
http://www.java2s.com/Code/Jar/jfree/jfreechart.jar.htm
http://www.java2s.com/Code/Jar/jfree/jcommon.jar.htm
download the zip file for windows platform
The Action class
The action class outputs the image as output stream. The response is taken from action and displayed in JSP page.
package com.companyname.project;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.data.DefaultCategoryDataset;
public class BarChart extends ActionSupport {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
CategoryPlot plot = null;
BarRenderer renderer = null;
JFreeChart jFreeChart =null;
public String execute()
{
return SUCCESS;
}
public String getImage()throws Exception
{
try
{
File image = File.createTempFile("image", "temp");
dataset.addValue(new Double(15), "","2008");
dataset.addValue(new Double(20),"","2009");
dataset.addValue(new Double(10), "", "2010");
dataset.addValue(new Double(50),"","2011");
final CategoryAxis categoryAxis = new CategoryAxis("Years");
categoryAxis.setLabelFont(new Font("Verdana",Font.PLAIN,8));
final ValueAxis valueAxis = new NumberAxis("Performance");
valueAxis.setLabelFont(new Font("Verdana",Font.PLAIN,8));
renderer = new BarRenderer();
renderer.setSeriesPaint(0, Color.LIGHT_GRAY);
renderer.setDrawBarOutline(false);
plot = new CategoryPlot(dataset, categoryAxis, valueAxis,renderer);
plot.setOrientation(PlotOrientation.VERTICAL);
jFreeChart = new JFreeChart(" Performance", JFreeChart.DEFAULT_TITLE_FONT,plot, true);
jFreeChart.setBorderVisible(true);
jFreeChart.setBackgroundPaint(new Color(139,163,241));
jFreeChart.setBorderPaint(new Color(207,230,245));
jFreeChart.getTitle().setFont(new Font("Verdana",Font.PLAIN,12));
ChartUtilities.saveChartAsJPEG(image, jFreeChart, 300, 200);
FileInputStream fileInStream = new FileInputStream(image);
java.io.OutputStream outStream = getServletResponse().getOutputStream();
long fileLength;
byte[] byteStream;
fileLength = image.length();
byteStream = new byte[(int) fileLength];
fileInStream.read(byteStream, 0, (int) fileLength);
getServletResponse().setContentType("image/jpg");
getServletResponse().setContentLength((int) fileLength);
getServletResponse().setHeader("Cache-Control","no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
getServletResponse().setHeader("Pragma", "no-cache");
fileInStream.close();
outStream.write(byteStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
e.printStackTrace();
}
return NONE;
}
}
The JSP page
The JSP page displays the image using tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
img src= <%=request.getContextPath()%>/namespace/actionName.action" height="300" width="350">
Here action class is the same, only the method differs. The execute method of action is meant for forwarding to the JSP page, and the second function is actually generating the image, which is called in JSP.
The output is a bar chart with hard coded values
