In this tutorial, we will provide an example of using JFreeChart with Java Servlets API and create a web application that will display a chart on the browser when invoked from the client end. I will try to keep this as simple as possible so that anybody can run this example on the fly and see the power of JFreeChart in action. I will be using Tomcat 7.0.14 as my web server and you are free to use your choice of server as long as you are able to run the example properly. We will get started with this step by step guide that explains how to use JFreeChart for web applications;
Step-1: Download Tomcat from tomcat.apache.org and make sure the installation works. Now, I'm running the installation on a windows machine and I had to set JAVA_HOME environment variable for TomCat to work properly. Make sure that you set your TomCat installation properly and is able to access the Tomcat webserver before you proceed further. I will not dwelve more on this as this tutorial is targetted towards JFreeChart.
Step-2: We will now write a simple Java program that will act as a servlet and return a PNG image for us which will contain a Pie Chart created using JFreeChart. When you invoke this Java Servlet from the client browser, a PNG chart image would be returned by the server back to the client, and your web browser will render this image on the screen for you. Look at a commented version of the JFreeChart Servlet code below and follow the example;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
/* Code for the HTTP Servlet that will return the Pie Chart as a PNG image
back to the browser after generating it using JFreeChart API */
public class PieChartServlet extends HttpServlet {
public PieChartServlet() {
/* No code in the constructor for this demonstration */
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
try {
DefaultPieDataset myServletPieChart = new DefaultPieDataset();
myServletPieChart.setValue("Maths", 74);
myServletPieChart.setValue("Physics", 87);
myServletPieChart.setValue("Chemistry", 62);
myServletPieChart.setValue("Biology", 92);
myServletPieChart.setValue("English", 51);
JFreeChart mychart = ChartFactory.createPieChart("Programming - Colored Pie Chart Example",myServletPieChart,true,true,false);
response.setContentType("image/png"); /* Set the HTTP Response Type */
ChartUtilities.writeChartAsPNG(out, mychart, 400, 300);/* Write the data to the output stream */
}
catch (Exception e) {
System.err.println(e.toString()); /* Throw exceptions to log files */
}
finally {
out.close();/* Close the output stream */
}
}
}
Step-3: You now have to compile this servlet code and take out a class file. You will need the servlet-api.jar file available under apache-tomcat-7.0.14\lib folder for compiling the code. You will also need JFreeChart and JCommon jar files as they are required for creating the Pie Chart. A sample compilation example is provided below;
javac -classpath .;C:\Tomcat\apache-tomcat-7.0.14\lib\servlet-api.jar PieChartServlet.java
Once you have the class file ready, we can deploy this to the Tomcat server and check if we are able to get a pie chart in the output. We will continue this tutorial in another part where we will explain how to deploy this servlet in tomcat and return a chart image for a client request.
No comments:
Post a Comment