Stacked Bar Chart in Java

Introduction

 
In this article, we are going to describe how to make our own Stacked Bar chart using the JFree Chart class in Java. I think all of you know about the use of graphs; a chart is a good way to represent a large amount of data.
 

Stacked Bar chart Definition 

 
Stacked Bar chart 
 
This is an image of a sample Bar Chart representation of sales by quarter. So a stacked bar chart displays the results of multiple queries stacked on top of one another, either vertically or horizontally. A stacked bar chart is an effective way to present the absolute values of data points represented by the segments of each bar, as well as the total value represented by data points from each series stacked in a bar.
 
Example
 
This example shows you how to create a stacked bar chart using JFreeChart. The bar chart will represent scores of two teams. In the code given below we have extended the class ApplicationFrame to create a frame and also pass a string value to the constructor of the ApplicationFrame class by using the super keyword that will be the name of the created frame.
 
In the following example, each bar of the stacked bar graph is divided into two categories: girls and boys.
 
Each of the three bars represents a whole. That is: about 38 students like Basketball, out of which 16 are girls.
 
Stacked Bar Graph1.jpg 
 
Some methods that are used in the following Example :
  • 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.
  • createCategoryDataset(): This method is used to create the instance of CategoryDataset Interface and that contains a copy of the data in an array.
  • createStackedBarChart(): This method is used to create a stacked bar chart for given values. It takes title, domain axis label, range axis label, dataset, Plot Orientation, legend, tooltips, and URLs as parameters.
  • setBackgroundPaint(): This method is used to set the paint used to fill the chart background.

Code

  1. import java.awt.Color;  
  2. import org.jfree.chart.ChartFactory;  
  3. import org.jfree.chart.ChartPanel;  
  4. import org.jfree.chart.JFreeChart;  
  5. import org.jfree.chart.plot.CategoryPlot;  
  6. import org.jfree.chart.plot.PlotOrientation;  
  7. import org.jfree.data.category.CategoryDataset;  
  8. import org.jfree.data.general.DatasetUtilities;  
  9. import org.jfree.ui.ApplicationFrame;  
  10. import org.jfree.ui.RefineryUtilities;  
  11. public class StackedBarChart extends ApplicationFrame {  
  12.     public StackedBarChart(String titel) {  
  13.         super(titel);  
  14.         final CategoryDataset dataset = createDataset();  
  15.         final JFreeChart chart = createChart(dataset);  
  16.         final ChartPanel chartPanel = new ChartPanel(chart);  
  17.         chartPanel.setPreferredSize(new java.awt.Dimension(500350));  
  18.         setContentPane(chartPanel);  
  19.     }  
  20.     private CategoryDataset createDataset() {  
  21.         double[][] data = new double[][] {  
  22.             {  
  23.                 210,  
  24.                 300,  
  25.                 320,  
  26.                 265,  
  27.                 299,  
  28.                 200  
  29.             }, {  
  30.                 200,  
  31.                 304,  
  32.                 201,  
  33.                 201,  
  34.                 340,  
  35.                 300  
  36.             },  
  37.         };  
  38.         return DatasetUtilities.createCategoryDataset("Team ""Match", data);  
  39.     }  
  40.     private JFreeChart createChart(final CategoryDataset dataset) {  
  41.         final JFreeChart chart = ChartFactory.createStackedBarChart("Stacked Bar Chart """,  
  42.             "Score", dataset, PlotOrientation.VERTICAL, truetruefalse);  
  43.         chart.setBackgroundPaint(new Color(249231236));  
  44.         CategoryPlot plot = chart.getCategoryPlot();  
  45.         plot.getRenderer().setSeriesPaint(0new Color(12800));  
  46.         plot.getRenderer().setSeriesPaint(1new Color(00255));  
  47.         return chart;  
  48.     }  
  49.     public static void main(final String[] args) {  
  50.         final StackedBarChart demo =  
  51.             new StackedBarChart("Stacked Bar Chart");  
  52.         demo.pack();  
  53.         RefineryUtilities.centerFrameOnScreen(demo);  
  54.         demo.setVisible(true);  
  55.     }  
  56. }  
Output
 
StackedBarChart.gif 
 
Resources


Similar Articles