JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java
JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java
How to create a dynamic Bar Chart using JFreeChart library in Java-
In Java creating any graph or chart like bar chart or pie chart is very easy. For this we have a very light weight but powerful library called JFreeChart. This is an open source library in Java that allows developer to generate charts/graphs when user wants graphs to be regenerated frequently and dynamically. JfreeChart can be downloaded from http://sourceforge.net/project/showfiles.php?group_id=15494&package_id=12428
For this below code snippet I’ve used version jfreechart-0.9.21.jar
The project consists of one jsp file and one servlet. Inside Jsp file in the tag the path of the servlet is given in the src attribute that invokes the servlet. The servlet is responsible in generating the bar chart based on the data given.
Graphtest.jsp
|
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html> <HEAD> <TITLE>TITLE> HEAD>
<BODY> <P>The Generated Chart is:<BR> <BR> P> <IMG src="GraphGen"> BODY> html:html>
|
GraphGen.java
|
package com.tctalk.chartgen.servlets;
import java.awt.Color; 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.chart.labels.StandardCategoryToolTipGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.urls.StandardCategoryURLGenerator; import org.jfree.data.category.DefaultCategoryDataset;
public class GraphGen extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { genGraph(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { genGraph(req, resp); }
public void genGraph(HttpServletRequest req, HttpServletResponse resp) { try { OutputStream out = resp.getOutputStream(); // Create a simple Bar chart System.out.println("Setting dataset values"); DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(30, "Girls", "SCIENCE CLASS"); dataset.setValue(30, "Boys", "SCIENCE CLASS");
dataset.setValue(10, "Girls", "ECONOMICS CLASS"); dataset.setValue(50, "Boys", "ECONOMICS CLASS");
dataset.setValue(5, "Girls", "LANGUAGE CLASS"); dataset.setValue(55, "Boys", "LANGUAGE CLASS");
JFreeChart chart = ChartFactory.createBarChart3D( "Comparison between Girls and Boys in Science, " + "Economics and Language classes", "Students Comparisons", "No of Students", dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.white);
// Set the background colour of the chart chart.getTitle().setPaint(Color.blue);
// Adjust the colour of the title CategoryPlot plot = chart.getCategoryPlot();
// Get the Plot object for a bar graph
plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.red); CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, Color.red); renderer.setSeriesPaint(1, Color.green); renderer.setItemURLGenerator(
new StandardCategoryURLGenerator( "index1.html", "series", "section")); renderer.setToolTipGenerator( new StandardCategoryToolTipGenerator());
resp.setContentType("image/png"); ChartUtilities.writeChartAsPNG(out, chart, 625, 500); } catch (Exception e) { System.err.println( "Problem occurred creating chart." + e.getMessage()); } }
}
|
And the output is:
Explanation of the Java code-
- To create a Bar chart we first define an instance of the DefaultCategoryDataset object.
- The setValue() method of this object is used to set the data and name of the bars to be generated.
- The Bar chart is generated by using createBarChart3D method of CharFactory. This generates 3D version of the Bar chart based on the data set value passed as arguments.
- Appearances of the Bar are set by using the various self described methods. And finally the Bar chart is generated using writeChartAsPNG() method.
