KickJava   Java API By Example, From Geeks To Geeks.

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


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, 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  * SampleXYDataset2.java
24  * ---------------------
25  * (C) Copyright 2000-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: SampleXYDataset2.java,v 1.2 2003/05/29 15:23:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 22-Oct-2001 : Version 1 (DG);
35  * Renamed DataSource.java --> Dataset.java etc. (DG);
36  * 07-Nov-2001 : Updated source header (DG);
37  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  * 29-Oct-2002 : Modified so that you can specify dataset parameters in the constructor (DG);
39  *
40  */

41
42 package org.jfree.chart.demo;
43
44 import org.jfree.data.AbstractSeriesDataset;
45 import org.jfree.data.DomainInfo;
46 import org.jfree.data.Range;
47 import org.jfree.data.RangeInfo;
48 import org.jfree.data.XYDataset;
49
50 /**
51  * Random data for a scatter plot demo.
52  * <P>
53  * Note that the aim of this class is to create a self-contained data source for demo purposes -
54  * it is NOT intended to show how you should go about writing your own datasets.
55  *
56  * @author David Gilbert
57  */

58 public class SampleXYDataset2 extends AbstractSeriesDataset implements XYDataset,
59                                                                        DomainInfo, RangeInfo {
60
61     /** The series count. */
62     private static final int DEFAULT_SERIES_COUNT = 4;
63
64     /** The item count. */
65     private static final int DEFAULT_ITEM_COUNT = 100;
66
67     /** The range. */
68     private static final double DEFAULT_RANGE = 200;
69
70     /** The x values. */
71     private Double JavaDoc[][] xValues;
72
73     /** The y values. */
74     private Double JavaDoc[][] yValues;
75
76     /** The number of series. */
77     private int seriesCount;
78
79     /** The number of items. */
80     private int itemCount;
81
82     /** The minimum domain value. */
83     private Number JavaDoc domainMin;
84
85     /** The maximum domain value. */
86     private Number JavaDoc domainMax;
87
88     /** The minimum range value. */
89     private Number JavaDoc rangeMin;
90
91     /** The maximum range value. */
92     private Number JavaDoc rangeMax;
93
94     /** The range of the domain. */
95     private Range domainRange;
96
97     /** The range. */
98     private Range range;
99
100     /**
101      * Creates a sample dataset using default settings (4 series, 100 data items per series,
102      * random data in the range 0 - 200).
103      */

104     public SampleXYDataset2() {
105         this(DEFAULT_SERIES_COUNT, DEFAULT_ITEM_COUNT);
106     }
107
108     /**
109      * Creates a sample dataset.
110      *
111      * @param seriesCount the number of series.
112      * @param itemCount the number of items.
113      */

114     public SampleXYDataset2(int seriesCount, int itemCount) {
115
116         this.xValues = new Double JavaDoc[seriesCount][itemCount];
117         this.yValues = new Double JavaDoc[seriesCount][itemCount];
118         this.seriesCount = seriesCount;
119         this.itemCount = itemCount;
120
121         double minX = Double.POSITIVE_INFINITY;
122         double maxX = Double.NEGATIVE_INFINITY;
123         double minY = Double.POSITIVE_INFINITY;
124         double maxY = Double.NEGATIVE_INFINITY;
125
126         for (int series = 0; series < seriesCount; series++) {
127             for (int item = 0; item < itemCount; item++) {
128
129                 double x = (Math.random() - 0.5) * DEFAULT_RANGE;
130                 xValues[series][item] = new Double JavaDoc(x);
131                 if (x < minX) {
132                     minX = x;
133                 }
134                 if (x > maxX) {
135                     maxX = x;
136                 }
137
138                 double y = (Math.random() + 0.5) * 6 * x + x;
139                 yValues[series][item] = new Double JavaDoc(y);
140                 if (y < minY) {
141                     minY = y;
142                 }
143                 if (y > maxY) {
144                     maxY = y;
145                 }
146
147             }
148         }
149
150         this.domainMin = new Double JavaDoc(minX);
151         this.domainMax = new Double JavaDoc(maxX);
152         this.domainRange = new Range(minX, maxX);
153
154         this.rangeMin = new Double JavaDoc(minY);
155         this.rangeMax = new Double JavaDoc(maxY);
156         this.range = new Range(minY, maxY);
157
158     }
159
160     /**
161      * Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
162      *
163      * @param series the index (zero-based) of the series.
164      * @param item the index (zero-based) of the required item.
165      *
166      * @return the x-value for the specified series and item.
167      */

168     public Number JavaDoc getXValue(int series, int item) {
169         return xValues[series][item];
170     }
171
172     /**
173      * Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
174      *
175      * @param series the index (zero-based) of the series.
176      * @param item the index (zero-based) of the required item.
177      *
178      * @return the y-value for the specified series and item.
179      */

180     public Number JavaDoc getYValue(int series, int item) {
181         return yValues[series][item];
182     }
183
184     /**
185      * Returns the number of series in the dataset.
186      *
187      * @return the series count.
188      */

189     public int getSeriesCount() {
190         return this.seriesCount;
191     }
192
193     /**
194      * Returns the name of the series.
195      *
196      * @param series the index (zero-based) of the series.
197      *
198      * @return the name of the series.
199      */

200     public String JavaDoc getSeriesName(int series) {
201         return "Sample " + series;
202     }
203
204     /**
205      * Returns the number of items in the specified series.
206      *
207      * @param series the index (zero-based) of the series.
208      *
209      * @return the number of items in the specified series.
210      */

211     public int getItemCount(int series) {
212         return this.itemCount;
213     }
214
215     /**
216      * Returns the minimum domain value.
217      *
218      * @return the minimum domain value.
219      */

220     public Number JavaDoc getMinimumDomainValue() {
221         return this.domainMin;
222     }
223
224     /**
225      * Returns the maximum domain value.
226      *
227      * @return the maximum domain value.
228      */

229     public Number JavaDoc getMaximumDomainValue() {
230         return this.domainMax;
231     }
232
233     /**
234      * Returns the range of values in the domain.
235      *
236      * @return the range.
237      */

238     public Range getDomainRange() {
239         return this.domainRange;
240     }
241
242     /**
243      * Returns the minimum range value.
244      *
245      * @return the minimum range value.
246      */

247     public Number JavaDoc getMinimumRangeValue() {
248         return this.rangeMin;
249     }
250
251     /**
252      * Returns the maximum range value.
253      *
254      * @return the maximum range value.
255      */

256     public Number JavaDoc getMaximumRangeValue() {
257         return this.rangeMax;
258     }
259
260     /**
261      * Returns the range of values in the range (y-values).
262      *
263      * @return the range.
264      */

265     public Range getValueRange() {
266         return this.range;
267     }
268
269 }
270
Popular Tags