
// PieChartExample.java
// https://www.javatpoint.com/jfreechart-pie-chart
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* A 2D pie chart made up of 5 coloured segments, 
   stored in a PieDataset.


    Usage:
      > compile PieChartExample.java
      > run PieChartExample
*/

import javax.swing.*;
import java.text.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.labels.*;

import org.jfree.data.general.*;



public class PieChartExample extends JFrame
{

  public PieChartExample()
  {
    super("Pie Chart Example");

    PieDataset 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 PieChartExample()


  private PieDataset createDataset()
  {
    DefaultPieDataset<String> ds = new DefaultPieDataset<>();
    ds.setValue("80-100", 120);   // section key and value
    ds.setValue("60-79", 80);
    ds.setValue("40-59", 20);
    ds.setValue("20-39", 7);
    ds.setValue("0-19", 3);
    return ds;
  } /* createDataset */


  private JFreeChart createChart(PieDataset ds)
  {
    JFreeChart chart = ChartFactory.createPieChart(
                                   "Pie Chart Example", ds);
    PiePlot piePlot = (PiePlot)chart.getPlot();

    // Format labels
    /* uses {0} for the pie section key, 
            {1} for the section value, and 
            {2} for the percent section value for the entire pie
    */
    PieSectionLabelGenerator labelGen =
            new StandardPieSectionLabelGenerator("Marks {0} : {2}"); 
    piePlot.setLabelGenerator(labelGen);
    
    // no shadows around pie
    piePlot.setShadowXOffset(0);
	piePlot.setShadowYOffset(0);

    return chart;
  } /* createChart */



  // ----------------------------------------

  public static void main(String[] args)
  {  new PieChartExample();  } 


}  // end of PieChartExample class