
// StatisticalBarChartDemo.java
// http://www.java2s.com/Code/Java/Chart/JFreeChartStatisticalBarChartDemo.htm
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* Three kinds of data, separated into four groups.
   Drawn as a Statistical Bar Chart, where each bar has a 
   mean value and a standard deviation line.

    Usage:
      > compile StatisticalBarChartDemo.java
      > run StatisticalBarChartDemo
*/

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.*;

import org.jfree.data.statistics.*;



public class StatisticalBarChartDemo extends JFrame
{

  public StatisticalBarChartDemo()
  {
    super("Statistical Bar Chart Demo");

    StatisticalCategoryDataset ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    // pack();
    setSize(800, 400);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // end of StatisticalBarChartDemo()



  private StatisticalCategoryDataset createDataset()
  {
    DefaultStatisticalCategoryDataset ds =
                    new DefaultStatisticalCategoryDataset();

    // mean, stdev, row key, column key
    ds.add(32.5, 17.9, "Series 1", "Type 1");  
    ds.add(27.8, 11.4, "Series 1", "Type 2");
    ds.add(29.3, 14.4, "Series 1", "Type 3");
    ds.add(37.9, 10.3, "Series 1", "Type 4");

    ds.add(22.9,  7.9, "Series 2", "Type 1");
    ds.add(21.8, 18.4, "Series 2", "Type 2");
    ds.add(19.3, 12.4, "Series 2", "Type 3");
    ds.add(30.3, 20.7, "Series 2", "Type 4");

    ds.add(12.5, 10.9, "Series 3", "Type 1");
    ds.add(24.8,  7.4, "Series 3", "Type 2");
    ds.add(19.3, 13.4, "Series 3", "Type 3");
    ds.add(17.1, 10.6, "Series 3", "Type 4");

    return ds;
  } /* createDataset */


  private JFreeChart createChart(StatisticalCategoryDataset ds)
  // build the plot, then the chart
  {
    CategoryAxis xAxis = new CategoryAxis("Type");
    xAxis.setLowerMargin(0.01);   // % space before first bar
    xAxis.setUpperMargin(0.01);   // % space after last bar
    xAxis.setCategoryMargin(0.2);  // % space between groups

    ValueAxis yAxis = new NumberAxis("Value");

    CategoryItemRenderer renderer = new StatisticalBarRenderer();

    CategoryPlot plot = new CategoryPlot(ds, xAxis, yAxis, renderer);

    return new JFreeChart("Statistical Bar Chart Demo", plot);
  }  // end of createChart()


  // ------------------------------------------

  public static void main(String[] args)
  {   new StatisticalBarChartDemo();  } 

}  // end of StatisticalBarChartDemo class
