How to combine line chart and bar chart in JFreeChart



How to combine line chart and bar chart in JFreeChart

Line Chart - A line chart is a series of data points connected by a straight line segments.

Bar Chart - A bar chart is chart that represents the grouped data with rectangles. The height or length of bar is propositional to the value that represents the bar.

In this post, I will show you how to combine line chart and bar chart using the JFreeChart library.

 

JAR dependencies

Add the following jars to build path of your project, if you are using Eclipse.

  • jfreechart-1.0.19.jar
  • jcommon-1.0.23.jar

 

Sample data for Line chart and Bar chart

Consider the following sample traffic data of www.boraji.com site.

Total number of daily visitor

Date Number of Visitors              
2016-12-19 200
2016-12-20 150
2016-12-21 100
2016-12-22 210
2016-12-23 240
2016-12-24 195
2016-12-25 245

Total number of daily unique visitor

Date Number of UNIQUE Visitors
2016-12-19 150
2016-12-20 130
2016-12-21 95
2016-12-22 195
2016-12-23 200
2016-12-24 180
2016-12-25 230

 

Java program for combined line chart and bar chart

Here is an example of creating combined bar chart and line chart from the above sample data.

CombineBarAndLineChartExample.java

package com.boraji.tutorial.jfreechart;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * @author imssbora
 *
 */
public class CombineBarAndLineChartExample extends JFrame {

   private static final long serialVersionUID = 1L;

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

      // Create Category plot
      CategoryPlot plot = new CategoryPlot();

      // Add the first dataset and render as bar
      CategoryItemRenderer lineRenderer = new LineAndShapeRenderer();
      plot.setDataset(0, createDataset());
      plot.setRenderer(0, lineRenderer);

      // Add the second dataset and render as lines
      CategoryItemRenderer baRenderer = new BarRenderer();
      plot.setDataset(1, createDataset());
      plot.setRenderer(1, baRenderer);

      // Set Axis
      plot.setDomainAxis(new CategoryAxis("Time"));
      plot.setRangeAxis(new NumberAxis("Value"));

      JFreeChart chart = new JFreeChart(plot);
      chart.setTitle("Combined Bar And Line Chart | BORAJI.COM");

      ChartPanel panel = new ChartPanel(chart);
      setContentPane(panel);
   }

   private DefaultCategoryDataset createDataset() {

      // First Series
      String series1 = "Vistor";
      DefaultCategoryDataset dataset = new DefaultCategoryDataset();
      dataset.addValue(200, series1, "2016-12-19");
      dataset.addValue(150, series1, "2016-12-20");
      dataset.addValue(100, series1, "2016-12-21");
      dataset.addValue(210, series1, "2016-12-22");
      dataset.addValue(240, series1, "2016-12-23");
      dataset.addValue(195, series1, "2016-12-24");
      dataset.addValue(245, series1, "2016-12-25");

      // Second Series
      String series2 = "Unique Visitor";
      dataset.addValue(150, series2, "2016-12-19");
      dataset.addValue(130, series2, "2016-12-20");
      dataset.addValue(95, series2, "2016-12-21");
      dataset.addValue(195, series2, "2016-12-22");
      dataset.addValue(200, series2, "2016-12-23");
      dataset.addValue(180, series2, "2016-12-24");
      dataset.addValue(230, series2, "2016-12-25");

      return dataset;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> {
         CombineBarAndLineChartExample example = new CombineBarAndLineChartExample(
               "Line Chart and Bar chart Example");
         example.setSize(800, 400);
         example.setLocationRelativeTo(null);
         example.setVisible(true);
      });
   }
}

Output

JFreeChart - Bubble chart