
// ScatterPlotExample.java
// https://stackoverflow.com/questions/6665354/changing-the-shapes-of-points-in-scatter-plot/6669529#6669529
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* A random spread of NUM_COORDS (x,y) coordinates centered around 
   the origin. They are displayed in a ScatterPlot Chart, 
   with the data stored in a XYSeries.

   The graph's x- and y- axes are shown on the plot.

   Each coordinate is drawn as a red 'x' cross. 

    Usage:
      > compile ScatterPlotExample.java
      > run ScatterPlotExample
*/


import java.awt.*;
import java.util.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.util.ShapeUtils;

import org.jfree.data.xy.*;


public class ScatterPlotExample extends JFrame
{
  private static final int NUM_COORDS = 100;

  private Random rand = new Random();


  public ScatterPlotExample()
  {
    super("Scatter Plot Example");

    XYSeriesCollection 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);
  }  // end of ScatterPlotExample()


  private XYSeriesCollection createDataset()
  {
    XYSeries series = new XYSeries("Coordinates");
    for (int i = 0; i < NUM_COORDS; i++)
      series.add(rand.nextGaussian(), rand.nextGaussian());   // (x,y)

    XYSeriesCollection ds = new XYSeriesCollection();
    ds.addSeries(series);
    return ds;
  } /* createDataset */


  public JFreeChart createChart(XYSeriesCollection ds)
  {
    JFreeChart chart = ChartFactory.createScatterPlot(
                             "Scatter Plot Demo ", "X", "Y", ds);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setDomainCrosshairVisible(true);  // show x- and y- axes
    plot.setRangeCrosshairVisible(true);

    // change squares to red crosses
    XYItemRenderer renderer = plot.getRenderer();
    Shape cross = ShapeUtils.createDiagonalCross(3, 1);
    renderer.setSeriesShape(0, cross);
    renderer.setSeriesPaint(0, Color.red);

    return chart;
  } /* createChart */


  // -------------------------------------

  public static void main(String args[])
  {  new ScatterPlotExample();  }

}  // end of ScatterPlotExample class

