
// AreaChartDemo.java
// http://www.java2s.com/Code/Java/Chart/JFreeChartAreaChartDemo.htm
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* An area chart made up of three areas, filled with
   partially transparenct color.

   Each line is made from 8 data points.

    Usage:
      > compile AreaChartDemo.java
      > run AreaChartDemo
*/


import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;

import org.jfree.data.category.*;
import org.jfree.data.general.*;


public class AreaChartDemo extends JFrame
{

  public AreaChartDemo()
  {
    super("Area Chart Demo");

    CategoryDataset ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    // pack();
    setSize(800, 500);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // AreaChartDemo()


  private CategoryDataset createDataset()
  {
    double[][] data = new double[][] {    // three lines of y- values
      {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0},
      {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0},
      {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0}
    };
    // eight groups per line

    return DatasetUtils.createCategoryDataset(
                          "Series ", "Type ", data );
          // labels for each line, labels for each group, data
          // e.g. "Series 1", "Type 8"
  } /* createDataset */



  private JFreeChart createChart(CategoryDataset ds)
  {
    JFreeChart chart = ChartFactory.createAreaChart(
           "Area Chart", "Category", "Value", ds);

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setForegroundAlpha(0.5f);

    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.white);

    // have areas start at origin, and extend to end of x-axis
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                      // only slow integer ticks on y-axis
    return chart;
  } /* createChart */


  // ----------------------------------------------

  public static void main(final String[] args)
  {  new AreaChartDemo();  }

} // AreaChartDemo
