KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > StackedBarChartDemo3


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -------------------------
23  * StackedBarChartDemo3.java
24  * -------------------------
25  * (C) Copyright 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: StackedBarChartDemo3.java,v 1.2 2003/11/28 10:57:33 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 27-Nov-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import org.jfree.chart.ChartFactory;
41 import org.jfree.chart.ChartPanel;
42 import org.jfree.chart.JFreeChart;
43 import org.jfree.chart.axis.ValueAxis;
44 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
45 import org.jfree.chart.plot.CategoryPlot;
46 import org.jfree.chart.plot.PlotOrientation;
47 import org.jfree.chart.renderer.CategoryItemRenderer;
48 import org.jfree.data.CategoryDataset;
49 import org.jfree.ui.ApplicationFrame;
50 import org.jfree.ui.RefineryUtilities;
51
52 /**
53  * A simple demonstration application showing how to create a stacked bar chart
54  * using data from a {@link CategoryDataset}.
55  *
56  * @author David Gilbert
57  */

58 public class StackedBarChartDemo3 extends ApplicationFrame {
59
60     /**
61      * Creates a new demo.
62      *
63      * @param title the frame title.
64      */

65     public StackedBarChartDemo3(String JavaDoc title) {
66
67         super(title);
68         CategoryDataset dataset = createDataset();
69         JFreeChart chart = createChart(dataset);
70         ChartPanel chartPanel = new ChartPanel(chart);
71         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
72         setContentPane(chartPanel);
73
74     }
75     
76     /**
77      * Creates a sample dataset.
78      *
79      * @return a sample dataset.
80      */

81     private CategoryDataset createDataset() {
82         return DemoDatasetFactory.createCategoryDataset();
83     }
84     
85     /**
86      * Creates a sample chart.
87      *
88      * @param dataset the dataset for the chart.
89      *
90      * @return a sample chart.
91      */

92     private JFreeChart createChart(CategoryDataset dataset) {
93
94         JFreeChart chart = ChartFactory.createStackedBarChart(
95             "Stacked Bar Chart Demo 3", // chart title
96
"Category", // domain axis label
97
"Value", // range axis label
98
dataset, // data
99
PlotOrientation.VERTICAL, // the plot orientation
100
true, // legend
101
false, // tooltips
102
false // urls
103
);
104         CategoryPlot plot = chart.getCategoryPlot();
105         CategoryItemRenderer renderer = new ExtendedStackedBarRenderer();
106         renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
107         plot.setRenderer(renderer);
108         
109         ValueAxis rangeAxis = plot.getRangeAxis();
110         rangeAxis.setLowerMargin(0.15);
111         rangeAxis.setUpperMargin(0.15);
112         return chart;
113         
114     }
115
116     // ****************************************************************************
117
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
118
// * Please note that commercial support and documentation is available from: *
119
// * *
120
// * http://www.object-refinery.com/jfreechart/support.html *
121
// * *
122
// * This is not only a great service for developers, but is a VERY IMPORTANT *
123
// * source of funding for the JFreeChart project. Please support us so that *
124
// * we can continue developing free software. *
125
// ****************************************************************************
126

127     /**
128      * Starting point for the demonstration application.
129      *
130      * @param args ignored.
131      */

132     public static void main(String JavaDoc[] args) {
133
134         StackedBarChartDemo3 demo = new StackedBarChartDemo3("Stacked Bar Chart Demo 3");
135         demo.pack();
136         RefineryUtilities.centerFrameOnScreen(demo);
137         demo.setVisible(true);
138
139     }
140
141 }
142
Popular Tags