- JFreeChart api is for generating Different Charts.
- chart type supported by this library line charts, scatter plots, time series charts, high-low-open-close charts, candlestick plots, Gantt charts, etc.
- Download Link:-
Project Structure:-
Test JFreeChart:-
package com.sandeep.jfreechart.servlet;
import java.awt.image.RenderedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* Servlet implementation class JFreeChartServlet
*/
public class JFreeChartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JFreeChartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/png");
ServletOutputStream os = response.getOutputStream();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(76, "Java", "Sandeep");
dataset.addValue(30, "Java", "Sangeeta");
dataset.addValue(50, "Java", "Surabhi");
dataset.addValue(20, "Java", "Sumanta");
dataset.addValue(10, "Oracle", "Sandeep");
dataset.addValue(90, "Oracle", "Sangeeta");
dataset.addValue(23, "Oracle", "Surabhi");
dataset.addValue(87, "Oracle", "Sumanta");
JFreeChart chart = ChartFactory.createAreaChart("Area Chart", "",
"Value", dataset, PlotOrientation.VERTICAL, true, true, false);
RenderedImage chartImage = chart.createBufferedImage(300, 300);
ImageIO.write(chartImage, "png", os);
os.flush();
os.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}