KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChartDemo.java
24  * -----------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: BarChartDemo.java,v 1.3 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 11-Jun-2002 : Version 1 (DG);
35  * 25-Jun-2002 : Removed redundant imports (DG);
36  * 09-Oct-2002 : Added frame centering (DG);
37  * 18-Nov-2002 : Changed from DefaultCategoryDataset to DefaultTableDataset (DG);
38  * 28-Oct-2003 : Changed to display gradient paint (DG);
39  * 10-Nov-2003 : Renamed BarChartDemo.java (DG);
40  *
41  */

42
43 package org.jfree.chart.demo;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.GradientPaint JavaDoc;
47
48 import org.jfree.chart.ChartFactory;
49 import org.jfree.chart.ChartPanel;
50 import org.jfree.chart.JFreeChart;
51 import org.jfree.chart.axis.NumberAxis;
52 import org.jfree.chart.plot.CategoryPlot;
53 import org.jfree.chart.plot.PlotOrientation;
54 import org.jfree.chart.renderer.BarRenderer;
55 import org.jfree.data.CategoryDataset;
56 import org.jfree.data.DefaultCategoryDataset;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RefineryUtilities;
59
60 /**
61  * A simple demonstration application showing how to create a bar chart.
62  *
63  * @author David Gilbert
64  */

65 public class BarChartDemo extends ApplicationFrame {
66
67     /**
68      * Creates a new demo instance.
69      *
70      * @param title the frame title.
71      */

72     public BarChartDemo(String JavaDoc title) {
73
74         super(title);
75
76         CategoryDataset dataset = createDataset();
77         JFreeChart chart = createChart(dataset);
78
79         // add the chart to a panel...
80
ChartPanel chartPanel = new ChartPanel(chart);
81         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
82         setContentPane(chartPanel);
83
84     }
85
86     /**
87      * Returns a sample dataset.
88      *
89      * @return The dataset.
90      */

91     private CategoryDataset createDataset() {
92         
93         // row keys...
94
String JavaDoc series1 = "First";
95         String JavaDoc series2 = "Second";
96         String JavaDoc series3 = "Third";
97
98         // column keys...
99
String JavaDoc category1 = "Category 1";
100         String JavaDoc category2 = "Category 2";
101         String JavaDoc category3 = "Category 3";
102         String JavaDoc category4 = "Category 4";
103         String JavaDoc category5 = "Category 5";
104
105         // create the dataset...
106
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
107
108         dataset.addValue(1.0, series1, category1);
109         dataset.addValue(4.0, series1, category2);
110         dataset.addValue(3.0, series1, category3);
111         dataset.addValue(5.0, series1, category4);
112         dataset.addValue(5.0, series1, category5);
113
114         dataset.addValue(5.0, series2, category1);
115         dataset.addValue(7.0, series2, category2);
116         dataset.addValue(6.0, series2, category3);
117         dataset.addValue(8.0, series2, category4);
118         dataset.addValue(4.0, series2, category5);
119
120         dataset.addValue(4.0, series3, category1);
121         dataset.addValue(3.0, series3, category2);
122         dataset.addValue(2.0, series3, category3);
123         dataset.addValue(3.0, series3, category4);
124         dataset.addValue(6.0, series3, category5);
125         
126         return dataset;
127         
128     }
129     
130     /**
131      * Creates a sample chart.
132      *
133      * @param dataset the dataset.
134      *
135      * @return The chart.
136      */

137     private JFreeChart createChart(CategoryDataset dataset) {
138         
139         // create the chart...
140
JFreeChart chart = ChartFactory.createBarChart(
141             "Bar Chart Demo", // chart title
142
"Category", // domain axis label
143
"Value", // range axis label
144
dataset, // data
145
PlotOrientation.VERTICAL,
146             true, // include legend
147
true, // tooltips?
148
false // URLs?
149
);
150
151         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
152

153         // set the background color for the chart...
154
chart.setBackgroundPaint(new Color JavaDoc(0xBBBBDD));
155
156         // get a reference to the plot for further customisation...
157
CategoryPlot plot = chart.getCategoryPlot();
158         
159         // set the range axis to display integers only...
160
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
161         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
162
163         // disable bar outlines...
164
BarRenderer renderer = (BarRenderer) plot.getRenderer();
165         renderer.setDrawBarOutline(false);
166         
167         // set up gradient paints for series...
168
GradientPaint JavaDoc gp0 = new GradientPaint JavaDoc(
169             0.0f, 0.0f, Color.blue,
170                     0.0f, 0.0f, Color.lightGray
171         );
172                 GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(
173                     0.0f, 0.0f, Color.green,
174             0.0f, 0.0f, Color.lightGray
175                 );
176                 GradientPaint JavaDoc gp2 = new GradientPaint JavaDoc(
177                     0.0f, 0.0f, Color.red,
178             0.0f, 0.0f, Color.lightGray
179                 );
180         renderer.setSeriesPaint(0, gp0);
181         renderer.setSeriesPaint(1, gp1);
182         renderer.setSeriesPaint(2, gp2);
183         
184         // OPTIONAL CUSTOMISATION COMPLETED.
185

186         return chart;
187         
188     }
189     
190     // ****************************************************************************
191
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
192
// * Please note that commercial support and documentation is available from: *
193
// * *
194
// * http://www.object-refinery.com/jfreechart/support.html *
195
// * *
196
// * This is not only a great service for developers, but is a VERY IMPORTANT *
197
// * source of funding for the JFreeChart project. Please support us so that *
198
// * we can continue developing free software. *
199
// ****************************************************************************
200

201     /**
202      * Starting point for the demonstration application.
203      *
204      * @param args ignored.
205      */

206     public static void main(String JavaDoc[] args) {
207
208         BarChartDemo demo = new BarChartDemo("Bar Chart Demo");
209         demo.pack();
210         RefineryUtilities.centerFrameOnScreen(demo);
211         demo.setVisible(true);
212
213     }
214
215 }
216
Popular Tags