KickJava   Java API By Example, From Geeks To Geeks.

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


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 program is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation;
12  * either version 2 of the License, or (at your option) any later version.
13  *
14  * This program 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with this program;
19  * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA.
21  *
22  * ----------------------
23  * SymbolicYPlotDemo.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  * Changes
31  * -------
32  * 29-Mar-2002 : Version 1 (AB);
33  * 23-Apr-2002 : Updated to reflect revisions in combined plot classes (DG);
34  * 25-Jun-2002 : Removed unnecessary imports (DG);
35  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.awt.GradientPaint JavaDoc;
43
44 import javax.swing.JFrame JavaDoc;
45
46 import org.jfree.chart.ChartFrame;
47 import org.jfree.chart.JFreeChart;
48 import org.jfree.chart.axis.NumberAxis;
49 import org.jfree.chart.axis.SymbolicAxis;
50 import org.jfree.chart.axis.ValueAxis;
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.YisSymbolic;
61 import org.jfree.ui.RefineryUtilities;
62
63 /**
64  * A demonstration application for the symbolic axis plots.
65  *
66  * @author Anthony Boulestreau
67  */

68 public class SymbolicYPlotDemo {
69
70     // ****************************************************************************
71
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
72
// * Please note that commercial support and documentation is available from: *
73
// * *
74
// * http://www.object-refinery.com/jfreechart/support.html *
75
// * *
76
// * This is not only a great service for developers, but is a VERY IMPORTANT *
77
// * source of funding for the JFreeChart project. Please support us so that *
78
// * we can continue developing free software. *
79
// ****************************************************************************
80

81     /**
82      * Displays an XYPlot with Y symbolic data.
83      *
84      * @param frameTitle the frame title.
85      * @param data the data.
86      * @param chartTitle the chart title.
87      * @param xAxisLabel the x-axis label.
88      * @param yAxisLabel the y-axis label.
89      */

90     private static void displayYSymbolic(String JavaDoc frameTitle,
91                                          XYDataset data, String JavaDoc chartTitle,
92                                          String JavaDoc xAxisLabel, String JavaDoc yAxisLabel) {
93
94         JFreeChart chart = createYSymbolicPlot(chartTitle, xAxisLabel, yAxisLabel, data, true);
95         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 1000, 0, Color.green));
96
97         JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
98         frame.pack();
99         RefineryUtilities.positionFrameRandomly(frame);
100         frame.show();
101
102     }
103
104     /**
105      * Create and display an overlaid chart.
106      *
107      * @param frameTitle the frame title.
108      * @param data1 dataset1.
109      * @param data2 dataset2.
110      */

111     private static void displayYSymbolicOverlaid(String JavaDoc frameTitle,
112                                                  XYDataset data1, XYDataset data2) {
113
114         String JavaDoc title = "Animals Overlaid";
115         String JavaDoc xAxisLabel = "Miles";
116         String JavaDoc yAxisLabel = "Animal";
117
118         // combine the y symbolic values of the two data sets...
119
String JavaDoc[] combinedYSymbolicValues
120             = SampleYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1,
121                                                              (YisSymbolic) data2);
122
123         // make master dataset...
124
CombinedDataset data = new CombinedDataset();
125         data.add(data1);
126         data.add(data2);
127
128         // decompose data...
129
XYDataset series0 = new SubSeriesDataset(data, 0);
130         XYDataset series1 = new SubSeriesDataset(data, 1);
131         XYDataset series2 = new SubSeriesDataset(data, 2);
132         XYDataset series3 = new SubSeriesDataset(data, 3);
133         XYDataset series4 = new SubSeriesDataset(data, 4);
134         XYDataset series5 = new SubSeriesDataset(data, 5);
135         XYDataset series6 = new SubSeriesDataset(data, 6);
136         XYDataset series7 = new SubSeriesDataset(data, 7);
137
138         // create main plot...
139
ValueAxis valueAxis = new NumberAxis(xAxisLabel);
140         SymbolicAxis symbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues);
141         XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
142         XYPlot plot = new XYPlot(series0, valueAxis, symbolicAxis, renderer);
143
144         plot.setSecondaryDataset(0, series1);
145         XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
146         plot.setSecondaryRenderer(0, renderer1);
147
148         plot.setSecondaryDataset(1, series2);
149         XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
150         plot.setSecondaryRenderer(1, renderer2);
151         
152         plot.setSecondaryDataset(2, series3);
153         XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
154         plot.setSecondaryRenderer(2, renderer3);
155         
156         plot.setSecondaryDataset(3, series4);
157         XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
158         plot.setSecondaryRenderer(3, renderer4);
159         
160         plot.setSecondaryDataset(4, series5);
161         XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
162         plot.setSecondaryRenderer(4, renderer5);
163         
164         plot.setSecondaryDataset(5, series6);
165         XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
166         plot.setSecondaryRenderer(5, renderer6);
167         
168         plot.setSecondaryDataset(6, series7);
169         XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
170         plot.setSecondaryRenderer(6, renderer7);
171
172         // make the chart...
173
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
174         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
175
176         // and present it in a frame...
177
JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
178         frame.pack();
179         RefineryUtilities.positionFrameRandomly(frame);
180         frame.show();
181
182     }
183
184     /**
185      * Create and display a multi XY plot with horizontal layout.
186      *
187      * @param frameTitle the frame title.
188      * @param data1 dataset1.
189      * @param data2 dataset2.
190      */

191     private static void displayYSymbolicCombinedHorizontally(String JavaDoc frameTitle,
192                                                              SampleYSymbolicDataset data1,
193                                                              SampleYSymbolicDataset data2) {
194
195         String JavaDoc title = "Animals Horizontally Combined";
196         String JavaDoc xAxisLabel = "Miles";
197         String JavaDoc yAxisLabel = null;
198
199         // combine the y symbolic values of the two data sets
200
String JavaDoc[] combinedYSymbolicValues
201             = SampleYSymbolicDataset.combineYSymbolicDataset(data1, data2);
202
203         // make master dataset...
204
CombinedDataset data = new CombinedDataset();
205         data.add(data1);
206         data.add(data2);
207
208         // decompose data...
209
XYDataset series0 = new SubSeriesDataset(data, 0);
210         XYDataset series1 = new SubSeriesDataset(data, 1);
211         XYDataset series2 = new SubSeriesDataset(data, 2);
212         XYDataset series3 = new SubSeriesDataset(data, 3);
213         XYDataset series4 = new SubSeriesDataset(data, 4);
214         XYDataset series5 = new SubSeriesDataset(data, 5);
215         XYDataset series6 = new SubSeriesDataset(data, 6);
216         XYDataset series7 = new SubSeriesDataset(data, 7);
217
218         // create axes...
219
ValueAxis valueAxis0 = new NumberAxis(xAxisLabel);
220         ValueAxis valueAxis1 = new NumberAxis(xAxisLabel);
221         ValueAxis valueAxis2 = new NumberAxis(xAxisLabel);
222         ValueAxis valueAxis3 = new NumberAxis(xAxisLabel);
223         ValueAxis valueAxis4 = new NumberAxis(xAxisLabel);
224         ValueAxis valueAxis5 = new NumberAxis(xAxisLabel);
225         ValueAxis valueAxis6 = new NumberAxis(xAxisLabel);
226         ValueAxis valueAxis7 = new NumberAxis(xAxisLabel);
227         SymbolicAxis symbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues);
228
229         // make a combined plot
230
CombinedRangeXYPlot mainPlot = new CombinedRangeXYPlot(symbolicAxis);
231
232         // add the sub-plots
233
XYItemRenderer renderer0 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
234         XYPlot subplot0 = new XYPlot(series0, valueAxis0, null, renderer0);
235         XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
236         XYPlot subplot1 = new XYPlot(series1, valueAxis1, null, renderer1);
237         XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
238         XYPlot subplot2 = new XYPlot(series2, valueAxis2, null, renderer2);
239         XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
240         XYPlot subplot3 = new XYPlot(series3, valueAxis3, null, renderer3);
241         XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
242         XYPlot subplot4 = new XYPlot(series4, valueAxis4, null, renderer4);
243         XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
244         XYPlot subplot5 = new XYPlot(series5, valueAxis5, null, renderer5);
245         XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
246         XYPlot subplot6 = new XYPlot(series6, valueAxis6, null, renderer6);
247         XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
248         XYPlot subplot7 = new XYPlot(series7, valueAxis7, null, renderer7);
249
250         mainPlot.add(subplot0, 1);
251         mainPlot.add(subplot1, 1);
252         mainPlot.add(subplot2, 1);
253         mainPlot.add(subplot3, 1);
254         mainPlot.add(subplot4, 1);
255         mainPlot.add(subplot5, 1);
256         mainPlot.add(subplot6, 1);
257         mainPlot.add(subplot7, 1);
258
259         // make the top level JFreeChart object
260
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
261
262         // then customise it a little...
263
chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
264
265         // and present it in a frame...
266
JFrame JavaDoc ySymbolicFrame = new ChartFrame(frameTitle, chart);
267         ySymbolicFrame.pack();
268         RefineryUtilities.positionFrameRandomly(ySymbolicFrame);
269         ySymbolicFrame.show();
270
271     }
272
273     /**
274      * Displays a vertically combined symbolic plot.
275      *
276      * @param frameTitle the frame title.
277      * @param data1 dataset 1.
278      * @param data2 dataset 2.
279      */

280     private static void displayYSymbolicCombinedVertically(String JavaDoc frameTitle,
281                                                            SampleYSymbolicDataset data1,
282                                                            SampleYSymbolicDataset data2) {
283
284         String JavaDoc title = "Animals Vertically Combined";
285         String JavaDoc xAxisLabel = "Miles";
286         String JavaDoc yAxisLabel = null;
287
288         // create master dataset...
289
CombinedDataset data = new CombinedDataset();
290         data.add(data1);
291         data.add(data2);
292
293         // decompose data...
294
XYDataset series0 = new SubSeriesDataset(data, 0);
295         XYDataset series1 = new SubSeriesDataset(data, 1);
296         XYDataset series2 = new SubSeriesDataset(data, 2);
297         XYDataset series3 = new SubSeriesDataset(data, 3);
298         XYDataset series4 = new SubSeriesDataset(data, 4);
299         XYDataset series5 = new SubSeriesDataset(data, 5);
300         XYDataset series6 = new SubSeriesDataset(data, 6);
301         XYDataset series7 = new SubSeriesDataset(data, 7);
302
303         // common horizontal and vertical axes
304
ValueAxis valueAxis = new NumberAxis(xAxisLabel);
305         SymbolicAxis symbolicAxis1
306             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data1).getYSymbolicValues());
307         SymbolicAxis symbolicAxis2
308             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data2).getYSymbolicValues());
309
310         // create the main plot...
311
CombinedDomainXYPlot mainPlot = new CombinedDomainXYPlot(valueAxis);
312
313         // and the sub-plots...
314
XYItemRenderer renderer0 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
315         XYPlot subplot0 = new XYPlot(series0, null, symbolicAxis1, renderer0);
316         XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
317         XYPlot subplot1 = new XYPlot(series1, null, symbolicAxis1, renderer1);
318         XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
319         XYPlot subplot2 = new XYPlot(series2, null, symbolicAxis1, renderer2);
320         XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
321         XYPlot subplot3 = new XYPlot(series3, null, symbolicAxis1, renderer3);
322         XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
323         XYPlot subplot4 = new XYPlot(series4, null, symbolicAxis2, renderer4);
324         XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
325         XYPlot subplot5 = new XYPlot(series5, null, symbolicAxis2, renderer5);
326         XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
327         XYPlot subplot6 = new XYPlot(series6, null, symbolicAxis2, renderer6);
328         XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
329         XYPlot subplot7 = new XYPlot(series7, null, symbolicAxis2, renderer7);
330
331         // add the subplots to the main plot...
332
mainPlot.add(subplot0, 1);
333         mainPlot.add(subplot1, 1);
334         mainPlot.add(subplot2, 1);
335         mainPlot.add(subplot3, 1);
336         mainPlot.add(subplot4, 1);
337         mainPlot.add(subplot5, 1);
338         mainPlot.add(subplot6, 1);
339         mainPlot.add(subplot7, 1);
340
341         // construct the chart...
342
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
343         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
344
345         // and present it in a frame...
346
JFrame JavaDoc frame = new ChartFrame(frameTitle, chart);
347         frame.pack();
348         RefineryUtilities.positionFrameRandomly(frame);
349         frame.show();
350
351     }
352
353     /**
354      * Creates a XY graph with symbolic value on Y axis.
355      *
356      * @param title the chart title.
357      * @param xAxisLabel the x-axis label.
358      * @param yAxisLabel the y-axis label.
359      * @param data the data.
360      * @param legend a flag controlling whether or not the legend is created for the chart.
361      *
362      * @return the chart.
363      */

364     public static JFreeChart createYSymbolicPlot(String JavaDoc title, String JavaDoc xAxisLabel,
365                                                  String JavaDoc yAxisLabel, XYDataset data,
366                                                  boolean legend) {
367
368         ValueAxis valueAxis = new NumberAxis(xAxisLabel);
369         SymbolicAxis symbolicAxis
370             = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data).getYSymbolicValues());
371
372         XYPlot plot = new XYPlot(data, valueAxis, symbolicAxis, null);
373         XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES,
374                                                              new SymbolicXYToolTipGenerator());
375         plot.setRenderer(renderer);
376         JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
377         return chart;
378
379     }
380
381     /**
382      * Creates a sample symbolic dataset.
383      *
384      * @return the dataset.
385      */

386     public static SampleYSymbolicDataset createYSymbolicSample1() {
387
388         String JavaDoc[] sData = {"Lion", "Elephant", "Monkey", "Hippopotamus", "Giraffe"};
389         SampleYSymbolicDataset data
390             = new SampleYSymbolicDataset("AY Sample", 20, sData, 4, 20,
391                 new String JavaDoc[] {"A Fall", "A Spring", "A Summer", "A Winter"});
392         return data;
393
394     }
395
396     /**
397      * Creates a sample symbolic dataset.
398      *
399      * @return The dataset.
400      */

401     public static SampleYSymbolicDataset createYSymbolicSample2() {
402
403         String JavaDoc[] sData = {"Giraffe", "Gazelle", "Zebra", "Gnu"};
404         SampleYSymbolicDataset data
405             = new SampleYSymbolicDataset("BY Sample", 40, sData, 4, 20,
406                 new String JavaDoc[] {"B Fall", "B Spring", "B Summer", "B Winter"});
407         return data;
408
409     }
410
411     /**
412      * The starting point for the demonstration application.
413      *
414      * @param args ignored.
415      */

416     public static void main(String JavaDoc[] args) {
417
418         SampleYSymbolicDataset s1 = createYSymbolicSample1();
419         SampleYSymbolicDataset s2 = createYSymbolicSample2();
420
421         displayYSymbolic("Example 1", s1, "Animal A", "Miles", "Animal");
422
423         displayYSymbolic("Example 2", s2, "Animal B", "Miles", "Animal");
424
425         displayYSymbolicCombinedHorizontally("Example 3", (SampleYSymbolicDataset) s1.clone(),
426                                                           (SampleYSymbolicDataset) s2.clone());
427
428         displayYSymbolicCombinedVertically("Example 4", (SampleYSymbolicDataset) s1.clone(),
429                                                         (SampleYSymbolicDataset) s2.clone());
430
431         displayYSymbolicOverlaid("Example 5", (SampleYSymbolicDataset) s1.clone(),
432                                               (SampleYSymbolicDataset) s2.clone());
433     }
434
435 }
436
Popular Tags