KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > CombinedXYPlotTests


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  * CombinedXYPlotTests.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: CombinedXYPlotTests.java,v 1.13 2003/08/15 11:55:26 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 27-Mar-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.plot.junit;
39
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.ObjectInput JavaDoc;
43 import java.io.ObjectInputStream JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45 import java.io.ObjectOutputStream JavaDoc;
46
47 import junit.framework.Test;
48 import junit.framework.TestCase;
49 import junit.framework.TestSuite;
50
51 import org.jfree.chart.axis.AxisLocation;
52 import org.jfree.chart.axis.DateAxis;
53 import org.jfree.chart.axis.NumberAxis;
54 import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
55 import org.jfree.chart.plot.CombinedDomainXYPlot;
56 import org.jfree.chart.plot.XYPlot;
57 import org.jfree.chart.renderer.StandardXYItemRenderer;
58 import org.jfree.chart.renderer.XYBarRenderer;
59 import org.jfree.chart.renderer.XYItemRenderer;
60 import org.jfree.data.IntervalXYDataset;
61 import org.jfree.data.XYDataset;
62 import org.jfree.data.time.Day;
63 import org.jfree.data.time.TimeSeries;
64 import org.jfree.data.time.TimeSeriesCollection;
65 import org.jfree.date.SerialDate;
66
67 /**
68  * Tests for the {@link CombinedXYPlot} class.
69  *
70  * @author David Gilbert
71  */

72 public class CombinedXYPlotTests extends TestCase {
73
74     /**
75      * Returns the tests as a test suite.
76      *
77      * @return the test suite.
78      */

79     public static Test suite() {
80         return new TestSuite(CombinedXYPlotTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public CombinedXYPlotTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Serialize an instance, restore it, and check for equality.
94      */

95     public void testSerialization() {
96
97         // create a parent plot...
98
CombinedDomainXYPlot p1 = new CombinedDomainXYPlot(new NumberAxis("Value"));
99         p1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
100
101         // create subplot 1...
102
IntervalXYDataset data1 = createDataset1();
103         XYItemRenderer renderer1 = new XYBarRenderer(0.20);
104         renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0,000.0"));
105         XYPlot subplot1 = new XYPlot(data1, new DateAxis("Date"), null, renderer1);
106
107         // create subplot 2...
108
XYDataset data2 = this.createDataset2();
109         XYItemRenderer renderer2 = new StandardXYItemRenderer();
110         renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0,000.0"));
111         XYPlot subplot2 = new XYPlot(data2, new DateAxis("Date"), null, renderer2);
112
113         // add the subplots...
114
p1.add(subplot1, 1);
115         p1.add(subplot2, 1);
116
117         CombinedDomainXYPlot p2 = null;
118
119         try {
120             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
121             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
122             out.writeObject(p1);
123             out.close();
124
125             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
126             p2 = (CombinedDomainXYPlot) in.readObject();
127             in.close();
128         }
129         catch (Exception JavaDoc e) {
130             e.printStackTrace();
131         }
132         assertEquals(p1, p2);
133
134     }
135
136     /**
137      * Creates a sample dataset.
138      *
139      * @return Series 1.
140      */

141     private IntervalXYDataset createDataset1() {
142
143         // create dataset 1...
144
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
145         series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3);
146         series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4);
147         series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3);
148         series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3);
149         series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4);
150         series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3);
151         series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5);
152         series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3);
153         series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4);
154         series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6);
155         series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3);
156         series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2);
157         series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2);
158         series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2);
159         series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2);
160
161         TimeSeriesCollection collection = new TimeSeriesCollection(series1);
162         collection.setDomainIsPointsInTime(false); // this tells the time series collection that
163
// we intend the data to represent time periods
164
// NOT points in time. This is required when
165
// determining the min/max values in the
166
// dataset's domain.
167
return collection;
168
169     }
170
171     /**
172      * Creates a sample dataset.
173      *
174      * @return Series 2.
175      */

176     private XYDataset createDataset2() {
177
178         // create dataset 2...
179
TimeSeries series2 = new TimeSeries("Series 2", Day.class);
180
181         series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2);
182         series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3);
183         series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5);
184         series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3);
185         series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0);
186         series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3);
187         series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2);
188         series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2);
189         series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9);
190         series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2);
191         series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3);
192         series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6);
193         series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7);
194         series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9);
195
196         return new TimeSeriesCollection(series2);
197
198     }
199
200 }
201
Popular Tags