
// PolarChartExample.java
// https://www.javatips.net/blog/create-polar-chart-using-jfreechart
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/*  A polar chart showing three data series drawn around 
    the orgin. The origin is at the top, and the angles (in
    degrees) run clockwise.

    The data axis labels are drawn rotated.

    Usage:
      > compile PolarChartExample.java
      > run PolarChartExample
*/

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;

import org.jfree.data.xy.*;



public class PolarChartExample extends JFrame
{

  public PolarChartExample()
  {
    super("Polar Chart Example");

    XYSeriesCollection ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    // pack();
    setSize(700, 600);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // PolarChartExample()



  private XYSeriesCollection createDataset()
  // generate three random data series
  {
    XYSeriesCollection ds = new XYSeriesCollection();

    ds.addSeries( genData("Series 1", 77.0, 8.0));  // radius, angle step
    ds.addSeries( genData("Series 2", 52.0, 6.0));
    ds.addSeries( genData("Series 3", 32.0, 2.0));

    return ds;
  } /* createDataset */



  private static XYSeries genData(String name,
                        double radius, double step)
  // generate a random series of polar coordinates (angle,radius)
  {
    XYSeries series = new XYSeries(name);
    for (double angle = 0.0; angle < 365.0; angle += step) {
      double r = radius * (1.1 + Math.random());
      series.add(angle, r);
    }
    return series;
  } /* genData */



  private JFreeChart createChart(XYSeriesCollection ds)
  {
    JFreeChart chart = ChartFactory.createPolarChart(
               "Polar Chart Example", ds, true, false, false);
                                    // includes a legend, tooltips, URLs?

    PolarPlot plot = (PolarPlot)chart.getPlot();

    // rotate the radius axis labels, so easier to read
    ValueAxis axis = plot.getAxis();
    axis.setVerticalTickLabels(true);

    return chart;
  } /* createChart */


  // -----------------------------------------

  public static void main(String[] args)
  {   new PolarChartExample(); }

}  // PolarChartExample class