JFreeChart-Bar Chart

JFreeChart Bar Chart with Tutorial, Basic Terminology, What is Jfree Chart, Jfreechart Installation, Jfreechart class, Jfreechart's key classes, ChartUtilities class, ChartFrame class, PiePlot3D class, NumberAxis Class, XYSeriesCollection Class, TimeSeries Class, TimeSeriesCollection, PiePlot class etc.

JFreeChart-Bar Chart

JFreeChart-Bar Chart

A bar chart is a chart that represents the grouped data with rectangles. The height or length of the bar is propositional to the value that represents the bar.

Bar Chart example

Let us consider a sample input data for our bar chart example.

Year Population in Million
USA INDIA CHINA
2005 10 15 20
2010 15 20 25
2015 20 25 30

The following codes create a bar chart from the above sample data:

BarChartExample.java

  1. import javax.swing.JFrame;  
  2. import javax.swing.SwingUtilities;  
  3.   
  4. import org.jfree.chart.ChartFactory;  
  5. import org.jfree.chart.ChartPanel;  
  6. import org.jfree.chart.JFreeChart;  
  7. import org.jfree.chart.plot.PlotOrientation;  
  8. import org.jfree.data.category.CategoryDataset;  
  9. import org.jfree.data.category.DefaultCategoryDataset;  
  10.   
  11. public class BarChartExample extends JFrame {  
  12.   
  13.   private static final long serialVersionUID = 1L;  
  14.   
  15.   public BarChartExample(String appTitle) {  
  16.     super(appTitle);  
  17.   
  18.     // Create Dataset  
  19.     CategoryDataset dataset = createDataset();  
  20.       
  21.     //Create chart  
  22.     JFreeChart chart=ChartFactory.createBarChart(  
  23.         "Bar Chart Example"//Chart Title  
  24.         "Year"// Category axis  
  25.         "Population in Million"// Value axis  
  26.         dataset,  
  27.         PlotOrientation.VERTICAL,  
  28.         true,true,false  
  29.        );  
  30.   
  31.     ChartPanel panel=new ChartPanel(chart);  
  32.     setContentPane(panel);  
  33.   }  
  34.   
  35.   private CategoryDataset createDataset() {  
  36.     DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  37.   
  38.     // Population in 2005  
  39.     dataset.addValue(10"USA""2005");  
  40.     dataset.addValue(15"India""2005");  
  41.     dataset.addValue(20"China""2005");  
  42.   
  43.     // Population in 2010  
  44.     dataset.addValue(15"USA""2010");  
  45.     dataset.addValue(20"India""2010");  
  46.     dataset.addValue(25"China""2010");  
  47.   
  48.     // Population in 2015  
  49.     dataset.addValue(20"USA""2015");  
  50.     dataset.addValue(25"India""2015");  
  51.     dataset.addValue(30"China""2015");  
  52.   
  53.     return dataset;  
  54.   }  
  55.   
  56.   public static void main(String[] args) throws Exception {  
  57.     SwingUtilities.invokeAndWait(()->{  
  58.       BarChartExample example=new BarChartExample("Bar Chart Window");  
  59.       example.setSize(800400);  
  60.       example.setLocationRelativeTo(null);  
  61.       example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  62.       example.setVisible(true);  
  63.     });  
  64.   }  
  65.