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.
Required Libraries
You need to download
Following jar must be in classpath
- jfreechart-1.5.0.jar
- 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