KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------
27  * ScatterPlotTests.java
28  * ---------------------
29  * (C) Copyright 2002-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: ScatterPlotTests.java,v 1.4 2005/04/13 08:43:26 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 11-Jun-2002 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (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.StandardXYToolTipGenerator;
59 import org.jfree.chart.labels.XYToolTipGenerator;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.chart.plot.XYPlot;
62 import org.jfree.chart.renderer.xy.XYItemRenderer;
63 import org.jfree.data.Range;
64 import org.jfree.data.xy.XYDataset;
65 import org.jfree.data.xy.XYSeries;
66 import org.jfree.data.xy.XYSeriesCollection;
67
68 /**
69  * Tests for a scatter plot.
70  *
71  */

72 public class ScatterPlotTests 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(ScatterPlotTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

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

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

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

135     public void testReplaceDataset() {
136
137         // create a dataset...
138
XYSeries series1 = new XYSeries("Series 1");
139         series1.add(10.0, 10.0);
140         series1.add(20.0, 20.0);
141         series1.add(30.0, 30.0);
142         XYDataset dataset = new XYSeriesCollection(series1);
143
144         LocalListener l = new LocalListener();
145         this.chart.addChangeListener(l);
146         this.chart.getXYPlot().setDataset(dataset);
147         assertEquals(true, l.flag);
148         ValueAxis axis = this.chart.getXYPlot().getRangeAxis();
149         Range range = axis.getRange();
150         assertTrue("Expecting the lower bound of the range to be around 10: "
151                    + range.getLowerBound(), range.getLowerBound() <= 10);
152         assertTrue("Expecting the upper bound of the range to be around 30: "
153                    + range.getUpperBound(), range.getUpperBound() >= 30);
154
155     }
156
157     /**
158      * Check that setting a tool tip generator for a series does override the
159      * default generator.
160      */

161     public void testSetSeriesToolTipGenerator() {
162         XYPlot plot = (XYPlot) this.chart.getPlot();
163         XYItemRenderer renderer = plot.getRenderer();
164         StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
165         renderer.setSeriesToolTipGenerator(0, tt);
166         XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
167         assertTrue(tt2 == tt);
168     }
169     
170     /**
171      * Create a horizontal bar chart with sample data in the range -3 to +3.
172      *
173      * @return The chart.
174      */

175     private static JFreeChart createChart() {
176
177         // create a dataset...
178
XYSeries series1 = new XYSeries("Series 1");
179         series1.add(1.0, 1.0);
180         series1.add(2.0, 2.0);
181         series1.add(3.0, 3.0);
182         XYDataset dataset = new XYSeriesCollection(series1);
183
184         // create the chart...
185
return ChartFactory.createScatterPlot(
186             "Scatter Plot", // chart title
187
"Domain",
188             "Range",
189             dataset, // data
190
PlotOrientation.VERTICAL,
191             true, // include legend
192
true, // tooltips
193
false // urls
194
);
195
196     }
197
198     /**
199      * A chart change listener.
200      *
201      */

202     static class LocalListener implements ChartChangeListener {
203
204         /** A flag. */
205         private boolean flag = false;
206
207         /**
208          * Event handler.
209          *
210          * @param event the event.
211          */

212         public void chartChanged(ChartChangeEvent event) {
213             this.flag = true;
214         }
215
216     }
217
218 }
219
Popular Tags