charts painted1

JFreeChart Bar Chart Tutorial for Dynamic Java Web Applications

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.

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 ofthe 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.

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.

Want to learn more about our custom development capabilities? Talk with us.

51262107 29dc 47d3 8fdd ce19890d5e09
java 500x360

“java.lang.OutOfMemoryError: Java heap space” Unexpected Tomcat Crash – Solved

We started the usual things like upgrading the core components and making more logs that the Tomcat can output. After all these we got rows in the localhost log saying “java.lang.OutOfMemoryError: Java heap space” but with no other information like rows of code, etc. We continued to optimize the memory usage by flushing the input and output streams in the code and others. After that we received this log:

This was the first time when we had a log where there was a specific row number indicating the error. After looking at the code, we found that the row was in logic, where we assign a field from the database to a string variable. It was nothing unusual, so we decided to check the field in the database table. It was a “text” field in a PostgreSQL database that we access using JDBC driver. After a SELECT length(‘field’) command, we estimated that the biggest field data is about 1 MB! So, if we wanted to take the whole table that would be about 400 MB data fetched in a single click that invoked this method. After that we were able to finally reproduce the server crash, clicking several times on the button that invoked the method fetching this abnormally large amount of data. We had to revise the code so that we take only one field when needed. This solved the crashing issue.

The lesson is that we need to make sure we are not requesting too much data using the SQL statements. Even if we are getting a small amount of columns like in this case, we need to have in mind that one of them could contain a lot of information and make the system crash – especially with lots of concurrent users.