Stacked Bar Chart using JFreeChart Posted on: August 30, 2008 at 12:00 AM
This Example shows you how to create a 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.
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(800, 500));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
DefaultCategoryDataset result = new DefaultCategoryDataset();
private LegendItemCollection createLegendItems() {
LegendItemCollection result = new LegendItemCollection();
LegendItem item1 = new LegendItem("US", "US", "US", "US", new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, new Color(16, 89, 172), 0.0f, 0.0f,
new Color(201, 201, 244)));
LegendItem item2 = new LegendItem("Europe", "Europe", "Europe", "Europe", new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, new Color(10, 144, 40), 0.0f, 0.0f, new Color(160, 240, 180)));
LegendItem item3 =
new LegendItem("Asia", "Asia", "Asia", "Asia", new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, new Color(255, 35, 35), 0.0f, 0.0f, new Color(255, 180, 180)));
public static void main(final String[] args) { final StackedBarChart1 demo = new StackedBarChart1("Stacked Bar Chart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Ask Questions? Discuss: Stacked Bar Chart using JFreeChart View All Comments
Post your Comment