JFreeChart Scatter Chart

Chapter: JFreeChart Last Updated: 20-10-2017 07:13:31 UTC

Program:

            /* ............... START ............... */
                
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class JavaJFreeChartScatterChart extends JFrame {

	private static final long serialVersionUID = 6294689542092367723L;

	public JavaJFreeChartScatterChart(String title) {
		super(title);

		// Create dataset
		XYDataset dataset = createDataset();

		// Create chart
		JFreeChart chart = ChartFactory.
				createScatterPlot("Boys VS Girls weight "
						+ "comparison chart", "X-Axis", "Y-Axis",
				dataset);

		// Changes background color
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(new Color(255, 228, 196));

		// Create Panel
		ChartPanel panel = new ChartPanel(chart);
		setContentPane(panel);
	}

	private XYDataset createDataset() {
		XYSeriesCollection dataset = new XYSeriesCollection();

		// Boys (Age,weight) series
		XYSeries series1 = new XYSeries("Boys");
		series1.add(1, 72.9);
		series1.add(2, 81.6);
		series1.add(3, 88.9);
		series1.add(4, 96);
		series1.add(5, 102.1);
		series1.add(6, 108.5);
		series1.add(7, 113.9);
		series1.add(8, 119.3);
		series1.add(9, 123.8);
		series1.add(10, 124.4);

		dataset.addSeries(series1);

		// Girls (Age,weight) series
		XYSeries series2 = new XYSeries("Girls");
		series2.add(1, 72.5);
		series2.add(2, 80.1);
		series2.add(3, 87.2);
		series2.add(4, 94.5);
		series2.add(5, 101.4);
		series2.add(6, 107.4);
		series2.add(7, 112.8);
		series2.add(8, 118.2);
		series2.add(9, 122.9);
		series2.add(10, 123.4);

		dataset.addSeries(series2);

		return dataset;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			JavaJFreeChartScatterChart example = new JavaJFreeChartScatterChart("Scatter Chart Example | JAVASCAN.COM");
			example.setSize(800, 400);
			example.setLocationRelativeTo(null);
			example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			example.setVisible(true);
		});
	}
}
                /* ............... END ............... */
        

Output


JFreeChart Scatter Chart

Tags

JFreeChart Scatter Chart, Java, JFreeChart

Similar Programs Chapter Last Updated
JFreeChart XY Step Chart JFreeChart 10-06-2017
JFreeChart Wafer Map Chart JFreeChart 10-06-2017
JFreeChart XY Step Area Chart JFreeChart 10-06-2017
JFreeChart BarChart Color Customise JFreeChart 14-10-2016
JFreeChart Bar Chart 3D Example JFreeChart 05-08-2016
JFreeChart Bar Chart 3D Horizontal JFreeChart 05-08-2016
JFreeChart Area Chart Example JFreeChart 05-08-2016
JFreeChart Bubble Chart Example JFreeChart 03-08-2016
JFreeChart XY Chart Example JFreeChart 03-08-2016
JFreeChart Dynamic Data Demo Chart Example JFreeChart 08-07-2016
JFreeChart Background Paint Example JFreeChart 07-07-2016
JFreeChart Get Plot Example JFreeChart 07-07-2016
JFreeChart Background Color Example JFreeChart 06-07-2016
JFreeChart Subtitles Example JFreeChart 06-07-2016
JFreeChart Chart Title Example JFreeChart 06-07-2016
JFreeChart Chart Border Example JFreeChart 05-07-2016
JFreeChart Time Series Chart Example JFreeChart 23-06-2016
JFreeChart Pie Chart 3D Example JFreeChart 08-06-2016
JFreeChart Line Chart Example JFreeChart 04-06-2016
JFreeChart Bar Chart Example JFreeChart 22-09-2018
JFreeChart Pie Chart Example JFreeChart 04-06-2016

1