KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41 import java.awt.GradientPaint JavaDoc;
42 import java.util.Date JavaDoc;
43
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.axis.ColorBar;
47 import org.jfree.chart.axis.DateAxis;
48 import org.jfree.chart.axis.LogarithmicAxis;
49 import org.jfree.chart.axis.NumberAxis;
50 import org.jfree.chart.axis.ValueAxis;
51 import org.jfree.chart.plot.ContourPlot;
52 import org.jfree.data.ContourDataset;
53 import org.jfree.data.DefaultContourDataset;
54 import org.jfree.ui.ApplicationFrame;
55 import org.jfree.ui.RefineryUtilities;
56
57 /**
58  * A demonstration application to illustrate ContourPlot.
59  * Command line options exist to control different plot properties
60  * such as colorbar orientation, etc. List of options are available
61  * by launching with the -? option, e.g., ContourPlotDemo -?
62  *
63  * @author David M. O'Donnell
64  */

65 public class ContourPlotDemo extends ApplicationFrame {
66
67     /** The x-axis. */
68     private ValueAxis xAxis = null;
69
70     /** The y-axis. */
71     private NumberAxis yAxis = null;
72
73     /** The z-axis. */
74     private ColorBar zColorBar = null;
75
76     /** Flag for vertical z-axis. */
77     //private static boolean zIsVertical = false;
78

79     /** Flag for x is date axis. */
80     private static boolean xIsDate = false;
81
82     /** Flag for x is log. */
83     private static boolean xIsLog = false;
84
85     /** Flag for y is log. */
86     private static boolean yIsLog = false;
87
88     /** Flag for z is log. */
89     private static boolean zIsLog = false;
90
91     /** Flag for x is inverted. */
92     private static boolean xIsInverted = false;
93
94     /** Flag for y is inverted. */
95     private static boolean yIsInverted = false;
96
97     /** Flag for z is inverted. */
98     private static boolean zIsInverted = false;
99
100     /** Flag for make holes. */
101     private static boolean makeHoles = false;
102
103     /** The number of x values in the dataset. */
104     private static int numX = 10;
105
106     /** The number of y values in the dataset. */
107     private static int numY = 20;
108
109     /** The ratio. */
110     private static double ratio = 0.0;
111     
112     /** The panel. */
113     public ChartPanel panel = null;
114
115
116     /**
117      * Constructs a new demonstration application.
118      *
119      * @param title the frame title.
120      */

121     public ContourPlotDemo(String JavaDoc title) {
122
123         super(title);
124
125         JFreeChart chart = createContourPlot();
126         panel = new ChartPanel(chart, true, true, true, true, true);
127         panel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
128         panel.setMaximumDrawHeight(100000); //stop ChartPanel from scaling output
129
panel.setMaximumDrawWidth(100000); //stop ChartPanel from scaling output
130
panel.setHorizontalZoom(true);
131         panel.setVerticalZoom(true);
132         panel.setFillZoomRectangle(true);
133
134     }
135
136     // ****************************************************************************
137
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
138
// * Please note that commercial support and documentation is available from: *
139
// * *
140
// * http://www.object-refinery.com/jfreechart/support.html *
141
// * *
142
// * This is not only a great service for developers, but is a VERY IMPORTANT *
143
// * source of funding for the JFreeChart project. Please support us so that *
144
// * we can continue developing free software. *
145
// ****************************************************************************
146

147     /**
148      * Creates a ContourPlot chart.
149      *
150      * @return the chart.
151      */

152     private JFreeChart createContourPlot() {
153
154         String JavaDoc title = "Contour Plot";
155         String JavaDoc xAxisLabel = "X Values";
156         String JavaDoc yAxisLabel = "Y Values";
157         String JavaDoc zAxisLabel = "Color Values";
158
159         if (xIsDate) {
160             xAxis = new DateAxis(xAxisLabel);
161             xIsLog = false; // force axis to be linear when displaying dates
162
}
163         else {
164             if (xIsLog) {
165                 xAxis = new LogarithmicAxis(xAxisLabel);
166             }
167             else {
168                 xAxis = new NumberAxis(xAxisLabel);
169             }
170         }
171
172         if (yIsLog) {
173             yAxis = new LogarithmicAxis(yAxisLabel);
174         }
175         else {
176             yAxis = new NumberAxis(yAxisLabel);
177         }
178
179         if (zIsLog) {
180             zColorBar = new ColorBar(zAxisLabel);
181         }
182         else {
183             zColorBar = new ColorBar(zAxisLabel);
184         }
185  
186         if (xAxis instanceof NumberAxis) {
187             ((NumberAxis) xAxis).setAutoRangeIncludesZero(false);
188             ((NumberAxis) xAxis).setInverted(xIsInverted);
189         }
190
191         yAxis.setAutoRangeIncludesZero(false);
192
193         yAxis.setInverted(yIsInverted);
194
195         if (!xIsDate) {
196             ((NumberAxis) xAxis).setLowerMargin(0.0);
197             ((NumberAxis) xAxis).setUpperMargin(0.0);
198         }
199
200         yAxis.setLowerMargin(0.0);
201         yAxis.setUpperMargin(0.0);
202
203         zColorBar.getAxis().setInverted(zIsInverted);
204         zColorBar.getAxis().setTickMarksVisible(true);
205
206         ContourDataset data = createDataset();
207
208         ContourPlot plot = new ContourPlot(data, xAxis, yAxis, zColorBar);
209
210         if (xIsDate) {
211             ratio = Math.abs(ratio); // don't use plot units for ratios when x axis is date
212
}
213         plot.setDataAreaRatio(ratio);
214
215         JFreeChart chart = new JFreeChart(title, null, plot, false);
216         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.green));
217
218         return chart;
219
220     }
221
222     /**
223      * Creates a ContourDataset.
224      *
225      * @return ContourDataset.
226      */

227     private ContourDataset createDataset() {
228
229         int numValues = numX * numY;
230         Date JavaDoc[] tmpDateX = new Date JavaDoc[numValues];
231         double[] tmpDoubleX = new double[numValues];
232         double[] tmpDoubleY = new double[numValues];
233
234         Double JavaDoc[] oDoubleX = new Double JavaDoc[numValues];
235         Double JavaDoc[] oDoubleY = new Double JavaDoc[numValues];
236         Double JavaDoc[] oDoubleZ = new Double JavaDoc[numValues];
237
238         int j = 0;
239         int z = 0;
240         int i = 0;
241         int last = 0;
242         double zmult = 1.0;
243         for (int k = 0; k < numValues; k++) {
244             i = k / numX;
245             if (last != i) {
246                 last = i;
247                 z = 0;
248                 zmult = 1.005 * zmult;
249             }
250             tmpDateX[k] = new Date JavaDoc((long) ((i + 100) * 1.e8));
251             tmpDoubleX[k] = i + 2;
252             tmpDoubleY[k] = zmult * (z++);
253             oDoubleX[k] = new Double JavaDoc(tmpDoubleX[k]);
254             oDoubleY[k] = new Double JavaDoc(tmpDoubleY[k]);
255             double rad = Math.random();
256             if (makeHoles && (rad > 0.4 && rad < 0.6)) {
257                 oDoubleZ[k] = null;
258             }
259             else {
260 // tmpDoubleZ[k] = 3.0 * ((tmpDoubleX[k] + 1) * (tmpDoubleY[k] + 1) + 100);
261
oDoubleZ[k] = new Double JavaDoc(3.0 * ((tmpDoubleX[k] + 1) * (tmpDoubleY[k] + 1) + 100));
262             }
263             j++;
264         }
265         ContourDataset data = null;
266
267         if (xIsDate) {
268             data = new DefaultContourDataset("Contouring", tmpDateX, oDoubleY, oDoubleZ);
269         }
270         else {
271             data = new DefaultContourDataset("Contouring", oDoubleX, oDoubleY, oDoubleZ);
272         }
273         return data;
274
275     }
276
277     /**
278      * Sets options passed via the command line
279      *
280      * @param args the command line arguments.
281      *
282      * @return Flag indicating whether program should continue.
283      */

284     protected static boolean processArgs(String JavaDoc[] args) {
285
286         String JavaDoc[] options = {"-?",
287                             "-invert",
288                             "-log",
289                             "-date",
290                             "-vertical",
291                             "-holes",
292                             "-ratio:",
293                             "-numX:",
294                             "-numY:"};
295
296         for (int i = 0; i < args.length; i++) {
297             boolean foundOption = false;
298             for (int j = 0; j < options.length; j++) {
299                 if (args[i].startsWith(options[j])) {
300                     foundOption = true;
301                     int index = 0;
302                     String JavaDoc tmpStr = null;
303                     switch (j) {
304                         case 0: // -?
305
usage();
306                             return false;
307                         case 1:
308                             xIsInverted = true;
309                             yIsInverted = true;
310                             zIsInverted = true;
311                             break;
312                         case 2:
313                             xIsLog = true;
314                             yIsLog = true;
315                             zIsLog = true;
316                             break;
317                         case 3:
318                             xIsDate = true;
319                             break;
320                         case 4:
321                             //zIsVertical = true;
322
break;
323                         case 5:
324                             makeHoles = true;
325                             break;
326                         case 6:
327                             index = args[i].indexOf(':');
328                             tmpStr = args[i].substring(index + 1);
329                             ratio = Double.parseDouble(tmpStr);
330                             break;
331                         case 7:
332                             index = args[i].indexOf(':');
333                             tmpStr = args[i].substring(index + 1);
334                             numX = Integer.parseInt(tmpStr);
335                             break;
336                         case 8:
337                             index = args[i].indexOf(':');
338                             tmpStr = args[i].substring(index + 1);
339                             numY = Integer.parseInt(tmpStr);
340                             break;
341                         default:
342                             System.out.println("Only 9 options available, update options array");
343                     }
344                 }
345             }
346             if (!foundOption) {
347                 System.out.println("Unknown option: " + args[i]);
348                 usage();
349                 return false;
350             }
351         }
352
353         return true; // continue running application
354
}
355
356     /**
357      * Prints usage information.
358      */

359     public static void usage() {
360         System.out.println("Usage:");
361         System.out.println("ContourPlotDemo -? -invert -log -date -vertical -holes -ratio:value "
362                            + "-numX:value -numY:value");
363         System.out.println("Where:");
364         System.out.println("-? displays usage and exits");
365         System.out.println("-invert cause axes to be inverted");
366         System.out.println("-log all axes will be logcale");
367         System.out.println("-date the X axis will be a date");
368         System.out.println("-vertical the colorbar will be drawn vertically");
369         System.out.println("-holes demos plotting data with missing values");
370         System.out.println("-ratio forces plot to maintain aspect ratio (Y/X) indicated by value");
371         System.out.println(" positive values are in pixels, while negative is in plot units");
372         System.out.println("-numX number of values to generate along the X axis");
373         System.out.println("-numY number of values to generate along the X axis");
374
375     }
376     /**
377      * Starting point for the demonstration application.
378      *
379      * @param args command line options, launch ContourDemoPlot -? for listing of options.
380      */

381     public static void main(String JavaDoc[] args) {
382
383         if (!processArgs(args)) {
384             System.exit(1);
385         }
386         ContourPlotDemo demo = new ContourPlotDemo("ContourPlot Demo");
387         demo.setContentPane(demo.panel);
388         demo.pack();
389         RefineryUtilities.centerFrameOnScreen(demo);
390         demo.setVisible(true);
391
392     }
393
394 }
395
Popular Tags