Dynamic Piecahrts in Java Using JFreeChart

Introduction

In this lesson, we are going to learn how to generate Charts in servlets using JFreeChart API.The API provided by JFreeChart tool is a nice library for creating variety of charts like Pie charts, bar graphs e.t.c.In this lesson we are going to generate PieChart that can be returned through servlet OutputStream as an image.

Generationg a Pie Chart

In my project i am going to use a single servlet class which is named PieChartServlet shown below.The output of this servlet will be shown  as below.


Pre-requisites

Required libraries for using JFreeChart are
  • jcommon-1.0.16.jar
  • jfreechart-1.o.13.jar

PieChartServlet.java


package friends;
// friends tutorials 
import java.io.IOException;
import java.io.PrintWriter;
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;
import dao.Reports;

/* 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 {
/**

*/
private static final long serialVersionUID = 1L;
public PieChartServlet() {
/* No code in the constructor for this demonstration */
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response); 
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
try {



int qn=Integer.parseInt(request.getParameter("qn"));
Reports r=new Reports();
//getChart(int) method in dao.Report class which returns an array of int 
int val[]=r.getChart(qn);
DefaultPieDataset myServletPieChart = new DefaultPieDataset();

myServletPieChart.setValue("a="+val[0],val[0] );
myServletPieChart.setValue("b="+val[1],val[1]);
myServletPieChart.setValue("c="+val[2],val[2]);
myServletPieChart.setValue("d="+val[3],val[3]);
int ansd=val[0]+val[1]+val[2]+val[3];
JFreeChart mychart = ChartFactory.createPieChart("Answered "+ansd,myServletPieChart,true,true,false); 
response.setContentType("image/png"); /* Set the HTTP Response Type */
ChartUtilities.writeChartAsPNG(out, mychart, 175, 180);/* 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 */
}
}
}

Generating a pie chart is easy using  JFreeChart.We can directly call this servlet  in browser and embed in jsp or redirect to a folder.



0 comments:

Post a Comment