KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SampleXYSymbolicDataset.java
24  * ----------------------------
25  * (C) Copyright 2000-2003, by Anthony Boulestreau and Contributors;
26  *
27  * Original Author: Anthony Boulestreau.
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
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.XisSymbolic;
46 import org.jfree.data.YisSymbolic;
47
48 /**
49  * Random data for a symbolic plot demo.
50  *
51  * @author Anthony Boulestreau
52  */

53 public class SampleXYSymbolicDataset extends AbstractSeriesDataset
54                                      implements XYDataset, XisSymbolic, YisSymbolic {
55
56     /** Series names. */
57     private String JavaDoc[] seriesName;
58
59     /** Items. */
60     private int[] item;
61
62     /** A series index. */
63     private int serie;
64
65     /** X values. */
66     private Integer JavaDoc[][] xValues;
67
68     /** Y values. */
69     private Integer JavaDoc[][] yValues;
70
71     /** X symbolic values. */
72     private String JavaDoc[] xSymbolicValues;
73
74     /** Y symbolic values. */
75     private String JavaDoc[] ySymbolicValues;
76
77     /** The dataset name. */
78     private String JavaDoc datasetName;
79
80     /**
81      * Creates a new dataset.
82      *
83      * @param datasetName the dataset name.
84      * @param xValues the x values.
85      * @param yValues the y values.
86      * @param xSymbolicValues the x symbols.
87      * @param ySymbolicValues the y symbols.
88      * @param seriesName the series name.
89      */

90     public SampleXYSymbolicDataset(String JavaDoc datasetName,
91                                    Integer JavaDoc[][] xValues,
92                                    Integer JavaDoc[][] yValues,
93                                    String JavaDoc[] xSymbolicValues,
94                                    String JavaDoc[] ySymbolicValues,
95                                    String JavaDoc[] seriesName) {
96
97         this.datasetName = datasetName;
98         this.xValues = xValues;
99         this.yValues = yValues;
100         this.xSymbolicValues = xSymbolicValues;
101         this.ySymbolicValues = ySymbolicValues;
102         this.serie = xValues.length;
103         this.item = new int[serie];
104         for (int i = 0; i < serie; i++) {
105             this.item[i] = xValues[i].length;
106         }
107         this.seriesName = seriesName;
108
109     }
110
111     /**
112      * Returns the x-value for the specified series and item. Series are
113      * numbered 0, 1, ...
114      *
115      * @param series the index (zero-based) of the series.
116      * @param item the index (zero-based) of the required item.
117      *
118      * @return the x-value for the specified series and item.
119      */

120     public Number JavaDoc getXValue(int series, int item) {
121         return xValues[series][item];
122     }
123
124     /**
125      * Returns the y-value for the specified series and item. Series are
126      * numbered 0, 1, ...
127      *
128      * @param series the index (zero-based) of the series.
129      * @param item the index (zero-based) of the required item.
130      *
131      * @return the y-value for the specified series and item.
132      */

133     public Number JavaDoc getYValue(int series, int item) {
134         return yValues[series][item];
135     }
136
137     /**
138      * Sets the x-value for the specified series and item with the specified
139      * new <CODE>Number</CODE> value. Series are numbered 0, 1, ...
140      * <P>
141      * This method is used by combineXSymbolicDataset to modify the reference
142      * to the symbolic value ...
143      *
144      * @param series the index (zero-based) of the series.
145      * @param item the index (zero-based) of the required item.
146      * @param newValue the value to set.
147      */

148     public void setXValue(int series, int item, Number JavaDoc newValue) {
149         xValues[series][item] = (Integer JavaDoc) newValue;
150     }
151
152     /**
153      * Sets the y-value for the specified series and item with the specified
154      * new <CODE>Number</CODE> value. Series are numbered 0, 1, ...
155      * <P>
156      * This method is used by combineYSymbolicDataset to modify the reference
157      * to the symbolic value ...
158      *
159      * @param series the index (zero-based) of the series.
160      * @param item the index (zero-based) of the required item.
161      * @param newValue the value to set.
162      */

163     public void setYValue(int series, int item, Number JavaDoc newValue) {
164         yValues[series][item] = (Integer JavaDoc) newValue;
165     }
166
167     /**
168      * Returns the number of series in the dataset.
169      *
170      * @return The number of series in the dataset.
171      */

172     public int getSeriesCount() {
173         return serie;
174     }
175
176     /**
177      * Returns the name of the series.
178      *
179      * @param series the index (zero-based) of the series.
180      *
181      * @return the name of the series.
182      */

183     public String JavaDoc getSeriesName(int series) {
184         if (seriesName != null) {
185             return seriesName[series];
186         }
187         else {
188             return datasetName + series;
189         }
190     }
191
192     /**
193      * Returns the number of items in the specified series.
194      *
195      * @param series the index (zero-based) of the series.
196      * @return the number of items in the specified series.
197      */

198     public int getItemCount(int series) {
199         return item[series];
200     }
201
202     /**
203      * Returns the list of X symbolic values.
204      *
205      * @return array of symbolic value.
206      */

207     public String JavaDoc[] getXSymbolicValues() {
208         return xSymbolicValues;
209     }
210
211     /**
212      * Returns the list of Y symbolic values.
213      *
214      * @return array of symbolic value.
215      */

216     public String JavaDoc[] getYSymbolicValues() {
217         return ySymbolicValues;
218     }
219
220     /**
221      * Sets the list of X symbolic values.
222      *
223      * @param sValues the new list of symbolic value.
224      */

225     public void setXSymbolicValues(String JavaDoc[] sValues) {
226         xSymbolicValues = sValues;
227     }
228
229     /**
230      * Sets the list of Y symbolic values.
231      *
232      * @param sValues the new list of symbolic value.
233      */

234     public void setYSymbolicValues(String JavaDoc[] sValues) {
235         ySymbolicValues = sValues;
236     }
237
238     /**
239      * Returns the X symbolic value of the data set specified by
240      * <CODE>series</CODE> and <CODE>item</CODE> parameters.
241      *
242      * @param series value of the serie.
243      * @param item value of the item.
244      *
245      * @return the symbolic value.
246      */

247     public String JavaDoc getXSymbolicValue(int series, int item) {
248         Integer JavaDoc intValue = (Integer JavaDoc) getXValue(series, item);
249         return getXSymbolicValue(intValue);
250     }
251
252     /**
253      * Returns the Y symbolic value of the data set specified by
254      * <CODE>series</CODE> and <CODE>item</CODE> parameters.
255      *
256      * @param series value of the serie.
257      * @param item value of the item.
258      * @return the symbolic value.
259      */

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

272     public String JavaDoc getXSymbolicValue(Integer JavaDoc val) {
273         return xSymbolicValues[val.intValue()];
274     }
275
276     /**
277      * Returns the Y symbolic value linked with the specified
278      * <CODE>Integer</CODE>.
279      *
280      * @param val value of the integer linked with the symbolic value.
281      * @return the symbolic value.
282      */

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

303     public static String JavaDoc[] combineYSymbolicDataset(YisSymbolic dataset1, YisSymbolic dataset2) {
304
305         SampleXYSymbolicDataset sDataset1 = (SampleXYSymbolicDataset) dataset1;
306         SampleXYSymbolicDataset sDataset2 = (SampleXYSymbolicDataset) dataset2;
307         String JavaDoc[] sDatasetSymbolicValues1 = sDataset1.getYSymbolicValues();
308         String JavaDoc[] sDatasetSymbolicValues2 = sDataset2.getYSymbolicValues();
309
310         //Combine the two list of symbolic value of the two data set
311
int s1length = sDatasetSymbolicValues1.length;
312         int s2length = sDatasetSymbolicValues2.length;
313         List JavaDoc ySymbolicValuesCombined = new Vector JavaDoc();
314         for (int i = 0; i < s1length; i++) {
315             ySymbolicValuesCombined.add(sDatasetSymbolicValues1[i]);
316         }
317         for (int i = 0; i < s2length; i++) {
318             if (!ySymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) {
319                 ySymbolicValuesCombined.add(sDatasetSymbolicValues2[i]);
320             }
321         }
322
323         //Change the Integer reference of the second data set
324
int newIndex;
325         for (int i = 0; i < sDataset2.getSeriesCount(); i++) {
326             for (int j = 0; j < sDataset2.getItemCount(i); j++) {
327                 newIndex = ySymbolicValuesCombined.indexOf(sDataset2.getYSymbolicValue(i, j));
328                 sDataset2.setYValue(i, j, new Integer JavaDoc(newIndex));
329             }
330         }
331
332         //Set the new list of symbolic value on the two data sets
333
String JavaDoc[] ySymbolicValuesCombinedA = new String JavaDoc[ySymbolicValuesCombined.size()];
334         ySymbolicValuesCombined.toArray(ySymbolicValuesCombinedA);
335         sDataset1.setYSymbolicValues(ySymbolicValuesCombinedA);
336         sDataset2.setYSymbolicValues(ySymbolicValuesCombinedA);
337
338         return ySymbolicValuesCombinedA;
339     }
340
341     /**
342      * This function modify <CODE>dataset1</CODE> and <CODE>dataset1</CODE> in
343      * order that they share the same X symbolic value list.
344      * <P>
345      * The sharing X symbolic value list is obtained adding the X symbolic data
346      * list of the fist data set to the X symbolic data list of the second data
347      * set.
348      * <P>
349      * This function is use with the <I>combined plot</I> functions of
350      * JFreeChart.
351      *
352      * @param dataset1 the first data set to combine.
353      * @param dataset2 the second data set to combine.
354      *
355      * @return the shared X symbolic array.
356      */

357     public static String JavaDoc[] combineXSymbolicDataset(XisSymbolic dataset1, XisSymbolic dataset2) {
358         SampleXYSymbolicDataset sDataset1 = (SampleXYSymbolicDataset) dataset1;
359         SampleXYSymbolicDataset sDataset2 = (SampleXYSymbolicDataset) dataset2;
360         String JavaDoc[] sDatasetSymbolicValues1 = sDataset1.getXSymbolicValues();
361         String JavaDoc[] sDatasetSymbolicValues2 = sDataset2.getXSymbolicValues();
362
363         //Combine the two list of symbolic value of the two data set
364
int s1length = sDatasetSymbolicValues1.length;
365         int s2length = sDatasetSymbolicValues2.length;
366         List JavaDoc xSymbolicValuesCombined = new Vector JavaDoc();
367         for (int i = 0; i < s1length; i++) {
368             xSymbolicValuesCombined.add(sDatasetSymbolicValues1[i]);
369         }
370         for (int i = 0; i < s2length; i++) {
371             if (!xSymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) {
372                 xSymbolicValuesCombined.add(sDatasetSymbolicValues2[i]);
373             }
374         }
375
376         //Change the Integer reference of the second data set
377
int newIndex;
378         for (int i = 0; i < sDataset2.getSeriesCount(); i++) {
379             for (int j = 0; j < sDataset2.getItemCount(i); j++) {
380                 newIndex = xSymbolicValuesCombined.indexOf(sDataset2.getXSymbolicValue(i, j));
381                 sDataset2.setXValue(i, j, new Integer JavaDoc(newIndex));
382             }
383         }
384
385         //Set the new list of symbolic value on the two data sets
386
String JavaDoc[] xSymbolicValuesCombinedA = new String JavaDoc[xSymbolicValuesCombined.size()];
387         xSymbolicValuesCombined.toArray(xSymbolicValuesCombinedA);
388         sDataset1.setXSymbolicValues(xSymbolicValuesCombinedA);
389         sDataset2.setXSymbolicValues(xSymbolicValuesCombinedA);
390
391         return xSymbolicValuesCombinedA;
392     }
393
394     /**
395      * Clone the SampleXYSymbolicDataset object
396      *
397      * @return the cloned object.
398      */

399     public Object JavaDoc clone() {
400         String JavaDoc nDatasetName = new String JavaDoc(this.datasetName);
401         Integer JavaDoc[][] nXValues = (Integer JavaDoc[][]) cloneArray(this.xValues);
402         Integer JavaDoc[][] nYValues = (Integer JavaDoc[][]) cloneArray(this.yValues);
403         String JavaDoc[] nXSymbolicValues = (String JavaDoc[]) cloneArray(this.xSymbolicValues);
404         String JavaDoc[] nYSymbolicValues = (String JavaDoc[]) cloneArray(this.ySymbolicValues);
405         String JavaDoc[] seriesName = (String JavaDoc[]) cloneArray(this.seriesName);
406         return new SampleXYSymbolicDataset(nDatasetName, nXValues, nYValues,
407                                            nXSymbolicValues, nYSymbolicValues, seriesName);
408     }
409
410     /**
411      * Returns a clone of the array.
412      *
413      * @param arr the array.
414      *
415      * @return a clone.
416      */

417     private static Object JavaDoc cloneArray(Object JavaDoc arr) {
418
419         if (arr == null) {
420             return arr;
421         }
422
423         Class JavaDoc cls = arr.getClass();
424         if (!cls.isArray()) {
425             return arr;
426         }
427
428         int length = Array.getLength(arr);
429         Object JavaDoc[] newarr = (Object JavaDoc[]) Array.newInstance(cls.getComponentType(), length);
430
431         Object JavaDoc obj;
432
433         for (int i = 0; i < length; i++) {
434             obj = Array.get(arr, i);
435             if (obj.getClass().isArray()) {
436                 newarr[i] = cloneArray(obj);
437             }
438             else {
439                 newarr[i] = obj;
440             }
441         }
442
443         return newarr;
444     }
445
446 }
447
Popular Tags