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
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.
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, tool tips and urls as parameters.
- setBackgroundPaint(): This method is used to set the paint used to fill the chart background.
Code
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class StackedBarChart extends ApplicationFrame
{
public StackedBarChart(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(500, 350));
setContentPane(chartPanel);
}
private CategoryDataset createDataset()
{
double[][] data = new double[][]
{
{210, 300, 320, 265, 299, 200},
{200, 304, 201, 201, 340, 300},
};
return DatasetUtilities.createCategoryDataset(
"Team "
, "Match", data);
}
private JFreeChart createChart(final CategoryDataset dataset)
{
final JFreeChart chart = ChartFactory.createStackedBarChart("Stacked Bar Chart ", "",
"Score",dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new Color(249, 231, 236));
CategoryPlot plot = chart.getCategoryPlot();
plot.getRenderer().setSeriesPaint(0, new Color(128, 0, 0));
plot.getRenderer().setSeriesPaint(1, new Color(0, 0, 255));
return chart;
}
public static void main(final String[] args)
{
final StackedBarChart demo =
new
StackedBarChart("Stacked Bar Chart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Output
Resources
Bar Chart in WPF
Charting on Fire with Visifire Charting Library for WP7
Tip: How to change the color of Chart Bars in Crystal Reports?
Bar Chart in Silverlight
Use the Force - Creating Salesforce Apps with RSSBus and Syncfusion