Using JfreeChart To Plot Charts In Java

Using JfreeChart To Plot Charts In Java

Using JfreeChart To Plot Charts In Java

Using JfreeChart To Plot Charts In Java is a skill every Java Programmer should have. Jfreechart is a free 100% library that helps Java Developer display Graphs on their applications, it is open source and distributed under the GNU General Public licence (They permit the of proprietary applications). Jfreechart can help you draw and display a vast range of chart types i.e Pie Chart, Line Chart, Time Series Chart etc. The Chart can also be exported to an image file i.e. png, jpg or a Vector Graphics file formats (pdf, eps ,svg).

A little implementation of Jfreechat will give you some understanding on how the API works. First of all download the Jfreechart API from here Jfreechart API Download

After successful download, check the lib folder and make sure you import the following jar files into your Netbeans Library;

  • iText-2.1.5.jar
  • jcommon-1.0.18.jar
  • jfreechart-1.0.15.jar
  • jfreechart-1.0.15-experimental.jar
  • jfreechart-1.0.15-swt.jar
  • junit.jar
  • servlet.jar
  • swtgraphics2d.jar

After successfully adding the above jar files to your library, you can proceed for proper implementation.

In using Jfreechart the user has to create DefaultCategoryDataset Reference variable and add values into the dataSet Reference variable as in:

DefaultCategoryDataset dataSet =new DefaultCategoryDataset()
dataset.addValue(10.21, "Graph", 1);

the dataSet method addValue adds a value that will be plotted on the graph, usually this can be iterated using for loop with values added on the fly

The method addValue contains 3 parameters, the first parameter is the data on the Y axis, The second parameter is the plot ID ad the third parameter is the data on the X axis (represented as a String).

private BufferedImage setChart() {
JFreeChart chart = ChartFactory.createLineChart(
"Graph Title",
"X-axis label",
"Y-axis label",
CategoryPlot plot = (CategoryPlot) chart.getPlot();
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
//set the size of the charts
BufferedImage image = chart.createBufferedImage(270, 200);
return image;

After all data has been added to the dataSet objects, the system can began to plot the Graph, the dataset object will be passed to a method above as one of the parameters (the setChart method above returns a bufferedImage object Which can be attached to jlabel to be displayed as an Image).

The setChart method above uses the ChartFactory static class to create a jFreeChart Object, the creatLine method has parameters that set the chart tile, x-axis label, y-axis label, inserts dataset etc. After the chart object is created it is then design, transformed into a BufferedImage and returned to the calling method.

to attach the bufferedImage to a jLabel, the user only has to implement the following code;

jLabel10.setIcon(new ImageIcon(setChart()));

Thank you for reading this post (Using JfreeChart To Plot Charts In Java), for questions post a comment below