KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SymbolicXYPlotDemo.java
24  * -----------------------
25  * (C) Copyright 2002, 2003, by Anthony Boulestreau and Contributors.
26  *
27  * Original Author: Anthony Boulestreau;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  *
31  * Changes
32  * -------
33  * 29-Mar-2002 : Version 1 (AB);
34  * 09-Apr-2002 : Minor changes reflecting the API change for XYItemRenderer (DG);
35  * 23-Apr-2002 : Updated to reflect changes in the combined plot classes (DG);
36  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.awt.Color JavaDoc;
43 import java.awt.GradientPaint JavaDoc;
44 import java.lang.reflect.Array JavaDoc;
45
46 import javax.swing.JFrame JavaDoc;
47
48 import org.jfree.chart.ChartFrame;
49 import org.jfree.chart.JFreeChart;
50 import org.jfree.chart.axis.SymbolicAxis;
51 import org.jfree.chart.labels.SymbolicXYToolTipGenerator;
52 import org.jfree.chart.plot.CombinedDomainXYPlot;
53 import org.jfree.chart.plot.CombinedRangeXYPlot;
54 import org.jfree.chart.plot.XYPlot;
55 import org.jfree.chart.renderer.StandardXYItemRenderer;
56 import org.jfree.chart.renderer.XYItemRenderer;
57 import org.jfree.data.CombinedDataset;
58 import org.jfree.data.SubSeriesDataset;
59 import org.jfree.data.XYDataset;
60 import org.jfree.data.XisSymbolic;
61 import org.jfree.data.YisSymbolic;
62 import org.jfree.ui.RefineryUtilities;
63
64 /**
65  * A demonstration application for the symbolic XY plots.
66  *
67  * @author Anthony Boulestreau
68  */

69 public class SymbolicXYPlotDemo {
70
71     /**
72      * Creates a XY graph with symbolic value on X and Y axis.
73      *
74      * @param title the title.
75      * @param xAxisLabel the x axis label.
76      * @param yAxisLabel the y axis label.
77      * @param data the dataset.
78      * @param legend create a legend?
79      *
80      * @return a chart.
81      */

82     public static JFreeChart createXYSymbolicPlot(String JavaDoc title, String JavaDoc xAxisLabel,
83                                                   String JavaDoc yAxisLabel, XYDataset data,
84                                                   boolean legend) {
85
86         SymbolicAxis xSymbolicAxis
87             = new SymbolicAxis(xAxisLabel, ((XisSymbolic) data).getXSymbolicValues());
88
89         SymbolicAxis ySymbolicAxis
90             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data).getYSymbolicValues());
91
92         XYPlot plot = new XYPlot(data, xSymbolicAxis, ySymbolicAxis, null);
93         XYItemRenderer renderer
94             = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES,
95                                          new SymbolicXYToolTipGenerator());
96         plot.setRenderer(renderer);
97         JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
98         return chart;
99
100     }
101
102     /**
103      * Creates a sample dataset.
104      *
105      * @return a dataset.
106      */

107     public static SampleYSymbolicDataset createYSymbolicSample1() {
108
109         String JavaDoc[] sData = {"Lion", "Elephant", "Monkey", "Hippopotamus", "Giraffe"};
110         SampleYSymbolicDataset data = new SampleYSymbolicDataset("AY Sample", 20, sData, 4, 20,
111             new String JavaDoc[] {"Fall", "Spring", "Summer", "Winter"});
112         return data;
113
114     }
115
116     /**
117      * Creates a sample dataset.
118      *
119      * @return a dataset.
120      */

121     public static SampleYSymbolicDataset createYSymbolicSample2() {
122
123         String JavaDoc[] sData = {"Giraffe", "Gazelle", "Zebra", "Gnu"};
124         SampleYSymbolicDataset data = new SampleYSymbolicDataset("BY Sample", 40, sData, 4, 10,
125             new String JavaDoc[] {"Fall", "Spring", "Summer", "Winter"});
126         return data;
127
128     }
129
130     /**
131      * Creates a sample dataset.
132      *
133      * @return a dataset.
134      */

135     public static SampleXYSymbolicDataset createXYSymbolicSample1() {
136
137         String JavaDoc[] xsData = {"Atmosphere", "Continental Ecosystem",
138                            "Limnic Ecosystem", "Marine Ecosystem"};
139         String JavaDoc[] ysData = {"Ionizing radiations", "Thermic pollutants", "Hydrocarbon",
140                            "Synthetic materials", "Pesticides", "Detergent",
141                            "Synthetic organic materials", "Sulphur", "Nitrate", "Phosphate",
142                            "Heavy metals", "Fluors", "Aerosols", "Dead organic materials",
143                            "Pathogen micro-organisms"};
144
145         int[][] xd = {{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
146                        2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
147         int[][] yd = {{0, 2, 3, 7, 10, 11, 12, 14, 0, 2, 3, 4, 7, 8, 9, 10, 11, 12, 14, 0, 1, 2, 3,
148                        4, 5, 7, 8, 9, 10, 13, 14, 0, 1, 2, 3, 4, 5, 8, 9, 10, 13, 14}};
149         Integer JavaDoc[][] xData = (Integer JavaDoc[][]) toArray(xd);
150         Integer JavaDoc[][] yData = (Integer JavaDoc[][]) toArray(yd);
151         SampleXYSymbolicDataset xySymbolicData = new SampleXYSymbolicDataset("AXY Sample",
152             xData, yData, xsData, ysData, new String JavaDoc[] {"A"});
153         return xySymbolicData;
154
155     }
156
157     /**
158      * Creates a sample dataset.
159      *
160      * @return a dataset.
161      */

162     public static SampleXYSymbolicDataset createXYSymbolicSample2() {
163
164         String JavaDoc[] xsData = {"Physic pollutant", "Chemical pollutant", "Biological pollutant"};
165         String JavaDoc[] ysData = {"Ionizing radiations", "Thermic pollutants", "Hydrocarbon",
166                             "Synthetic materials", "Pesticides", "Detergent",
167                             "Synthetic organic materials", "Sulphur", "Nitrate", "Phosphate",
168                             "Heavy metals", "Fluors", "Aerosols", "Dead organic materials",
169                             "Pathogen micro-organisms"};
170
171         int[][] xd = {{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2}};
172         int[][] yd = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}};
173         Integer JavaDoc[][] xData = (Integer JavaDoc[][]) toArray(xd);
174         Integer JavaDoc[][] yData = (Integer JavaDoc[][]) toArray(yd);
175         SampleXYSymbolicDataset xySymbolicData = new SampleXYSymbolicDataset("BXY Sample",
176             xData, yData, xsData, ysData, new String JavaDoc[] {"B"});
177         return xySymbolicData;
178
179     }
180
181     /**
182      * Displays an XYPlot with symbolic axes.
183      *
184      * @param frameTitle the frame title.
185      * @param data the dataset.
186      * @param chartTitle the chart title.
187      * @param xAxisLabel the x axis label.
188      * @param yAxisLabel the y axis label.
189      */

190     private static void displayXYSymbolic(String JavaDoc frameTitle,
191                                           XYDataset data, String JavaDoc chartTitle,
192                                           String JavaDoc xAxisLabel, String JavaDoc yAxisLabel) {
193
194         JFreeChart chart = createXYSymbolicPlot(chartTitle, xAxisLabel, yAxisLabel, data, true);
195         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 1000, 0, Color.green));
196         JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
197         frame.pack();
198         RefineryUtilities.positionFrameRandomly(frame);
199         frame.show();
200
201     }
202
203    /**
204     * Displays an overlaid XYPlot with X and Y symbolic data.
205     *
206     * @param frameTitle the frame title.
207     * @param data1 the dataset 1.
208     * @param data2 the dataset 2.
209     */

210     private static void displayXYSymbolicOverlaid(String JavaDoc frameTitle,
211                                                   XYDataset data1, XYDataset data2) {
212
213         String JavaDoc title = "Pollutant Overlaid";
214         String JavaDoc xAxisLabel = "Contamination and Type";
215         String JavaDoc yAxisLabel = "Pollutant";
216
217         // combine the x symbolic values of the two data sets
218
String JavaDoc[] combinedXSymbolicValues
219             = SampleXYSymbolicDataset.combineXSymbolicDataset((XisSymbolic) data1,
220                                                               (XisSymbolic) data2);
221
222         // combine the y symbolic values of the two data sets
223
String JavaDoc[] combinedYSymbolicValues
224             = SampleXYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1,
225                                                               (YisSymbolic) data2);
226
227         // make master dataset...
228
CombinedDataset data = new CombinedDataset();
229         data.add(data1);
230         data.add(data2);
231
232         // decompose data...
233
XYDataset series0 = new SubSeriesDataset(data, 0);
234         XYDataset series1 = new SubSeriesDataset(data, 1);
235
236         // create overlaid plot...
237
SymbolicAxis hsymbolicAxis = new SymbolicAxis(xAxisLabel, combinedXSymbolicValues);
238         SymbolicAxis vsymbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues);
239
240         XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
241         XYPlot plot = new XYPlot(series0, hsymbolicAxis, vsymbolicAxis, renderer1);
242
243         XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
244         plot.setSecondaryDataset(0, series1);
245         plot.setSecondaryRenderer(0, renderer2);
246
247         // make the chart...
248
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
249         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
250
251         // and present it in a frame...
252
JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
253         frame.pack();
254         RefineryUtilities.positionFrameRandomly(frame);
255         frame.show();
256
257     }
258
259     /**
260      * Displays an horizontally combined XYPlot with X and Y symbolic data.
261      *
262      * @param frameTitle the frame title.
263      * @param data1 the dataset 1.
264      * @param data2 the dataset 2.
265      */

266     private static void displayXYSymbolicCombinedHorizontally(String JavaDoc frameTitle,
267                                                               XYDataset data1, XYDataset data2) {
268
269         String JavaDoc title = "Pollutant Horizontally Combined";
270         String JavaDoc x1AxisLabel = "Contamination";
271         String JavaDoc x2AxisLabel = "Type";
272         String JavaDoc yAxisLabel = "Pollutant";
273
274         // combine the y symbolic values of the two data sets
275
String JavaDoc[] combinedYSymbolicValues =
276             SampleXYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1,
277                                                             (YisSymbolic) data2);
278
279         // make master dataset...
280
CombinedDataset data = new CombinedDataset();
281         data.add(data1);
282         data.add(data2);
283
284         // decompose data...
285
XYDataset series0 = new SubSeriesDataset(data, 0);
286         XYDataset series1 = new SubSeriesDataset(data, 1);
287
288         JFreeChart chart = null;
289
290         // common horizontal and vertical axes
291
SymbolicAxis hsymbolicAxis0
292             = new SymbolicAxis(x1AxisLabel, ((XisSymbolic) data1).getXSymbolicValues());
293         SymbolicAxis hsymbolicAxis1
294             = new SymbolicAxis(x2AxisLabel, ((XisSymbolic) data2).getXSymbolicValues());
295         SymbolicAxis symbolicAxis
296             = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues);
297
298         // create main plot...
299
CombinedRangeXYPlot mainPlot = new CombinedRangeXYPlot(symbolicAxis);
300
301         // add subplots...
302
XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
303         XYPlot subplot0 = new XYPlot(series0, hsymbolicAxis0, null, renderer);
304         XYPlot subplot1 = new XYPlot(series1, hsymbolicAxis1, null, renderer);
305         mainPlot.add(subplot0, 1);
306         mainPlot.add(subplot1, 1);
307
308         // make the top level JFreeChart object
309
chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
310         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
311
312         // and present it in a frame...
313
JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
314         frame.pack();
315         RefineryUtilities.positionFrameRandomly(frame);
316         frame.show();
317
318     }
319
320     // ****************************************************************************
321
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
322
// * Please note that commercial support and documentation is available from: *
323
// * *
324
// * http://www.object-refinery.com/jfreechart/support.html *
325
// * *
326
// * This is not only a great service for developers, but is a VERY IMPORTANT *
327
// * source of funding for the JFreeChart project. Please support us so that *
328
// * we can continue developing free software. *
329
// ****************************************************************************
330

331     /**
332      * Vertically combined sample1 and sample2 and display it.
333      *
334      * @param frameTitle the frame title.
335      * @param data1 the dataset 1.
336      * @param data2 the dataset 2.
337      */

338     private static void displayXYSymbolicCombinedVertically(String JavaDoc frameTitle,
339                                                             XYDataset data1, XYDataset data2) {
340
341         String JavaDoc title = "Pollutant Vertically Combined";
342         String JavaDoc xAxisLabel = "Contamination and Type";
343         String JavaDoc yAxisLabel = "Pollutant";
344
345         // combine the x symbolic values of the two data sets
346
String JavaDoc[] combinedXSymbolicValues
347             = SampleXYSymbolicDataset.combineXSymbolicDataset((XisSymbolic) data1,
348                                                               (XisSymbolic) data2);
349
350         // make master dataset...
351
CombinedDataset data = new CombinedDataset();
352         data.add(data1);
353         data.add(data2);
354
355         // decompose data...
356
XYDataset series0 = new SubSeriesDataset(data, 0);
357         XYDataset series1 = new SubSeriesDataset(data, 1);
358
359         // common horizontal and vertical axes
360
SymbolicAxis hsymbolicAxis
361             = new SymbolicAxis(xAxisLabel, combinedXSymbolicValues);
362
363         SymbolicAxis vsymbolicAxis0
364             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data1).getYSymbolicValues());
365
366         SymbolicAxis vsymbolicAxis1
367             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data2).getYSymbolicValues());
368
369         // create the main plot...
370
CombinedDomainXYPlot mainPlot = new CombinedDomainXYPlot(hsymbolicAxis);
371
372         // add the sub-plots...
373
XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
374         XYPlot subplot0 = new XYPlot(series0, null, vsymbolicAxis0, renderer);
375         XYPlot subplot1 = new XYPlot(series1, null, vsymbolicAxis1, renderer);
376
377         mainPlot.add(subplot0, 1);
378         mainPlot.add(subplot1, 1);
379
380         // make the chart...
381
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
382         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
383
384         // and present it in a frame...
385
JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
386         frame.pack();
387         RefineryUtilities.positionFrameRandomly(frame);
388         frame.show();
389
390     }
391
392     /**
393      * Transform an primitive array to an object array.
394      *
395      * @param arr the array.
396      *
397      * @return an array.
398      */

399     private static Object JavaDoc toArray(Object JavaDoc arr) {
400
401         if (arr == null) {
402             return arr;
403         }
404
405         Class JavaDoc cls = arr.getClass();
406         if (!cls.isArray()) {
407             return arr;
408         }
409
410         Class JavaDoc compType = cls.getComponentType();
411         int dim = 1;
412         while (!compType.isPrimitive()) {
413             if (!compType.isArray()) {
414                 return arr;
415             }
416             else {
417                 dim++;
418                 compType = compType.getComponentType();
419             }
420         }
421
422         int[] length = new int[dim];
423         length[0] = Array.getLength(arr);
424         Object JavaDoc[] newarr = null;
425
426         try {
427             if (compType.equals(Integer.TYPE)) {
428                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Integer"), length);
429             }
430             else if (compType.equals(Double.TYPE)) {
431                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Double"), length);
432             }
433             else if (compType.equals(Long.TYPE)) {
434                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Long"), length);
435             }
436             else if (compType.equals(Float.TYPE)) {
437                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Float"), length);
438             }
439             else if (compType.equals(Short.TYPE)) {
440                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Short"), length);
441             }
442             else if (compType.equals(Byte.TYPE)) {
443                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Byte"), length);
444             }
445             else if (compType.equals(Character.TYPE)) {
446                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Character"), length);
447             }
448             else if (compType.equals(Boolean.TYPE)) {
449                 newarr = (Object JavaDoc[]) Array.newInstance(Class.forName("java.lang.Boolean"), length);
450             }
451         }
452         catch (ClassNotFoundException JavaDoc ex) {
453             System.out.println(ex);
454         }
455
456         for (int i = 0; i < length[0]; i++) {
457             if (dim != 1) {
458                 newarr[i] = toArray(Array.get(arr, i));
459             }
460             else {
461                 newarr[i] = Array.get(arr, i);
462             }
463         }
464         return newarr;
465     }
466
467     /**
468      * Starting point for the application.
469      *
470      * @param args ignored.
471      */

472     public static void main(String JavaDoc[] args) {
473
474         SampleXYSymbolicDataset s1 = createXYSymbolicSample1();
475         SampleXYSymbolicDataset s2 = createXYSymbolicSample2();
476
477         displayXYSymbolic("Example 1", s1, "Pollutant", "contamination", "pollutant");
478
479         displayXYSymbolic("Example 2", s2, "Pollutant", "type", "pollutant");
480
481         displayXYSymbolicCombinedHorizontally("Example 3", (SampleXYSymbolicDataset) s1.clone(),
482                                                            (SampleXYSymbolicDataset) s2.clone());
483
484         displayXYSymbolicCombinedVertically("Example 4", (SampleXYSymbolicDataset) s1.clone(),
485                                                          (SampleXYSymbolicDataset) s2.clone());
486
487         displayXYSymbolicOverlaid("Example 5", (SampleXYSymbolicDataset) s1.clone(),
488                                                (SampleXYSymbolicDataset) s2.clone());
489
490     }
491
492 }
493
Popular Tags