Create bar chart servlet Java JFreechart Example

In this tutorial, we will explain how to create a Java servlet using JFreeChart that can return a stacked bar chart with a simple example. JFreechart is a very common and widely used Java library to create a wide range of charts. At the end of this example tutorial, you will really appreciate how easy it is to create the chart. The code is fully commented so that you can get a better understanding of what happens in the code and customize the code to your own needs. 

The step by step guide to create bar chart servlet using JFreechart and Java Servlets API is provided below:

Step -1: Download Apache TomCat and install it on your system. I won't cover setting up of TomCat in this tutorial. I have covered  this in detail earlier.Feel free to use the site search functionality if you would like to know how to setup TomCat.

Step-2: We present the full code to create the chart servlet here. This code will return a PNG image file to the browser and the browser should be able to render the chart for you. The full java code example which is tested working is provided below:

 1 /* Servlet code to generate bar chart using JFreeChart */
 2 /* Basic libraries required to support servlet code */
 3 import;
 4 import;
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 /* JFreechart specific libraries to generate chart */
10 import org.jfree.chart.ChartFactory;
11 import org.jfree.chart.ChartUtilities; /* This class will return the chart to the output stream */
12 import org.jfree.chart.JFreeChart; /* This class will hold the chart object */
13 import; /* This class will help the servlet to load chart data */
14 import org.jfree.chart.plot.PlotOrientation;
15 /* Main servlet code starts here */
16 public class BarChartServletExample extends HttpServlet {
17 public BarChartServletExample() {
18 /* No code in the constructor for this demonstration */
19 }
20 public void doGet(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22         OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
23         try {
24                /* Step - 1: Define the data for the bar chart  */
25                DefaultCategoryDataset bar_chart_servlet = new DefaultCategoryDataset();
26                bar_chart_servlet.addValue(33, "Q1", "Rome");
27                bar_chart_servlet.addValue(44, "Q1", "Cairo");
28                bar_chart_servlet.addValue(22, "Q2", "Rome");
29                bar_chart_servlet.addValue(12, "Q2", "Cairo");
30                bar_chart_servlet.addValue(56, "Q3", "Rome");
31                bar_chart_servlet.addValue(98, "Q3", "Cairo");
32                bar_chart_servlet.addValue(2, "Q4", "Rome");
33                bar_chart_servlet.addValue(15, "Q4", "Cairo");    
35                /* Step -2:Define the JFreeChart object to create bar chart */
36                JFreeChart BarChartObject=ChartFactory.createBarChart("jFreeChart Servlet Example Code","Country","Sales",bar_chart_servlet,PlotOrientation.VERTICAL,true,true,false);
38                /* Step -3: Set the response type to PNG */
39                response.setContentType("image/png"); /* Set the HTTP Response Type */
41                /* Step -4: Write the response to the output stream */
42                ChartUtilities.writeChartAsPNG(out, BarChartObject, 400, 300);/* Write the data to the output stream */
43         }
44         catch (Exception e) {
45                 System.err.println(e.toString()); /* Throw exceptions to log files */
46         }
47         finally {
48                 out.close();/* Close the output stream */
49         }
50         }
51 }

There are four main parts in the code:
  • Defining the data required to support the chart.
  • Creating the chart object.
  • Setting the output response type in the servlet as PNG. (You can also save the chart as JPG with this method)
  • Writing the response to the output stream.

1 comment:

  1. why this code always gives the the error that "starting server v7.0 server local host has encoutered a problem ..Server Tomcat v7.0 Server at localhost failed to start."