KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BubblyBubblesDemo.java
24  * ----------------------
25  * (C) Copyright 2003 by Barak Naveh and Contributors.
26  *
27  * Original Author: Barak Naveh;;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: BubblyBubblesDemo.java,v 1.5 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41 import java.awt.GradientPaint JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.plot.PlotOrientation;
48 import org.jfree.chart.plot.XYPlot;
49 import org.jfree.data.MatrixSeriesCollection;
50 import org.jfree.data.NormalizedMatrixSeries;
51 import org.jfree.ui.ApplicationFrame;
52 import org.jfree.ui.RefineryUtilities;
53
54 /**
55  * A demo that shows how matrix series can be used for charts that follow a
56  * constantly changing grid input.
57  *
58  * @author Barak Naveh
59  *
60  * @since Jun 25, 2003
61  */

62 public class BubblyBubblesDemo extends ApplicationFrame {
63
64     /** The default size. */
65     private static final int SIZE = 10;
66     
67     /** The default title. */
68     private static final String JavaDoc TITLE = "Population count at grid locations";
69
70     /**
71      * The normalized matrix series is used here to represent a changing
72      * population on a grid.
73      */

74     private NormalizedMatrixSeries m_series;
75
76     /**
77      * A demonstration application showing a bubble chart using matrix series.
78      *
79      * @param title the frame title.
80      */

81     public BubblyBubblesDemo(String JavaDoc title) {
82         super(title);
83
84         m_series = createInitialSeries();
85
86         MatrixSeriesCollection dataset = new MatrixSeriesCollection(m_series);
87
88         JFreeChart chart = ChartFactory.createBubbleChart(
89             TITLE, "X", "Y", dataset,
90             PlotOrientation.VERTICAL,
91             true,
92             true, false);
93
94         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0,
95                 1000, Color.blue));
96
97         XYPlot plot = chart.getXYPlot();
98         plot.setForegroundAlpha(0.5f);
99
100         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
101         domainAxis.setLowerBound(-0.5);
102
103         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
104
105         // rangeAxis.setInverted(true); // uncoment to reproduce a bug in jFreeChart
106
rangeAxis.setLowerBound(-0.5);
107
108         ChartPanel chartPanel = new ChartPanel(chart);
109         chartPanel.setVerticalZoom(true);
110         chartPanel.setHorizontalZoom(true);
111         setContentPane(chartPanel);
112     }
113
114     /**
115      * Starting point for the demonstration application.
116      *
117      * @param args ignored.
118      */

119     public static void main(String JavaDoc[] args) {
120         BubblyBubblesDemo demo = new BubblyBubblesDemo(TITLE);
121         demo.pack();
122         demo.setSize(800, 600);
123         RefineryUtilities.centerFrameOnScreen(demo);
124         demo.setVisible(true);
125
126         Thread JavaDoc updater = demo.new UpdaterThread();
127         updater.setDaemon(true);
128         updater.start();
129     }
130
131     // ****************************************************************************
132
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
133
// * Please note that commercial support and documentation is available from: *
134
// * *
135
// * http://www.object-refinery.com/jfreechart/support.html *
136
// * *
137
// * This is not only a great service for developers, but is a VERY IMPORTANT *
138
// * source of funding for the JFreeChart project. Please support us so that *
139
// * we can continue developing free software. *
140
// ****************************************************************************
141

142     /**
143      * Creates a series.
144      *
145      * @return The series.
146      */

147     private NormalizedMatrixSeries createInitialSeries() {
148         NormalizedMatrixSeries series =
149             new NormalizedMatrixSeries("Sample Grid 1", SIZE, SIZE);
150
151         // seed a few random bubbles
152
for (int count = 0; count < SIZE; count++) {
153             int i = (int) (Math.random() * SIZE);
154             int j = (int) (Math.random() * SIZE);
155
156             int mij = (int) (Math.random() * SIZE);
157             series.update(i, j, mij);
158         }
159
160         series.setScaleFactor(series.getItemCount());
161
162         return series;
163     }
164
165     /**
166      * A thread for updating the dataset.
167      */

168     private class UpdaterThread extends Thread JavaDoc {
169         /**
170          * @see java.lang.Runnable#run()
171          */

172         public void run() {
173             setPriority(MIN_PRIORITY); // be nice
174

175             while (true) {
176                 int i = (int) (Math.random() * SIZE);
177                 int j = (int) (Math.random() * SIZE);
178
179                 m_series.update(i, j, m_series.get(i, j) + 1);
180
181                 try {
182                     sleep(50);
183                 }
184                 catch (InterruptedException JavaDoc e) {
185                 }
186             }
187         }
188     }
189 }
190
191
192
193
Popular Tags