
// StackedAreaChartDemo.java
// http://www.java2s.com/Code/Java/Chart/JFreeChartStackedAreaChartDemo.htm
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* A stacked area chart made up of three areas, filled with
   partially transparenct color.

   Each line is made from 8 data points.

    Usage:
      > compile StackedAreaChartDemo.java
      > run StackedAreaChartDemo
*/

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 StackedAreaChartDemo extends JFrame
{

  public StackedAreaChartDemo()
  {
    super("Stacked 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);
  } // StackedAreaChartDemo()


  public CategoryDataset createDataset()
  {
    double[][] data = new double[][] {   // three lines of y- values
      // drawn in increasing order with first line touching the x-axis
      {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 */



  public JFreeChart createChart(CategoryDataset ds)
  {
    JFreeChart chart = ChartFactory.createStackedAreaChart(
                  "Stacked Area Chart", "Category","Value", ds);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    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(String[] args)
  {  new StackedAreaChartDemo();  } 


}  // StackedAreaChartDemo class
