

// BubbleChartExample.java
// https://www.javascan.com/710/chart-bubble-chart-example
// Modified by Andrew Davison, ad@coe.psu.ac.th, Nov 2022

/* Six XYZ coordinates on a bubble chart, with the
   z- values drawn as radii of bubbles at the corresponding
   (x,y) positions on the graph.

    Usage:
      > compile BubbleChartExample.java
      > run BubbleChartExample
*/


import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;

import org.jfree.data.xy.*;


public class BubbleChartExample extends JFrame
{

  public BubbleChartExample()
  {
    super("Bubble Chart Example");

    XYZDataset ds = createDataset();
    JFreeChart chart = createChart(ds);

    ChartPanel cp = new ChartPanel(chart);
    setContentPane(cp);

    setSize(500, 800);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }  // end of BubbleChartExample()



  public static XYZDataset createDataset()
  {
    DefaultXYZDataset ds = new DefaultXYZDataset();

    // six (x,y,z) coordinates
    double[] xs = { 30, 40, 50, 60, 70, 80 };   // weight (x-axis)
    double[] ys = { 10, 20, 30, 40, 50, 60 };   // age    (y-axis)
    double[] zs = { 4, 5, 10, 8, 9, 6 };        // bubble size  (z-axis)

    double[][] coords = { xs, ys, zs };
    ds.addSeries("In Work", coords);   // label used to identify the bubbles

    return ds;
  } /* createDataset */


  private static JFreeChart createChart(XYZDataset ds)
  {
    JFreeChart chart = ChartFactory.createBubbleChart(
                "Age vs. Weight vs. Work", "Weight", "Age", ds);

    XYPlot xyplot = (XYPlot) chart.getPlot();

    xyplot.setForegroundAlpha(0.5F);

    XYItemRenderer rend = xyplot.getRenderer();
    rend.setSeriesPaint(0, Color.BLUE);    // blue bubbles

    NumberAxis xAxis = (NumberAxis) xyplot.getDomainAxis();
    xAxis.setLowerMargin(0.2);  // x-axis margins as a percentage of range
    xAxis.setUpperMargin(0.5);

    NumberAxis yAxis = (NumberAxis) xyplot.getRangeAxis();
    yAxis.setLowerBound(5);   // y-axis runs between 5-65
    yAxis.setUpperBound(65);

    return chart;
  } /* createChart */


  // -------------------------------------

  public static void main(String args[])
  {  new BubbleChartExample();  } 

}  // end of BubbleChartExample class

