JFreeChart is an open-source Java based library which allows the creation of complex charts in a simple way. Supported chart types include:
-
X-Y charts (line, spline and scatter);
-
Pie charts;
-
Gantt charts;
-
Bar charts (horizontal and vertical, stacked and independent);
-
Single valued (thermometer, compass, speedometer).
I am going to explain how to use the chart library and how to create a bar chart with a custom color. To create a chart with JFreeChart library, first you have to make a Dataset object. The chart is generated from this data collection. Next you have to instate the class DefaultCategoryDataset, which is passed to the chart object. In the code below as a data source is used ArrayList, which may come from data base.
// load the data
double[] data = new double[jobTitles.size()];
for (int i = 0; i < jobTitles.size(); i++) {
data[i] = jobTitles.get(i).getCount();
}
// create the data set
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < data.length; i++) {
dataset.setValue(data[i], "Profit1", jobTitles.get(i).getTitle());
}
The next step is to do an object from chart type. The library uses a class named JFreeChart to display the chart. This chart object is an instance, which comes from the method createBarChart of the static class ChartFactory. The method takes as a parameter the formed collection (dataset). There are many properties, which you can pass to the constructor, such as labels, orientation, etc. It is possible to change colors, size, scale and many other characteristics.
You have ready object now and the next step is to send the chart to the web page. For this purpose, the library includes a class named ChartUtilities that provides several methods for saving charts to files or writing them out to streams in JPEG or PNG format. The methods from this class can be used for creating JPEG images for static web pages. The goal, however, is not to save the chart in a hard drive; the goal is to pass the graphic to the application. To do that, you can use the servlet technology and display the dynamic data stream in the jsp pages.
Consequently the next step is to generate BufferedImage from your chart object and to transfer this stream through the session. You should also write the image in the http response writer.
// create the chart
final JFreeChart chart = ChartFactory.createBarChart("Allocation of Duties",
"Values", dataset, PlotOrientation.VERTICAL, true, true, true);
// set custom color
GradientPaint gradientpaint0 = new GradientPaint(0.0F, 0.0F,
new Color(209, 228, 246), 0.0F, 0.0F, new Color(82, 141, 201));
BarRenderer r = (BarRenderer)chart.getCategoryPlot().getRenderer();
r.setSeriesPaint(0, gradientpaint0);
ChartRenderingInfo info = null;
HttpSession session = request.getSession();
try {
// create RenderingInfo object
response.setContentType("text.html");
info = new ChartRenderingInfo(new StandardEntityCollection());
BufferedImage chartImage = chart.createBufferedImage(640, 400, info);
session.setAttribute("chartImage", chartImage);
PrintWriter writer = new PrintWriter(response.getWriter());
ChartUtilities.writeImageMap(writer, "imageMap", info, false);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
Finally you should write the servlet. This is a Java object, which dynamically answers requests and built response to the client. Each servlet has two main methods - doGet(HttpServletRequest request, HttpServletResponse response) and doPost(HttpServletRequest request, HttpServletResponse response). You need to get the chart stream from the session in the servlet. The image is set to an attribute named chartImage in the session. You have to get http session instance from the request and then to retrieve the previously set attribute.
// Process the HTTP Get request
public void mdoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// get the chart from session
HttpSession session = request.getSession();
BufferedImage chartImage = (BufferedImage)session.getAttribute("chartImage");
// set the content type so the browser can see this as a picture
response.setContentType("image.png");
// send the picture
PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);
response.getOutputStream().write(encoder.pngEncode());
}
// Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
The result is the graphic below:
JFreeChart is a library for displaying charts and managing their properties with a big palette of options. For more information go to the project official site.
Teaser:
There are a lot of tutorials and posts explaining how to display a chart in a web application and which libraries to use. I am going to show the process from receiving the http request from the client to sending the response back from the server. I will share my real experience with JFreeChart and the servlet technology for creating and displaying dynamic charts in Java based web application.
Comments
Thanks alot - your answer
Hey, Ive been using
Sir can u provide me code of
Can u plz provide some of the