KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BubblyBubblesDemo2.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: BubblyBubblesDemo2.java,v 1.5 2003/11/28 10:57:35 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 BubblyBubblesDemo2 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 BubblyBubblesDemo2(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,
90             "X",
91             "Y",
92             dataset,
93             PlotOrientation.VERTICAL,
94             true,
95             true,
96             false
97         );
98
99         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.yellow));
100
101         XYPlot plot = chart.getXYPlot();
102         plot.setForegroundAlpha(0.5f);
103
104         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
105         domainAxis.setLowerBound(-0.5);
106
107         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
108
109         // rangeAxis.setInverted(true); // uncoment to reproduce a bug in jFreeChart
110
rangeAxis.setLowerBound(-0.5);
111
112         ChartPanel chartPanel = new ChartPanel(chart);
113         chartPanel.setVerticalZoom(true);
114         chartPanel.setHorizontalZoom(true);
115         setContentPane(chartPanel);
116     }
117
118     // ****************************************************************************
119
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
120
// * Please note that commercial support and documentation is available from: *
121
// * *
122
// * http://www.object-refinery.com/jfreechart/support.html *
123
// * *
124
// * This is not only a great service for developers, but is a VERY IMPORTANT *
125
// * source of funding for the JFreeChart project. Please support us so that *
126
// * we can continue developing free software. *
127
// ****************************************************************************
128

129     /**
130      * Starting point for the demonstration application.
131      *
132      * @param args ignored.
133      */

134     public static void main(String JavaDoc[] args) {
135         BubblyBubblesDemo2 demo = new BubblyBubblesDemo2(TITLE);
136         demo.pack();
137         demo.setSize(800, 600);
138         RefineryUtilities.centerFrameOnScreen(demo);
139         demo.setVisible(true);
140
141         Thread JavaDoc updater = demo.new UpdaterThread();
142         updater.setDaemon(true);
143         updater.start();
144     }
145
146
147     /**
148      * Creates a series.
149      *
150      * @return The series.
151      */

152     private NormalizedMatrixSeries createInitialSeries() {
153         NormalizedMatrixSeries series = new NormalizedMatrixSeries("Sample Grid 1", SIZE, SIZE);
154
155         // seed a few random bubbles
156
for (int count = 0; count < SIZE * 3; count++) {
157             int i = (int) (Math.random() * SIZE);
158             int j = (int) (Math.random() * SIZE);
159
160             double mij = Math.random() * 1;
161             series.update(i, j, mij);
162         }
163
164         series.setScaleFactor(series.getItemCount());
165
166         return series;
167     }
168
169     /**
170      * Updater thread.
171      */

172     private class UpdaterThread extends Thread JavaDoc {
173         /**
174          * @see java.lang.Runnable#run()
175          */

176         public void run() {
177             setPriority(MIN_PRIORITY); // be nice
178

179             while (true) {
180                 int i = (int) (Math.random() * SIZE);
181                 int j = (int) (Math.random() * SIZE);
182
183                 double change = Math.random() * 3.0 - 1.0;
184
185                 m_series.update(i, j, m_series.get(i, j) + change);
186
187                 try {
188                     sleep(50);
189                 }
190                 catch (InterruptedException JavaDoc e) {
191                 }
192             }
193         }
194     }
195 }
196
Popular Tags