Sunday 7 June 2015

JFreeChart Servlet Example

In this post I am going to explain, how to send a chart as servlet response.

Step 1: Create new Dynamic web project “jfreechart_demo”




Step 2: Convert project “jfreechart_demo” to maven project.



Step 3: Open pom.xml, update maven dependencies for jfreechart.

pom.xml   
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jfreechart_demo</groupId>
  <artifactId>jfreechart_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
    <jfree-chart-version>
      1.0.19
    </jfree-chart-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.jfree</groupId>
      <artifactId>jfreechart</artifactId>
      <version>${jfree-chart-version}</version>
    </dependency>

  </dependencies>
</project>

Step 4: Create a servlet “Demo.java”.





Demo.java
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * Servlet implementation class Demo
 */
@WebServlet("/Demo")
public class Demo extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    OutputStream out = response.getOutputStream(); /*
                             * Get the output stream
                             * from the response
                             * object
                             */
    try {
      DefaultCategoryDataset dataset = new DefaultCategoryDataset();
      dataset.addValue(500, "Company A Revenue", "2009");
      dataset.addValue(550, "Company A Revenue", "2010");
      dataset.addValue(450, "Company A Revenue", "2011");
      dataset.addValue(650, "Company A Revenue", "2012");
      dataset.addValue(550, "Company A Revenue", "2013");
      dataset.addValue(700, "Company A Revenue", "2014");

      dataset.addValue(200, "Company B Revenue", "2009");
      dataset.addValue(450, "Company B Revenue", "2010");
      dataset.addValue(398, "Company B Revenue", "2011");
      dataset.addValue(425, "Company B Revenue", "2012");
      dataset.addValue(770, "Company B Revenue", "2013");
      dataset.addValue(810, "Company B Revenue", "2014");

      /* create chart */
      JFreeChart chart = ChartFactory.createLineChart("", "Year",
          "Revenue in M$", dataset, PlotOrientation.VERTICAL, false,
          true, false);

      /* Set the HTTP Response Type */
      response.setContentType("image/png");

      /* Writes a chart to an output stream in PNG format */
      ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
    } catch (Exception e) {
      System.err.println(e.toString()); /* Throw exceptions to log files */
    } finally {
      out.close();/* Close the output stream */
    }
  }
}

Run above servlet, you will get following output.




Previous                                                 Next                                                 Home

No comments:

Post a Comment