KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SampleYSymbolicDataset.java
24  * ---------------------------
25  *
26  * Original Author: Anthony Boulestreau.
27  * Contributor(s): -;
28  *
29  *
30  * Changes
31  * -------
32  * 29-Mar-2002 : Version 1 (AB);
33  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
34  *
35  */

36
37 package org.jfree.chart.demo;
38
39 import java.lang.reflect.Array JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import org.jfree.data.AbstractSeriesDataset;
44 import org.jfree.data.XYDataset;
45 import org.jfree.data.YisSymbolic;
46
47 /**
48  * A dataset implementation that provides random data for a symbolic plot demo. This sample
49  * dataset has numerical x-values, and symbolic y-values.
50  *
51  * @author Anthony Boulestreau
52  */

53 public class SampleYSymbolicDataset extends AbstractSeriesDataset
54                                     implements XYDataset, YisSymbolic {
55
56     /** The series count. */
57     private static final int DEFAULT_SERIES_COUNT = 1;
58
59     /** The item count. */
60     private static final int DEFAULT_ITEM_COUNT = 50;
61
62     /** The series index. */
63     private int serie;
64
65     /** The item index. */
66     private int item;
67
68     /** The series names. */
69     private String JavaDoc[] serieNames;
70
71     /** The x values. */
72     private Double JavaDoc[][] xValues;
73
74     /** The y values. */
75     private Integer JavaDoc[][] yValues;
76
77     /** The y symbolic values. */
78     private String JavaDoc[] ySymbolicValues;
79
80     /** The dataset name. */
81     private String JavaDoc datasetName;
82
83     /**
84      * Creates a new sample dataset.
85      *
86      * @param datasetName the dataset name.
87      * @param xRange the upper limit of the (random) x-values.
88      * @param tabString the symbols for the y-values.
89      */

90     public SampleYSymbolicDataset(String JavaDoc datasetName, int xRange, String JavaDoc[] tabString) {
91         this(datasetName, xRange, tabString, DEFAULT_SERIES_COUNT, DEFAULT_ITEM_COUNT, null);
92     }
93
94     /**
95      * Creates a new sample dataset.
96      *
97      * @param datasetName the dataset name.
98      * @param xRange the upper limit of the (random) x-values.
99      * @param tabString the symbolic y-values.
100      * @param seriesCount the number of series to create.
101      * @param itemCount the number of items to create for each series.
102      * @param serieNames the series names.
103      */

104     public SampleYSymbolicDataset(String JavaDoc datasetName,
105                                   int xRange,
106                                   String JavaDoc[] tabString,
107                                   int seriesCount,
108                                   int itemCount,
109                                   String JavaDoc[] serieNames) {
110
111         this.datasetName = datasetName;
112         this.ySymbolicValues = tabString;
113         this.serie = seriesCount;
114         this.item = itemCount;
115         this.serieNames = serieNames;
116         this.xValues = new Double JavaDoc[seriesCount][itemCount];
117         this.yValues = new Integer JavaDoc[seriesCount][itemCount];
118
119         for (int s = 0; s < seriesCount; s++) {
120             for (int i = 0; i < itemCount; i++) {
121                 double x = Math.random() * xRange;
122                 double y = Math.random() * tabString.length;
123                 xValues[s][i] = new Double JavaDoc(x);
124                 yValues[s][i] = new Integer JavaDoc((int) y);
125             }
126         }
127     }
128
129     /**
130      * Creates a new sample dataset.
131      *
132      * @param datasetName the dataset name.
133      * @param xValues the x values.
134      * @param yValues the y values.
135      * @param ySymbolicValues the y symbols
136      * @param seriesCount the series count.
137      * @param itemCount the item count.
138      * @param serieNames the series names.
139      */

140     public SampleYSymbolicDataset(String JavaDoc datasetName,
141                                   Double JavaDoc[][] xValues,
142                                   Integer JavaDoc[][] yValues,
143                                   String JavaDoc[] ySymbolicValues,
144                                   int seriesCount,
145                                   int itemCount,
146                                   String JavaDoc[] serieNames) {
147
148         this.datasetName = datasetName;
149         this.xValues = xValues;
150         this.yValues = yValues;
151         this.ySymbolicValues = ySymbolicValues;
152         this.serie = seriesCount;
153         this.item = itemCount;
154         this.serieNames = serieNames;
155
156     }
157
158
159     /**
160      * Returns the x-value for the specified series and item. Series are
161      * 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
174      * numbered 0, 1, ...
175      *
176      * @param series the index (zero-based) of the series.
177      * @param item the index (zero-based) of the required item.
178      *
179      * @return the y-value for the specified series and item.
180      */

181     public Number JavaDoc getYValue(int series, int item) {
182         return yValues[series][item];
183     }
184
185     /**
186      * Sets the y-value for the specified series and item with the specified
187      * new <CODE>Number</CODE> value. Series are numbered 0, 1, ...
188      * <P>
189      * This method is used by combineYSymbolicDataset to modify the reference
190      * to the symbolic value ...
191      *
192      * @param series the index (zero-based) of the series.
193      * @param item the index (zero-based) of the required item.
194      * @param newValue the value to set.
195      */

196     public void setYValue(int series, int item, Number JavaDoc newValue) {
197         yValues[series][item] = (Integer JavaDoc) newValue;
198     }
199
200     /**
201      * Returns the number of series in the dataset.
202      *
203      * @return The number of series in the dataset.
204      */

205     public int getSeriesCount() {
206         return this.serie;
207     }
208
209     /**
210      * Returns the name of the series.
211      *
212      * @param series the index (zero-based) of the series.
213      *
214      * @return the name of the series.
215      */

216     public String JavaDoc getSeriesName(int series) {
217         if (serieNames != null) {
218             return serieNames[series];
219         }
220         else {
221             return datasetName + series;
222         }
223     }
224
225     /**
226      * Returns the number of items in the specified series.
227      *
228      * @param series The index (zero-based) of the series.
229      *
230      * @return the number of items in the specified series.
231      */

232     public int getItemCount(int series) {
233         return this.item;
234     }
235
236     /**
237      * Returns the list of symbolic values.
238      *
239      * @return array of symbolic value.
240      */

241     public String JavaDoc[] getYSymbolicValues() {
242         return ySymbolicValues;
243     }
244
245     /**
246      * Sets the list of symbolic values.
247      *
248      * @param sValues the new list of symbolic value.
249      */

250     public void setYSymbolicValues(String JavaDoc[] sValues) {
251         ySymbolicValues = sValues;
252     }
253
254     /**
255      * Returns the symbolic value of the data set specified by
256      * <CODE>series</CODE> and <CODE>item</CODE> parameters.
257      *
258      * @param series value of the serie.
259      * @param item value of the item.
260      *
261      * @return the symbolic value.
262      */

263     public String JavaDoc getYSymbolicValue(int series, int item) {
264         Integer JavaDoc intValue = (Integer JavaDoc) getYValue(series, item);
265         return getYSymbolicValue(intValue);
266     }
267
268     /**
269      * Returns the symbolic value linked with the specified <CODE>Integer</CODE>.
270      *
271      * @param val value of the integer linked with the symbolic value.
272      *
273      * @return the symbolic value.
274      */

275     public String JavaDoc getYSymbolicValue(Integer JavaDoc val) {
276         return ySymbolicValues[val.intValue()];
277     }
278
279     /**
280      * This function modify <CODE>dataset1</CODE> and <CODE>dataset1</CODE> in
281      * order that they share the same symbolic value list.
282      * <P>
283      * The sharing symbolic value list is obtained adding the symbolic data
284      * list of the fist data set to the symbolic data list of the second data
285      * set.
286      * <P>
287      * This function is use with the <I>combined plot</I> functions of
288      * JFreeChart.
289      *
290      * @param dataset1 the first data set to combine.
291      * @param dataset2 the second data set to combine.
292      *
293      * @return the shared symbolic array.
294      */

295     public static String JavaDoc[] combineYSymbolicDataset(YisSymbolic dataset1, YisSymbolic dataset2) {
296
297         SampleYSymbolicDataset sDataset1 = (SampleYSymbolicDataset) dataset1;
298         SampleYSymbolicDataset sDataset2 = (SampleYSymbolicDataset) dataset2;
299         String JavaDoc[] sDatasetSymbolicValues1 = sDataset1.getYSymbolicValues();
300         String JavaDoc[] sDatasetSymbolicValues2 = sDataset2.getYSymbolicValues();
301
302         //Combine the two list of symbolic value of the two data set
303
int s1length = sDatasetSymbolicValues1.length;
304         int s2length = sDatasetSymbolicValues2.length;
305         List JavaDoc ySymbolicValuesCombined = new Vector JavaDoc();
306         for (int i = 0; i < s1length; i++) {
307             ySymbolicValuesCombined.add(sDatasetSymbolicValues1[i]);
308         }
309         for (int i = 0; i < s2length; i++) {
310             if (!ySymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) {
311                 ySymbolicValuesCombined.add(sDatasetSymbolicValues2[i]);
312             }
313         }
314
315         //Change the Integer reference of the second data set
316
int newIndex;
317         for (int i = 0; i < sDataset2.getSeriesCount(); i++) {
318             for (int j = 0; j < sDataset2.getItemCount(i); j++) {
319                 newIndex = ySymbolicValuesCombined.indexOf(sDataset2.getYSymbolicValue(i, j));
320                 sDataset2.setYValue(i, j, new Integer JavaDoc(newIndex));
321             }
322         }
323
324         //Set the new list of symbolic value on the two data sets
325
String JavaDoc[] ySymbolicValuesCombinedA = new String JavaDoc[ySymbolicValuesCombined.size()];
326         ySymbolicValuesCombined.toArray(ySymbolicValuesCombinedA);
327         sDataset1.setYSymbolicValues(ySymbolicValuesCombinedA);
328         sDataset2.setYSymbolicValues(ySymbolicValuesCombinedA);
329
330         return ySymbolicValuesCombinedA;
331     }
332
333     /**
334      * Clone the SampleYSymbolicDataset object
335      *
336      * @return the cloned object.
337      */

338     public Object JavaDoc clone() {
339         String JavaDoc nDatasetName = new String JavaDoc(this.datasetName);
340         Double JavaDoc[][] nXValues = (Double JavaDoc[][]) cloneArray(this.xValues);
341         Integer JavaDoc[][] nYValues = (Integer JavaDoc[][]) cloneArray(this.yValues);
342         String JavaDoc[] nYSymbolicValues = (String JavaDoc[]) cloneArray(this.ySymbolicValues);
343         int serie = this.serie;
344         int item = this.item;
345         String JavaDoc[] serieNames = (String JavaDoc[]) cloneArray(this.serieNames);
346         return new SampleYSymbolicDataset(nDatasetName, nXValues, nYValues,
347                 nYSymbolicValues, serie, item, serieNames);
348     }
349
350     /**
351      * Clones the array.
352      *
353      * @param arr the array.
354      *
355      * @return an array.
356      */

357     private static Object JavaDoc cloneArray(Object JavaDoc arr) {
358
359         if (arr == null) {
360             return arr;
361         }
362
363         Class JavaDoc cls = arr.getClass();
364         if (!cls.isArray()) {
365             return arr;
366         }
367
368         int length = Array.getLength(arr);
369         Object JavaDoc[] newarr = (Object JavaDoc[]) Array.newInstance(cls.getComponentType(), length);
370
371         Object JavaDoc obj;
372
373         for (int i = 0; i < length; i++) {
374             obj = Array.get(arr, i);
375             if (obj.getClass().isArray()) {
376                 newarr[i] = cloneArray(obj);
377             }
378             else {
379                 newarr[i] = obj;
380             }
381         }
382
383         return newarr;
384      }
385
386 }
387
Popular Tags