KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BoxAndWhiskerDemo.java
24  * ----------------------
25  * (C) Copyright 2003, by David Browning and Contributors.
26  *
27  * Original Author: David Browning (for the Australian Institute of Marine Science);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: BoxAndWhiskerDemo.java,v 1.3 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 21-Aug-2003 : Version 1, contributed by David Browning (for the Australian Institute of
35  * Marine Science);
36  * 27-Aug-2003 : Renamed BoxAndWhiskerCategoryDemo --> BoxAndWhiskerDemo, moved dataset creation
37  * into the demo (DG);
38  *
39  */

40
41 package org.jfree.chart.demo;
42
43 import java.awt.Font JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46
47 import org.jfree.chart.ChartPanel;
48 import org.jfree.chart.JFreeChart;
49 import org.jfree.chart.axis.CategoryAxis;
50 import org.jfree.chart.axis.NumberAxis;
51 import org.jfree.chart.plot.CategoryPlot;
52 import org.jfree.chart.renderer.BoxAndWhiskerRenderer;
53 import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
54 import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * Demonstration of a box-and-whisker chart using a {@link CategoryPlot}.
60  *
61  * @author David Browning
62  */

63 public class BoxAndWhiskerDemo extends ApplicationFrame {
64
65     /**
66      * Creates a new demo.
67      *
68      * @param title the frame title.
69      */

70     public BoxAndWhiskerDemo(String JavaDoc title) {
71
72         super(title);
73         
74         BoxAndWhiskerCategoryDataset dataset = createSampleDataset();
75
76         CategoryAxis xAxis = new CategoryAxis("Type");
77         NumberAxis yAxis = new NumberAxis("Value");
78         yAxis.setAutoRangeIncludesZero(false);
79         BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
80         CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
81
82         JFreeChart chart = new JFreeChart(
83             "Box-and-Whisker Demo",
84             new Font JavaDoc("SansSerif", Font.BOLD, 14),
85             plot,
86             true
87         );
88         
89         ChartPanel chartPanel = new ChartPanel(chart);
90         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(450, 270));
91         setContentPane(chartPanel);
92
93     }
94
95     /**
96      * Creates a sample dataset.
97      *
98      * @return A sample dataset.
99      */

100     private BoxAndWhiskerCategoryDataset createSampleDataset() {
101         
102         final int SERIES_COUNT = 3;
103         final int CATEGORY_COUNT = 4;
104         final int ENTITY_COUNT = 22;
105         
106         DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
107         for (int i = 0; i < SERIES_COUNT; i++) {
108             for (int j = 0; j < CATEGORY_COUNT; j++) {
109                 List JavaDoc list = new ArrayList JavaDoc();
110                 // add some values...
111
for (int k = 0; k < ENTITY_COUNT; k++) {
112                     double value1 = 10.0 + Math.random() * 3;
113                     list.add(new Double JavaDoc(value1));
114                     double value2 = 11.25 + Math.random(); // concentrate values in the middle
115
list.add(new Double JavaDoc(value2));
116                 }
117                 dataset.add(list, "Series " + i, " Type " + j);
118             }
119         }
120
121         return dataset;
122     }
123
124     // ****************************************************************************
125
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
126
// * Please note that commercial support and documentation is available from: *
127
// * *
128
// * http://www.object-refinery.com/jfreechart/support.html *
129
// * *
130
// * This is not only a great service for developers, but is a VERY IMPORTANT *
131
// * source of funding for the JFreeChart project. Please support us so that *
132
// * we can continue developing free software. *
133
// ****************************************************************************
134

135     /**
136      * For testing from the command line.
137      *
138      * @param args ignored.
139      */

140     public static void main(String JavaDoc[] args) {
141
142         BoxAndWhiskerDemo demo = new BoxAndWhiskerDemo("Box-and-Whisker Chart Demo");
143         demo.pack();
144         RefineryUtilities.centerFrameOnScreen(demo);
145         demo.setVisible(true);
146
147     }
148
149 }
150
Popular Tags