
// BarChartEx1.java
// https://www.javatpoint.com/jfreechart-bar-chart
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* Three kinds of data, separated into three
   year. Drawn as a Bar Chart.

    Usage:
      > compile BarChartEx1.java
      > run BarChartEx1
*/


import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.data.category.*;


public class BarChartEx1 extends JFrame
{

  public BarChartEx1()
  {
    super("Bar Chart Example");

    CategoryDataset ds = createDataset();

    JFreeChart chart=ChartFactory.createBarChart(
          "Bar Chart Example",  "Year", "Pop. in Million", ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    setSize(800,400);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // end of BarChartEx1()


  private CategoryDataset createDataset()
  /* three roy key values divided into three years
     (column keys)
  */
  {
    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    // Year 2005
    ds.addValue(10, "USA", "2005");   
    // value; row (line) key; column (category) key

    ds.addValue(15, "India", "2005");
    ds.addValue(20, "China", "2005");

    // Year 2010
    ds.addValue(15, "USA", "2010");
    ds.addValue(20, "India", "2010");
    ds.addValue(25, "China", "2010");

    // Year 2015
    ds.addValue(20, "USA", "2015");
    ds.addValue(25, "India", "2015");
    ds.addValue(30, "China", "2015");

    return ds;
  } /* createDataset */


  // ---------------------------------------------

  public static void main(String[] args)
  {   new BarChartEx1();  }

}  // end of BarChartEx1 class
