
// XYPlotEx.java
// Andrew Davison, ad@coe.psu.ac.th, July 2021

/* Two lines in a XY Line Chart, 
   with data points stored as (x,y) values in a XYSeries.

   The data for each lines is read from the files
   men.txt and women.txt.

   A copy of the chart is saved as a PNG file.

    Usage:
      > compile XYPlotEx.java
      > run XYPlotEx
*/

import java.io.*;
import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;

import org.jfree.data.xy.*;


public class XYPlotEx extends JFrame
{
  private static final int WIDTH = 800;
  private static final int HEIGHT = 400;


  public XYPlotEx()
  {
    super("XYPlot Example");

    XYSeriesCollection ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    // pack();
    setSize(WIDTH, HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

    pause(1000);  // to avoid bugs in rendering
    saveChartPic(chart, "xyPlot.png");
  }  // end of XYPlotEx()



  private XYSeriesCollection createDataset()
  {
    XYSeriesCollection ds = new XYSeriesCollection();

    // Men (Age,weight) series
    XYSeries series1 = readXYData("men.txt", "Men");
    ds.addSeries(series1);

    //Women (Age,weight) series
    XYSeries series2 = readXYData("women.txt", "Women");
    ds.addSeries(series2);

    return ds;
  } /* createDataset */


  private XYSeries readXYData(String fnm, String title)
  {
    System.out.println("Reading chart data for \"" + title +
                                           "\" from " + fnm);

    XYSeries series = new XYSeries(title);
    try { BufferedReader br = new BufferedReader(
                                   new FileReader(new File(fnm)));
      String line;
      String[] toks;
      while ((line = br.readLine()) != null) {
        toks = line.split("\\s+");
        series.add(Integer.parseInt(toks[0]),
                   Double.parseDouble(toks[1]) );
      }
    }
    catch (IOException e) {
      System.out.println("Error: " + e);
    }
    return series;
  }  // end of readXYData()



  private JFreeChart createChart(XYSeriesCollection ds)
  {
    JFreeChart chart = ChartFactory.createScatterPlot(
                           "Boys vs. Girls weight Comparison",
                           "Age", "Weight", ds);

    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(new Color(255,228,196));

    return chart;
  } /* createChart */


  private void saveChartPic(JFreeChart chart, String fnm)
  {
    try {
      ChartUtils.saveChartAsPNG( new File(fnm), chart, WIDTH, HEIGHT);
      System.out.println("Saved chart to " + fnm);
    }
    catch (IOException e) {
      System.out.println("Could not save chart to " + fnm);
    }
  }  // end of saveChartPic()


  private void pause(int ms)
  {
    try {
      Thread.sleep(ms);
    }
    catch(InterruptedException ex) { }
  }  // end of pause()


  // -----------------------------------

  public static void main(String[] args)
  {  new XYPlotEx();  } 


}  // end of XYPlotEx class