
// BarChartEx2.java
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

// http://www.java2s.com/Code/Java/Chart/JFreeChartHorizontalBarChartDemo2.htm

/*  Three kinds of data, separated into eight groups.
    Drawn as a horizontal Bar Chart.
  
    The bars are drawn in solid colors, without highlighting.

    Usage:
      > compile BarChartEx2.java
      > run BarChartEx2

*/

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.*;

import org.jfree.data.category.*;

import org.jfree.data.general.DatasetUtils; // for building data sets



public class BarChartEx2 extends JFrame
{

  public BarChartEx2()
  {
    super("Bar Chart Example 2");

    CategoryDataset ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    setSize(500, 800);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // end of BarChartEx2()



  private CategoryDataset createDataset()
  {
    double[][] data = new double[][] {
      {1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0},
      {54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0},
      {41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0}
    };
    // eight groups (reading down), made up of three pieces of data per group

    return DatasetUtils.createCategoryDataset(
                                 "Series ", "Factor ", data);
  } /* createDataset */



  private JFreeChart createChart(CategoryDataset ds)
  {
    JFreeChart chart = ChartFactory.createBarChart(
      "Bar Chart Example 2", "Category", "Score (%)", ds,                
      PlotOrientation.HORIZONTAL, true, false, false);
                             // legend, tooltips, URLs

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);  // place x-axis along bottom

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setRange(0.0, 100.0);

    // solid color bars
    ((BarRenderer) plot.getRenderer()).
                     setBarPainter( new StandardBarPainter());

    return chart;
  } /* createChart */


  // -----------------------------------

  public static void main(final String[] args)
  {   new BarChartEx2();  } 


}  // end of BarChartEx2 class