
// XYLineChartEx.java
// https://zetcode.com/java/jfreechart/
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* A single line in a XY Line Chart, 
   with data points stored as (x,y) values in a XYSeries.

   The graph background is white, the line is red, and 
   grid lines are visible on the x- and y-axes.

    Usage:
      > compile XYLineChartEx.java
      > run XYLineChartEx
*/

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;

import org.jfree.data.xy.*;


public class XYLineChartEx extends JFrame
{

  public XYLineChartEx()
  {
    super("Line Chart Example");

    XYSeriesCollection ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    add(cp);

    setSize(500,500);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  } // end of XYLineChartEx()


  private XYSeriesCollection createDataset()
  {
    XYSeries series = new XYSeries("2022");
    series.add(18, 567);
    series.add(20, 612);
    series.add(25, 800);
    series.add(30, 980);
    series.add(40, 1410);
    series.add(50, 2350);

    XYSeriesCollection ds = new XYSeriesCollection();
    ds.addSeries(series);

    return ds;
  } /* createDataset */



  private JFreeChart createChart(XYSeriesCollection ds)
  {
    JFreeChart chart = ChartFactory.createXYLineChart(
             "Average salary per Age", "Age", "Salary (€)", ds); 

    XYPlot plot = chart.getXYPlot();

    // thicker red lines
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesPaint(0, Color.RED);
    renderer.setSeriesStroke(0, new BasicStroke(2.0f));
    plot.setRenderer(renderer);

    plot.setBackgroundPaint(Color.white);

    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.BLACK);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.BLACK);

    return chart;
  } /* createChart */


  // ---------------------------------------

  public static void main(String[] args)
  {   new XYLineChartEx();  }


}
