JFreeChart- Scatter Chart

JFreeChart Scatter Chart with Tutorial, Basic Terminology, What is Jfree Chart, Jfreechart Installation, Jfreechart class, Jfreechart's key classes, ChartUtilities class, ChartFrame class, PiePlot3D class, NumberAxis Class, XYSeriesCollection Class, TimeSeries Class, TimeSeriesCollection, PiePlot class etc.

JFreeChart- Scatter Chart

JFreeChart- Scatter Chart

A scatter chart or plot contains a set of data points plotted on horizontal and vertical axes. It displays data from an XYDataset.

Scatter Plot example:

Let us consider the following sample data for a scatter plot.

Boys (Series 1) Girls (Series 2)
Age Weight Age Weight
1 72.9 1 72.5
2 81.6 2 80.1
3 88.9 3 87.2
4 96 4 94.5
5 102.1 5 101.4
6 108.5 6 107.4
7 113.9 7 112.8
8 119.3 8 118.2
9 123.8 9 122.9
10 124.4 10 123.4

The following codes create a scatter plot from the above sample data:

ScatterPlotExample.java

  1. import java.awt.Color;  
  2. import javax.swing.JFrame;  
  3. import javax.swing.SwingUtilities;  
  4. import javax.swing.WindowConstants;  
  5. import org.jfree.chart.ChartFactory;  
  6. import org.jfree.chart.ChartPanel;  
  7. import org.jfree.chart.JFreeChart;  
  8. import org.jfree.chart.plot.XYPlot;  
  9. import org.jfree.data.xy.XYDataset;  
  10. import org.jfree.data.xy.XYSeries;  
  11. import org.jfree.data.xy.XYSeriesCollection;  
  12.   
  13. public class ScatterPlotExample extends JFrame {  
  14.   private static final long serialVersionUID = 6294689542092367723L;  
  15.   
  16.   public ScatterPlotExample(String title) {  
  17.     super(title);  
  18.   
  19.     // Create dataset  
  20.     XYDataset dataset = createDataset();  
  21.   
  22.     // Create chart  
  23.     JFreeChart chart = ChartFactory.createScatterPlot(  
  24.         "Boys VS Girls weight comparison chart",   
  25.         "X-Axis""Y-Axis", dataset);  
  26.   
  27.       
  28.     //Changes background color  
  29.     XYPlot plot = (XYPlot)chart.getPlot();  
  30.     plot.setBackgroundPaint(new Color(255,228,196));  
  31.       
  32.      
  33.     // Create Panel  
  34.     ChartPanel panel = new ChartPanel(chart);  
  35.     setContentPane(panel);  
  36.   }  
  37.   
  38.   private XYDataset createDataset() {  
  39.     XYSeriesCollection dataset = new XYSeriesCollection();  
  40.   
  41.     //Boys (Age,weight) series  
  42.     XYSeries series1 = new XYSeries("Boys");  
  43.     series1.add(172.9);  
  44.     series1.add(281.6);  
  45.     series1.add(388.9);  
  46.     series1.add(496);  
  47.     series1.add(5102.1);  
  48.     series1.add(6108.5);  
  49.     series1.add(7113.9);  
  50.     series1.add(8119.3);  
  51.     series1.add(9123.8);  
  52.     series1.add(10124.4);  
  53.   
  54.     dataset.addSeries(series1);  
  55.       
  56.    //Girls (Age,weight) series  
  57.     XYSeries series2 = new XYSeries("Girls");  
  58.     series2.add(172.5);  
  59.     series2.add(280.1);  
  60.     series2.add(387.2);  
  61.     series2.add(494.5);  
  62.     series2.add(5101.4);  
  63.     series2.add(6107.4);  
  64.     series2.add(7112.8);  
  65.     series2.add(8118.2);  
  66.     series2.add(9122.9);  
  67.     series2.add(10123.4);  
  68.   
  69.     dataset.addSeries(series2);  
  70.   
  71.     return dataset;  
  72.   }  
  73.   
  74.   public static void main(String[] args) {  
  75.     SwingUtilities.invokeLater(() -> {  
  76.       ScatterPlotExample example = new ScatterPlotExample("Scatter Chart Example");  
  77.       example.setSize(800400);  
  78.       example.setLocationRelativeTo(null);  
  79.       example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  80.       example.setVisible(true);  
  81.     });  
  82.   }  
  83. }