Java Bar Chart Example Code Program

There are many Java libraries that are available open source to create charts in Java. The most prominent ones are JFreechart and jCharts. In this tutorial, we will describe how to create a clustered bar chart in Java using the following Java libraries (with full working Java code examples) :

  • JFreeChart
  • jChart
We will cover creating bar charts with jChart2D, jzy3d, jopenchart and jensoft libraries in subsequent blog posts.We would like to provide a comparitive output values for a fixed input across these charting libraries. The input data we will be using for this set of tutorials is provided below:


Rome
Cairo
Q1
34
45
Q2
22
12
Q3
56
98
Q4
2
15
The output chart that we would like to generate by using each of these open source charting APIs, should  resemble something as shown below:

Creating Bar Charts in Java
Sample Output - Creating Clustered Bar Charts in Java
All of the code that we will be providing to create the chart would be commented as per our standard practice, so that it will be easy for the readers to understand how the code works. Each of these charting APIs follow their own convention for generating charts; you may wish to jump to a specific chart library if you want to know how to generate a bar chart only with that library. So, here we go.

JFreeChart Bar Chart Example

In order to create a bar chart like the one shown above using JFreeChart library, the code is provided below: (code is provided as a step by step example )
 1 /* The java code below generates a bar chart using JFreeChart java API */
 2 import java.io.*;
 3 import org.jfree.data.category.DefaultCategoryDataset; /* We will use this dataset class to populate data for our bar chart */
 4 import org.jfree.chart.ChartFactory; /* used to create a chart object */
 5 import org.jfree.chart.plot.PlotOrientation;
 6 import org.jfree.chart.JFreeChart; 
 7 import org.jfree.chart.ChartUtilities; /* We will use this class to convert the chart to a PNG image file */
 8 public class BarChartExample {  
 9      public static void main(String[] args){
10         try {
11                
12                /* Step - 1: Define the data for the bar chart  */
13                DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();
14                my_bar_chart_dataset.addValue(34, "Q1", "Rome");
15                my_bar_chart_dataset.addValue(45, "Q1", "Cairo");
16                my_bar_chart_dataset.addValue(22, "Q2", "Rome");
17                my_bar_chart_dataset.addValue(12, "Q2", "Cairo");
18                my_bar_chart_dataset.addValue(56, "Q3", "Rome");
19                my_bar_chart_dataset.addValue(98, "Q3", "Cairo");
20                my_bar_chart_dataset.addValue(2, "Q4", "Rome");
21                my_bar_chart_dataset.addValue(15, "Q4", "Cairo");
22                
23                /* Step -2:Define the JFreeChart object to create bar chart */
24                JFreeChart BarChartObject=ChartFactory.createBarChart("CountryVsSales - Bar Chart","Country","Sales",my_bar_chart_dataset,PlotOrientation.VERTICAL,true,true,false);                
25                          
26                 /* Step -3: Write the output as PNG file with bar chart information */                
27                 int width=640; /* Width of the image */
28                 int height=480; /* Height of the image */                
29                 File BarChart=new File("output_chart.png");              
30                 ChartUtilities.saveChartAsPNG(BarChart,BarChartObject,width,height); 
31         }
32         catch (Exception i)
33         {
34             System.out.println(i);
35         }
36     }
37 }

jCharts Bar Chart Example

The same clustered bar chart can also be generated using jChart java library. The java code in this case is provided below

 1 /* The java code below generates a clustered bar chart using JCharts java API */
 2 import java.io.*;
 3 import org.jCharts.chartData.DataSeries;
 4 import org.jCharts.test.TestDataGenerator;
 5 import org.jCharts.properties.ClusteredBarChartProperties;
 6 import org.jCharts.chartData.AxisChartDataSet;
 7 import org.jCharts.properties.AxisProperties;
 8 import org.jCharts.properties.ChartProperties;
 9 import org.jCharts.properties.LegendProperties;
10 import org.jCharts.axisChart.AxisChart;
11 import org.jCharts.encoders.PNGEncoder;
12 import java.io.OutputStream;
13 import java.io.FileOutputStream;
14 import org.jCharts.types.ChartType;
15 import java.awt.Paint;
16 public class BarChartExample {  
17      public static void main(String[] args){
18         try {
19                
20                /* Step -1: Define the chart decorative parameters */
21                String[] xAxisLabels= { "Q1", "Q2", "Q3", "Q4" };
22                String xAxisTitle= "Quarter"; /* X - Axis label */
23                String yAxisTitle= "Sales Count"; /*Y-Axis label */
24                String title= "CountryVsSales - Bar Chart"; /* Chart title */
25                DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); /* Define the dataseries */
26                 
27                /* Step - 2: Define the data for the clustered bar chart */
28                double[][] data= new double[][]{ { 30,33,56,2 }, { 45,12,98,15} };
29                String[] legendLabels= { "Rome","Cairo" }; /* Define legend for bar chart */
30                Paint[] paints= TestDataGenerator.getRandomPaints(2);
31                ClusteredBarChartProperties clusteredBarChartProperties= new ClusteredBarChartProperties();
32                AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_CLUSTERED, clusteredBarChartProperties );
33                dataSeries.addIAxisPlotDataSet( axisChartDataSet );
34                /* Step -3 - Create the chart */
35               ChartProperties chartProperties= new ChartProperties(); /* Special chart properties, if any */
36               AxisProperties axis_Properties= new AxisProperties();
37               LegendProperties legend_Properties= new LegendProperties(); /* Dummy Axis and legend properties class */
38               AxisChart my_output_chart= new AxisChart( dataSeries, chartProperties, axis_Properties, legend_Properties, 640, 480 ); /* Create Chart object */
39               OutputStream output = new FileOutputStream("out.png"); /* Define a output stream */
40               /* Step -4: Write the chart output as PNG image file */
41               PNGEncoder.encode(my_output_chart,output); /* This class creates the output PNG file for the chart */
42         }
43         catch (Exception i)
44         {
45             System.out.println(i);
46         }
47     }
48 }
In both the cases, the final output is a PNG version of the chart which is provided below:

jChart - Bar Chart Example in Java
jChart - Bar Chart Example in Java

JFreeChart - Bar Chart Example Output
JFreeChart - Bar Chart Example Output

No comments:

Post a Comment