KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------
28  * BarChartDemo1.java
29  * ------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): ;
34  *
35  * $Id: BarChartDemo1.java,v 1.1.2.3 2006/10/25 10:38:47 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
40  * the JFreeChart Developer Guide (DG);
41  *
42  */

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

67 public class BarChartDemo1 extends ApplicationFrame {
68
69     /**
70      * Creates a new demo instance.
71      *
72      * @param title the frame title.
73      */

74     public BarChartDemo1(String JavaDoc title) {
75         super(title);
76         CategoryDataset dataset = createDataset();
77         JFreeChart chart = createChart(dataset);
78         ChartPanel chartPanel = new ChartPanel(chart, false);
79         chartPanel.setPreferredSize(new Dimension JavaDoc(500, 270));
80         setContentPane(chartPanel);
81     }
82
83     /**
84      * Returns a sample dataset.
85      *
86      * @return The dataset.
87      */

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

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

150         // set the background color for the chart...
151
chart.setBackgroundPaint(Color.white);
152
153         // get a reference to the plot for further customisation...
154
CategoryPlot plot = chart.getCategoryPlot();
155         plot.setBackgroundPaint(Color.lightGray);
156         plot.setDomainGridlinePaint(Color.white);
157         plot.setDomainGridlinesVisible(true);
158         plot.setRangeGridlinePaint(Color.white);
159
160         // ******************************************************************
161
// More than 150 demo applications are included with the JFreeChart
162
// Developer Guide...for more information, see:
163
//
164
// > http://www.object-refinery.com/jfreechart/guide.html
165
//
166
// ******************************************************************
167

168         // set the range axis to display integers only...
169
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
170         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
171
172         // disable bar outlines...
173
BarRenderer renderer = (BarRenderer) plot.getRenderer();
174         renderer.setDrawBarOutline(false);
175         
176         // set up gradient paints for series...
177
GradientPaint JavaDoc gp0 = new GradientPaint JavaDoc(0.0f, 0.0f, Color.blue,
178                 0.0f, 0.0f, new Color JavaDoc(0, 0, 64));
179         GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(0.0f, 0.0f, Color.green,
180                 0.0f, 0.0f, new Color JavaDoc(0, 64, 0));
181         GradientPaint JavaDoc gp2 = new GradientPaint JavaDoc(0.0f, 0.0f, Color.red,
182                 0.0f, 0.0f, new Color JavaDoc(64, 0, 0));
183         renderer.setSeriesPaint(0, gp0);
184         renderer.setSeriesPaint(1, gp1);
185         renderer.setSeriesPaint(2, gp2);
186
187         CategoryAxis domainAxis = plot.getDomainAxis();
188         domainAxis.setCategoryLabelPositions(
189                 CategoryLabelPositions.createUpRotationLabelPositions(
190                         Math.PI / 6.0));
191         // OPTIONAL CUSTOMISATION COMPLETED.
192

193         return chart;
194         
195     }
196     
197     /**
198      * Starting point for the demonstration application.
199      *
200      * @param args ignored.
201      */

202     public static void main(String JavaDoc[] args) {
203         BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo 1");
204         demo.pack();
205         RefineryUtilities.centerFrameOnScreen(demo);
206         demo.setVisible(true);
207     }
208
209 }
210
Popular Tags