KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChartDemo4.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: BarChartDemo4.java,v 1.2 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 14-Nov-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41 import java.awt.GradientPaint JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.plot.CategoryPlot;
48 import org.jfree.chart.plot.PlotOrientation;
49 import org.jfree.chart.renderer.BarRenderer;
50 import org.jfree.data.CategoryDataset;
51 import org.jfree.data.DefaultCategoryDataset;
52 import org.jfree.ui.ApplicationFrame;
53 import org.jfree.ui.RefineryUtilities;
54
55 /**
56  * A bar chart with only two bars - here the 'maxBarWidth' attribute in the renderer prevents
57  * the bars from getting too wide.
58  *
59  * @author David Gilbert
60  */

61 public class BarChartDemo4 extends ApplicationFrame {
62
63     /**
64      * Creates a new demo instance.
65      *
66      * @param title the frame title.
67      */

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

87     private CategoryDataset createDataset() {
88         
89         // row keys...
90
String JavaDoc series1 = "First";
91         String JavaDoc series2 = "Second";
92
93         // column keys...
94
String JavaDoc category1 = "Category 1";
95
96         // create the dataset...
97
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
98         dataset.addValue(1.0, series1, category1);
99         dataset.addValue(5.0, series2, category1);
100         
101         return dataset;
102         
103     }
104     
105     // ****************************************************************************
106
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
107
// * Please note that commercial support and documentation is available from: *
108
// * *
109
// * http://www.object-refinery.com/jfreechart/support.html *
110
// * *
111
// * This is not only a great service for developers, but is a VERY IMPORTANT *
112
// * source of funding for the JFreeChart project. Please support us so that *
113
// * we can continue developing free software. *
114
// ****************************************************************************
115

116     /**
117      * Creates a sample chart.
118      *
119      * @param dataset the dataset.
120      *
121      * @return The chart.
122      */

123     private JFreeChart createChart(CategoryDataset dataset) {
124         
125         // create the chart...
126
JFreeChart chart = ChartFactory.createBarChart(
127             "Bar Chart Demo", // chart title
128
"Category", // domain axis label
129
"Value", // range axis label
130
dataset, // data
131
PlotOrientation.VERTICAL,
132             true, // include legend
133
true, // tooltips?
134
false // URLs?
135
);
136
137         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
138

139         // set the background color for the chart...
140
chart.setBackgroundPaint(new Color JavaDoc(0xBBBBDD));
141
142         // get a reference to the plot for further customisation...
143
CategoryPlot plot = chart.getCategoryPlot();
144         
145         // set the range axis to display integers only...
146
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
147         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
148
149         // disable bar outlines...
150
BarRenderer renderer = (BarRenderer) plot.getRenderer();
151         renderer.setDrawBarOutline(false);
152         renderer.setMaxBarWidth(0.10);
153         
154         // set up gradient paints for series...
155
GradientPaint JavaDoc gp0 = new GradientPaint JavaDoc(
156             0.0f, 0.0f, Color.blue,
157             0.0f, 0.0f, Color.lightGray
158         );
159         GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(
160             0.0f, 0.0f, Color.green,
161             0.0f, 0.0f, Color.lightGray
162         );
163         renderer.setSeriesPaint(0, gp0);
164         renderer.setSeriesPaint(1, gp1);
165         
166         // OPTIONAL CUSTOMISATION COMPLETED.
167

168         return chart;
169         
170     }
171     
172     /**
173      * Starting point for the demonstration application.
174      *
175      * @param args ignored.
176      */

177     public static void main(String JavaDoc[] args) {
178
179         BarChartDemo4 demo = new BarChartDemo4("Bar Chart Demo 4");
180         demo.pack();
181         RefineryUtilities.centerFrameOnScreen(demo);
182         demo.setVisible(true);
183
184     }
185
186 }
187
Popular Tags