Create Scatter Chart Using JFreeChart

Create Scatter Chart Using JFreeChart explains about creating a simple scatter chart using JFreechart API

A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are color-coded, one additional variable can be displayed. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

Reference -> https://en.wikipedia.org/wiki/Scatter_plot

How To Create Scatter Chart Using JFreeChart library?

JFreeChart is a free and open source java chart library used for creating professional quality charts. JFreeChart is purely written in java language, we can very easily incorporate JFreeChart in our java standalone and web applications.

Referenece - > http://www.jfree.org/jfreechart/

Here I am showing an example about How to create  a simple Scatter Chart using JFreeChart.

Note

1) If you need pie chart example, You can follow Create Pie Chart Using JFreeChart

2) If you need line chart example, You can follow Create Line Chart Using JFreeChart

3) If you need bar chart example, You can follow Create Bar Chart Using JFreeChart

4) If you need polar chart example, You can follow Create Polar Chart Using JFreeChart

5) If you need bubble chart example, You can follow Create Bubble Chart Using JFreeChart

6) If you need wind chart example, You can follow Create Wind Chart Using JFreeChart

Required Libraries

You need to download

  1. JFreeChart

Following jar must be in classpath

  1. jfreechart-1.5.0.jar
  2. jcommon-1.0.24.jar

pom.xml

<dependency>
	<groupId>org.jfree</groupId>
	<artifactId>jfreechart</artifactId>
	<version>1.5.0</version>
</dependency>
<dependency>
	<groupId>org.jfree</groupId>
	<artifactId>jcommon</artifactId>
	<version>1.0.24</version>
</dependency>

We can create following professional quality charts by using jfreechart.

  • Single valued Charts such as compass, speedometer, thermometer
  • Line Chart
  • Pie Chart
  • Bar Chart
  • Bubble Chart
  • Wind Chart
  • Polar Chart

JFreeChart Example(Scatter Chart)

import java.awt.Color;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
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 JFreeCharScatterChartExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public JFreeCharScatterChartExample(String applicationTitle, String chartTitle) {
	super(applicationTitle);

	// based on the dataset we create the chart
	JFreeChart chart = ChartFactory.createScatterPlot(chartTitle, "X-Axis", "Y-Axis", createDataset(),
		PlotOrientation.VERTICAL, true, true, true);

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

	// Adding chart into a chart panel
	ChartPanel chartPanel = new ChartPanel(chart);

	// settind default size
	chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

	// add to contentPane
	setContentPane(chartPanel);
    }

    private XYDataset createDataset() {

	// create the dataset...
	final XYSeriesCollection dataset = new XYSeriesCollection();

	XYSeries chromeValues = new XYSeries("Chrome");
	chromeValues.add(1, 72.1);
	chromeValues.add(2, 80.3);
	chromeValues.add(3, 90.4);
	chromeValues.add(4, 100.1);
	chromeValues.add(5, 110.2);
	chromeValues.add(6, 112.3);
	chromeValues.add(7, 122.4);
	chromeValues.add(8, 132.2);
	chromeValues.add(9, 142.2);
	chromeValues.add(10, 152.2);

	dataset.addSeries(chromeValues);

	XYSeries firefoxValues = new XYSeries("Firefox");
	firefoxValues.add(1, 72.3);
	firefoxValues.add(2, 80.5);
	firefoxValues.add(3, 90.6);
	firefoxValues.add(4, 100.3);
	firefoxValues.add(5, 110.5);
	firefoxValues.add(6, 112.6);
	firefoxValues.add(7, 122.5);
	firefoxValues.add(8, 132.4);
	firefoxValues.add(9, 142.3);
	firefoxValues.add(10, 152.5);
	
	dataset.addSeries(firefoxValues);
	
	return dataset;

    }

    public static void main(String[] args) {
	JFreeCharScatterChartExample chart = new JFreeCharScatterChartExample("Browser Usage Statistics",
		"Which Browser are you using?");
	chart.pack();
	chart.setVisible(true);
    }
}

Output









Your email address will not be published. Required fields are marked *