Stacked Bar Chart using JFreeChart

This Example shows you how to create a Stacked bar chart using JFreeChart.

Stacked Bar Chart using JFreeChart

Stacked Bar Chart using JFreeChart

     

This Example shows you how to create a Stacked bar chart using JFreeChart.
In the code given below we have extended class ApplicationFrame to create a frame and also pass a string value to the constructor of ApplicationFrame class by using super keyword that will be name of the created frame.

Some methods that are used in this code :
pack(): This method invokes the layout manager.

centerFrameOnScreen(): This method is used for the position of the frame in the middle of the screen.

setVisible(): This method is used for display frame on the screen.

addValue(): This method is used for add value of different category in DefaultCategoryDataset class object.

createStackedBarChart(): This method is used to create stacked bar chart for given values. It takes title, domain axis label, range axis label, dataset, Plot Orientation, legend, tool tips and urls as parameters.

setBackgroundPaint(): This method is used to set the paint used to fill the chart background.

addSubCategory():
This method is used for add sub category in the chart.

StackedBarChart2.java

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.awt.Rectangle;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.StandardGradientPaintTransformer;

public class StackedBarChart1 extends ApplicationFrame {

  StackedBarChart1(String titel) {
  super(titel);

  final CategoryDataset dataset = createDataset();
  final JFreeChart chart = createChart(dataset);
  final ChartPanel chartPanel = new ChartPanel(chart);
  chartPanel.setPreferredSize(
  
new java.awt.Dimension(800500));
  setContentPane(chartPanel);
  }

  private CategoryDataset createDataset() {
  DefaultCategoryDataset result = new DefaultCategoryDataset();

  result.addValue(20.3"Product 1 (US)""Jan 08");
  result.addValue(27.2"Product 1 (US)""Feb 08");
  result.addValue(19.7"Product 1 (US)""Mar 08");
  result.addValue(20.7"Product 1 (US)""Apr 08");
  result.addValue(19.4"Product 1 (Europe)""Jan 08");
  result.addValue(10.9"Product 1 (Europe)""Feb 08");
  result.addValue(18.4"Product 1 (Europe)""Mar 08");
  result.addValue(12.4"Product 1 (Europe)""Apr 08");
  result.addValue(16.5"Product 1 (Asia)""Jan 08");
  result.addValue(15.9"Product 1 (Asia)""Feb 08");
  result.addValue(16.1"Product 1 (Asia)""Mar 08");
  result.addValue(14.4"Product 1 (Asia)""Apr 08");


  result.addValue(23.3"Product 2 (US)""Jan 08");
  result.addValue(16.2"Product 2 (US)""Feb 08");
  result.addValue(28.7"Product 2 (US)""Mar 08");
  result.addValue(22.7"Product 2 (US)""Apr 08");
  result.addValue(12.7"Product 2 (Europe)""Jan 08");
  result.addValue(17.9"Product 2 (Europe)""Feb 08");
  result.addValue(12.6"Product 2 (Europe)""Mar 08");
  result.addValue(14.6"Product 2 (Europe)""Mar 08");
  result.addValue(15.4"Product 2 (Asia)""Jan 08");
  result.addValue(21.0"Product 2 (Asia)""Feb 08");
  result.addValue(11.1"Product 2 (Asia)""Mar 08");
  result.addValue(16.1"Product 2 (Asia)""Apr 08");


  result.addValue(11.9"Product 3 (US)""Jan 08");
  result.addValue(31.0"Product 3 (US)""Feb 08");
  result.addValue(22.7"Product 3 (US)""Mar 08");
  result.addValue(18.7"Product 3 (US)""Apr 08");
  result.addValue(15.3"Product 3 (Europe)""Jan 08");
  result.addValue(14.4"Product 3 (Europe)""Feb 08");
  result.addValue(25.3"Product 3 (Europe)""Mar 08");
  result.addValue(16.3"Product 3 (Europe)""Apr 08");
  result.addValue(23.9"Product 3 (Asia)""Jan 08");
  result.addValue(19.0"Product 3 (Asia)""Feb 08");
  result.addValue(10.1"Product 3 (Asia)""Mar 08");
  result.addValue(18.1"Product 3 (Asia)""Apr 08");


  return result;
  }

  private JFreeChart createChart(final CategoryDataset dataset) {

  final JFreeChart chart = 
   ChartFactory.createStackedBarChart
(
 "Stacked Bar Chart""Category""Value", dataset,
  PlotOrientation.VERTICAL, true, true, false);

  chart.setBackgroundPaint(new Color(249231236));

  GroupedStackedBarRenderer renderer = 
  new 
GroupedStackedBarRenderer();
  KeyToGroupMap map = new KeyToGroupMap("G1");

  map.mapKeyToGroup("Product 1 (US)""G1");
  map.mapKeyToGroup("Product 1 (Europe)""G1");
  map.mapKeyToGroup("Product 1 (Asia)""G1");


  map.mapKeyToGroup("Product 2 (US)""G2");
  map.mapKeyToGroup("Product 2 (Europe)""G2");
  map.mapKeyToGroup("Product 2 (Asia)""G2");


  map.mapKeyToGroup("Product 3 (US)""G3");
  map.mapKeyToGroup("Product 3 (Europe)""G3");
  map.mapKeyToGroup("Product 3 (Asia)""G3");

  renderer.setSeriesToGroupMap(map);


  renderer.setItemMargin(0.0);
  Paint p1 = new GradientPaint(
  0.0f0.0fnew Color(1689172)0.0f0.0fnew 
   
Color(201201244));
  renderer.setSeriesPaint(0, p1);
  renderer.setSeriesPaint(3, p1);
  renderer.setSeriesPaint(6, p1);

  Paint p2 = new GradientPaint(
  0.0f0.0fnew Color(1014440)0.0f0.0fnew 
    Color
(160240180));
  renderer.setSeriesPaint(1, p2);
  renderer.setSeriesPaint(4, p2);
  renderer.setSeriesPaint(7, p2);

  Paint p3 = new GradientPaint(
  0.0f0.0fnew Color(2553535)0.0f0.0fnew 
    Color
(255180180));
  renderer.setSeriesPaint(2, p3);
  renderer.setSeriesPaint(5, p3);
  renderer.setSeriesPaint(8, p3);


  renderer.setGradientPaintTransformer(
  new StandardGradientPaintTransformer
  (
GradientPaintTransformType.HORIZONTAL));

  SubCategoryAxis domainAxis = 
  new 
SubCategoryAxis("Product / Month");
  domainAxis.setCategoryMargin(0.05);
  domainAxis.addSubCategory("Product 1");
  domainAxis.addSubCategory("Product 2");
  domainAxis.addSubCategory("Product 3");

  CategoryPlot plot = (CategoryPlotchart.getPlot();
  plot.setDomainAxis(domainAxis);
  plot.setRenderer(renderer);
  plot.setFixedLegendItems(createLegendItems());
  return chart;
  }

  private LegendItemCollection createLegendItems() {
  LegendItemCollection result = new LegendItemCollection();
  LegendItem item1 = new LegendItem("US""US""US""US",
  new Rectangle(1010)new GradientPaint(0.0f0.0f,
  new Color(1689172)0.0f0.0f
   new 
Color(201201244)));
  LegendItem item2 = 
  
new LegendItem("Europe""Europe""Europe""Europe",
   new Rectangle(1010)new GradientPaint(0.0f0.0f,
   new Color(1014440)0.0f0.0f
 
new Color(160240180)));
  LegendItem item3 = 
   new 
LegendItem("Asia""Asia""Asia""Asia",
 new Rectangle(1010)new GradientPaint(0.0f0.0f,
  new Color(2553535)0.0f0.0f
  
new Color(255180180)));

  result.add(item1);
  result.add(item2);
  result.add(item3);

  return result;
  }

  public static void main(final String[] args) {
  final StackedBarChart1 demo = 
  
new StackedBarChart1("Stacked Bar Chart");
  demo.pack();
  RefineryUtilities.centerFrameOnScreen(demo);
  demo.setVisible(true);
  }
}

Output:



Download code