KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HistogramDemo.java
24  * ------------------
25  * (C) Copyright 2003, by Jelai Wang and Contributors.
26  *
27  * Original Author: Jelai Wang (jelaiw AT mindspring.com);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: HistogramDemo.java,v 1.3 2003/11/28 10:57:33 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 06-Jul-2003 : Version 1, contributed by Jelai Wang (DG);
35  * 07-Jul-2003 : Modified to display chart on screen, consistent with the other JFreeChart demo
36  * applications (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.io.IOException JavaDoc;
43 import java.util.Random JavaDoc;
44
45 import org.jfree.chart.ChartFactory;
46 import org.jfree.chart.ChartPanel;
47 import org.jfree.chart.JFreeChart;
48 import org.jfree.chart.plot.PlotOrientation;
49 import org.jfree.data.HistogramDataset;
50 import org.jfree.data.IntervalXYDataset;
51 import org.jfree.ui.ApplicationFrame;
52 import org.jfree.ui.RefineryUtilities;
53
54 /**
55  * A demo of the {@link HistogramDataset} class.
56  *
57  * @author Jelai Wang, jelaiw AT mindspring.com
58  */

59 public class HistogramDemo extends ApplicationFrame {
60     
61     /** For generating random numbers. */
62     static Random JavaDoc random = new Random JavaDoc();
63
64     /**
65      * Creates a new demo.
66      *
67      * @param title the frame title.
68      */

69     public HistogramDemo(String JavaDoc title) {
70         super(title);
71         IntervalXYDataset dataset = createDataset();
72         JFreeChart chart = createChart(dataset);
73         ChartPanel chartPanel = new ChartPanel(chart);
74         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
75         chartPanel.setMouseZoomable(true, false);
76         setContentPane(chartPanel);
77     }
78     
79     /**
80      * Creates a sample {@link HistogramDataset}.
81      *
82      * @return The dataset.
83      */

84     private IntervalXYDataset createDataset() {
85         HistogramDataset dataset = new HistogramDataset();
86         dataset.setType(HistogramDataset.RELATIVE_FREQUENCY);
87         dataset.addSeries("H1", gaussianData(1000, 3.0), 20);
88         dataset.addSeries("H0", gaussianData(1000, 0), 20);
89         return dataset;
90     }
91     
92     // ****************************************************************************
93
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
94
// * Please note that commercial support and documentation is available from: *
95
// * *
96
// * http://www.object-refinery.com/jfreechart/support.html *
97
// * *
98
// * This is not only a great service for developers, but is a VERY IMPORTANT *
99
// * source of funding for the JFreeChart project. Please support us so that *
100
// * we can continue developing free software. *
101
// ****************************************************************************
102

103     /**
104      * Creates a chart.
105      *
106      * @param dataset a dataset.
107      *
108      * @return The chart.
109      */

110     private JFreeChart createChart(IntervalXYDataset dataset) {
111         JFreeChart chart = ChartFactory.createHistogram(
112             "Histogram Demo",
113             null,
114             null,
115             dataset,
116             PlotOrientation.VERTICAL,
117             true,
118             false,
119             false
120         );
121         chart.getXYPlot().setForegroundAlpha(0.75f);
122         return chart;
123     }
124     
125     /**
126      * Generates an array of sample data.
127      *
128      * @param size the array size.
129      * @param shift the shift from zero.
130      *
131      * @return The array of sample data.
132      */

133     private static double[] gaussianData(int size, double shift) {
134         double[] d = new double[size];
135         for (int i = 0; i < d.length; i++) {
136             d[i] = random.nextGaussian() + shift;
137         }
138         return d;
139     }
140     
141     /**
142      * The starting point for the demo.
143      *
144      * @param args ignored.
145      *
146      * @throws IOException if there is a problem saving the file.
147      */

148     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
149         
150         HistogramDemo demo = new HistogramDemo("Histogram Demo");
151         demo.pack();
152         RefineryUtilities.centerFrameOnScreen(demo);
153         demo.setVisible(true);
154         
155     }
156
157 }
158
Popular Tags