KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > junit > StackedAreaChartTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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  * StackedAreaChartTests.java
29  * --------------------------
30  * (C) Copyright 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StackedAreaChartTests.java,v 1.1.2.1 2006/10/03 15:41:33 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 12-Apr-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import java.awt.Graphics2D JavaDoc;
46 import java.awt.geom.Rectangle2D JavaDoc;
47 import java.awt.image.BufferedImage JavaDoc;
48
49 import junit.framework.Test;
50 import junit.framework.TestCase;
51 import junit.framework.TestSuite;
52
53 import org.jfree.chart.ChartFactory;
54 import org.jfree.chart.JFreeChart;
55 import org.jfree.chart.axis.ValueAxis;
56 import org.jfree.chart.event.ChartChangeEvent;
57 import org.jfree.chart.event.ChartChangeListener;
58 import org.jfree.chart.labels.CategoryToolTipGenerator;
59 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
60 import org.jfree.chart.plot.CategoryPlot;
61 import org.jfree.chart.plot.PlotOrientation;
62 import org.jfree.chart.renderer.category.CategoryItemRenderer;
63 import org.jfree.chart.urls.CategoryURLGenerator;
64 import org.jfree.chart.urls.StandardCategoryURLGenerator;
65 import org.jfree.data.Range;
66 import org.jfree.data.category.CategoryDataset;
67 import org.jfree.data.general.DatasetUtilities;
68
69 /**
70  * Some tests for a stacked area chart.
71  */

72 public class StackedAreaChartTests extends TestCase {
73
74     /** A chart. */
75     private JFreeChart chart;
76
77     /**
78      * Returns the tests as a test suite.
79      *
80      * @return The test suite.
81      */

82     public static Test suite() {
83         return new TestSuite(StackedAreaChartTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

91     public StackedAreaChartTests(String JavaDoc name) {
92         super(name);
93     }
94
95     /**
96      * Common test setup.
97      */

98     protected void setUp() {
99         this.chart = createChart();
100     }
101
102     /**
103      * Draws the chart with a null info object to make sure that no exceptions
104      * are thrown (a problem that was occurring at one point).
105      */

106     public void testDrawWithNullInfo() {
107
108         boolean success = false;
109         try {
110             BufferedImage JavaDoc image = new BufferedImage JavaDoc(
111                 200 , 100, BufferedImage.TYPE_INT_RGB
112             );
113             Graphics2D JavaDoc g2 = image.createGraphics();
114             this.chart.draw(
115                 g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null
116             );
117             g2.dispose();
118             success = true;
119         }
120         catch (Exception JavaDoc e) {
121           success = false;
122         }
123         assertTrue(success);
124
125     }
126
127     /**
128      * Replaces the dataset and checks that it has changed as expected.
129      */

130     public void testReplaceDataset() {
131
132         // create a dataset...
133
Number JavaDoc[][] data = new Integer JavaDoc[][]
134             {{new Integer JavaDoc(-30), new Integer JavaDoc(-20)},
135              {new Integer JavaDoc(-10), new Integer JavaDoc(10)},
136              {new Integer JavaDoc(20), new Integer JavaDoc(30)}};
137
138         CategoryDataset newData
139             = DatasetUtilities.createCategoryDataset("S", "C", data);
140
141         LocalListener l = new LocalListener();
142         this.chart.addChangeListener(l);
143         this.chart.getCategoryPlot().setDataset(newData);
144         assertEquals(true, l.flag);
145         ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
146         Range range = axis.getRange();
147         assertTrue("Expecting the lower bound of the range to be around -30: "
148                     + range.getLowerBound(), range.getLowerBound() <= -30);
149         assertTrue("Expecting the upper bound of the range to be around 30: "
150                    + range.getUpperBound(), range.getUpperBound() >= 30);
151
152     }
153
154     /**
155      * Check that setting a tool tip generator for a series does override the
156      * default generator.
157      */

158     public void testSetSeriesToolTipGenerator() {
159         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
160         CategoryItemRenderer renderer = plot.getRenderer();
161         StandardCategoryToolTipGenerator tt
162             = new StandardCategoryToolTipGenerator();
163         renderer.setSeriesToolTipGenerator(0, tt);
164         CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
165         assertTrue(tt2 == tt);
166     }
167     
168     /**
169      * Check that setting a URL generator for a series does override the
170      * default generator.
171      */

172     public void testSetSeriesURLGenerator() {
173         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
174         CategoryItemRenderer renderer = plot.getRenderer();
175         StandardCategoryURLGenerator url1
176             = new StandardCategoryURLGenerator();
177         renderer.setSeriesItemURLGenerator(0, url1);
178         CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
179         assertTrue(url2 == url1);
180     }
181     
182     /**
183      * Create a stacked bar chart with sample data in the range -3 to +3.
184      *
185      * @return The chart.
186      */

187     private static JFreeChart createChart() {
188
189         // create a dataset...
190
Number JavaDoc[][] data = new Integer JavaDoc[][]
191             {{new Integer JavaDoc(-3), new Integer JavaDoc(-2)},
192              {new Integer JavaDoc(-1), new Integer JavaDoc(1)},
193              {new Integer JavaDoc(2), new Integer JavaDoc(3)}};
194
195         CategoryDataset dataset
196             = DatasetUtilities.createCategoryDataset("S", "C", data);
197
198         // create the chart...
199
return ChartFactory.createStackedAreaChart(
200             "Stacked Area Chart", // chart title
201
"Domain", "Range",
202             dataset, // data
203
PlotOrientation.HORIZONTAL,
204             true, // include legend
205
true,
206             true
207         );
208
209     }
210
211     /**
212      * A chart change listener.
213      */

214     static class LocalListener implements ChartChangeListener {
215
216         /** A flag. */
217         private boolean flag = false;
218
219         /**
220          * Event handler.
221          *
222          * @param event the event.
223          */

224         public void chartChanged(ChartChangeEvent event) {
225             this.flag = true;
226         }
227
228     }
229
230 }
231
Popular Tags