Create Bubble Chart Using JFreeChart

Create Bubble Chart Using JFreeChart explains about creating a simple bubble chart using JFreechart API

A bubble chart or bubble graph is a type of chart that displays three dimensions of data. Each entity with its triplet (v1, v2, v3) of associated data is plotted as a disk that expresses two of the vi values through the disk's xy location and the third through its size. Bubble charts can facilitate the understanding of social, economical, medical, and other scientific relationships.

Reference -> http://en.wikipedia.org/wiki/Bubble_chart

How To Create Bubble 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 Bubble 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 (Bubble Chart)

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.MatrixSeriesCollection;
import org.jfree.data.xy.NormalizedMatrixSeries;

public class JFreeChartBubbleChartExample extends JFrame {

   
private static final long serialVersionUID = 1L;
   
private static final int SIZE = 10;
   
public JFreeChartBubbleChartExample(String applicationTitle) {
       
super(applicationTitle);
       
// Creates a sample dataset for bubble chart
       
final NormalizedMatrixSeries series = new NormalizedMatrixSeries("Grid 1", SIZE, SIZE);
       
for (int count = 0; count < SIZE; count++) {
           
final int i = (int) (Math.random() * SIZE);
           
final int j = (int) (Math.random() * SIZE);
           
final int k = (int) (Math.random() * SIZE);
            series.update
(i, j, k);
       
}
       
series.setScaleFactor(series.getItemCount());
        MatrixSeriesCollection dataSet =
new MatrixSeriesCollection(series);

       
// Based on the dataset we are creating BubbleChart
       
JFreeChart bubbleChart = ChartFactory.createBubbleChart("Population Details", "X", "Y", dataSet, PlotOrientation.VERTICAL, true, true,false);
        XYPlot plot = bubbleChart.getXYPlot
();
        plot.setForegroundAlpha
(0.6f);
        NumberAxis domainAxis =
(NumberAxis) plot.getDomainAxis();
        domainAxis.setLowerBound
(-0.4);
        NumberAxis rangeAxis =
(NumberAxis) plot.getRangeAxis();
        rangeAxis.setLowerBound
(-0.4);
       
// Adding chart into a chart panel
       
ChartPanel chartPanel = new ChartPanel(bubbleChart);
       
// settind default size
       
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
       
// add to contentPane
       
setContentPane(chartPanel);
   
}

   
public static void main(String[] args) {
       
JFreeChartBubbleChartExample chart = new JFreeChartBubbleChartExample("Population Usage Statistics");
        chart.pack
();
        chart.setVisible
(true);
   
}
}
Output

Create Bubble Chart Using JFreeChart









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