KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > JThermometer


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------
27  * JThermometer.java
28  * -----------------
29  * A plot that displays a single value in a thermometer type display.
30  *
31  * (C) Copyright 2000-2005, Australian Antarctic Division and Contributors.
32  *
33  * Original Author: Bryan Scott.
34  * Contributor(s): David Gilbert (for Object Refinery Limited);
35  * Irv Thomae;
36  *
37  * Changes (from 17-Sep-2002)
38  * --------------------------
39  * 17-Sep-2002 : Reviewed with Checkstyle utility (DG);
40  * 18-Sep-2003 : Integrated new methods contributed by Irv Thomae (DG);
41  * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
42  * 31-May-2005 : Fixed typo in method name (DG);
43  *
44  */

45
46 package org.jfree.chart.plot;
47
48 import java.awt.CardLayout JavaDoc;
49 import java.awt.Color JavaDoc;
50 import java.awt.Font JavaDoc;
51 import java.awt.Paint JavaDoc;
52 import java.io.Serializable JavaDoc;
53 import java.text.DecimalFormat JavaDoc;
54
55 import javax.swing.JPanel JavaDoc;
56
57 import org.jfree.chart.ChartPanel;
58 import org.jfree.chart.JFreeChart;
59 import org.jfree.chart.axis.ValueAxis;
60 import org.jfree.chart.title.TextTitle;
61 import org.jfree.chart.title.Title;
62 import org.jfree.data.general.DefaultValueDataset;
63 import org.jfree.ui.RectangleInsets;
64
65 /**
66  * An initial quick and dirty. The concept behind this class would be to
67  * generate a gui bean that could be used within JBuilder, Netbeans etc...
68  *
69  * Copyright (c) 2002
70  * Australian Antarctic Division
71  *
72  * @author Bryan Scott
73  */

74 public class JThermometer extends JPanel JavaDoc implements Serializable JavaDoc {
75
76     /** For serialization. */
77     private static final long serialVersionUID = 1079905665515589820L;
78     
79     /** The dataset. */
80     private DefaultValueDataset data;
81
82     /** The chart. */
83     private JFreeChart chart;
84
85     /** The chart panel. */
86     private ChartPanel panel;
87
88     /** The thermometer plot. */
89     private ThermometerPlot plot = new ThermometerPlot();
90
91     /**
92      * Default constructor.
93      */

94     public JThermometer() {
95         super(new CardLayout JavaDoc());
96         this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
97         this.data = new DefaultValueDataset();
98         //data.setRange(new Double(-60000), new Double(60000));
99
this.plot.setDataset(this.data);
100         this.chart = new JFreeChart(
101             null, JFreeChart.DEFAULT_TITLE_FONT, this.plot, false
102         );
103         this.panel = new ChartPanel(this.chart);
104         add(this.panel, "Panel");
105         setBackground(getBackground());
106     }
107
108     /**
109      * Adds a subtitle to the chart.
110      *
111      * @param subtitle the subtitle.
112      */

113     public void addSubtitle(Title subtitle) {
114         this.chart.addSubtitle(subtitle);
115     }
116
117     /**
118      * Adds a subtitle to the chart.
119      *
120      * @param subtitle the subtitle.
121      */

122     public void addSubtitle(String JavaDoc subtitle) {
123         this.chart.addSubtitle(new TextTitle(subtitle));
124     }
125
126     /**
127      * Adds a subtitle to the chart.
128      *
129      * @param subtitle the subtitle.
130      * @param font the subtitle font.
131      */

132     public void addSubtitle(String JavaDoc subtitle, Font JavaDoc font) {
133         this.chart.addSubtitle(new TextTitle(subtitle, font));
134     }
135
136     /**
137      * Sets the value format for the thermometer.
138      *
139      * @param df the formatter.
140      */

141     public void setValueFormat(DecimalFormat JavaDoc df) {
142         this.plot.setValueFormat(df);
143     }
144
145     /**
146      * Sets the lower and upper bounds for the thermometer.
147      *
148      * @param lower the lower bound.
149      * @param upper the upper bound.
150      */

151     public void setRange(double lower, double upper) {
152         this.plot.setRange(lower, upper);
153     }
154
155     /**
156      * Sets the range.
157      *
158      * @param range the range type.
159      * @param displayLow the low value.
160      * @param displayHigh the high value.
161      */

162     public void setSubrangeInfo(int range, double displayLow,
163                                 double displayHigh) {
164         this.plot.setSubrangeInfo(range, displayLow, displayHigh);
165     }
166
167     /**
168      * Sets the range.
169      *
170      * @param range the range type.
171      * @param rangeLow the low value for the range.
172      * @param rangeHigh the high value for the range.
173      * @param displayLow the low value for display.
174      * @param displayHigh the high value for display.
175      */

176     public void setSubrangeInfo(int range,
177                              double rangeLow, double rangeHigh,
178                              double displayLow, double displayHigh) {
179
180         this.plot.setSubrangeInfo(range, rangeLow, rangeHigh, displayLow,
181                 displayHigh);
182
183     }
184
185     /**
186      * Sets the location at which the temperature value is displayed.
187      *
188      * @param loc the location.
189      */

190     public void setValueLocation(int loc) {
191         this.plot.setValueLocation(loc);
192         this.panel.repaint();
193     }
194
195     /**
196      * Sets the value paint.
197      *
198      * @param paint the paint.
199      */

200     public void setValuePaint(Paint JavaDoc paint) {
201         this.plot.setValuePaint(paint);
202     }
203
204     /**
205      * Returns the value of the thermometer.
206      *
207      * @return The value.
208      */

209     public Number JavaDoc getValue() {
210         if (this.data != null) {
211             return this.data.getValue();
212         }
213         else {
214             return null;
215         }
216     }
217
218     /**
219      * Sets the value of the thermometer.
220      *
221      * @param value the value.
222      */

223     public void setValue(double value) {
224         setValue(new Double JavaDoc(value));
225     }
226
227     /**
228      * Sets the value of the thermometer.
229      *
230      * @param value the value.
231      */

232     public void setValue(Number JavaDoc value) {
233         if (this.data != null) {
234             this.data.setValue(value);
235         }
236     }
237
238     /**
239      * Sets the unit type.
240      *
241      * @param i the unit type.
242      */

243     public void setUnits(int i) {
244         if (this.plot != null) {
245             this.plot.setUnits(i);
246         }
247     }
248
249     /**
250      * Sets the outline paint.
251      *
252      * @param p the paint.
253      */

254     public void setOutlinePaint(Paint JavaDoc p) {
255         if (this.plot != null) {
256             this.plot.setOutlinePaint(p);
257         }
258     }
259
260     /**
261      * Sets the foreground color.
262      *
263      * @param fg the foreground color.
264      */

265     public void setForeground(Color JavaDoc fg) {
266         super.setForeground(fg);
267         if (this.plot != null) {
268             this.plot.setThermometerPaint(fg);
269         }
270     }
271
272     /**
273      * Sets the background color.
274      *
275      * @param bg the background color.
276      */

277     public void setBackground(Color JavaDoc bg) {
278         super.setBackground(bg);
279         if (this.plot != null) {
280             this.plot.setBackgroundPaint(bg);
281         }
282         if (this.chart != null) {
283             this.chart.setBackgroundPaint(bg);
284         }
285         if (this.panel != null) {
286             this.panel.setBackground(bg);
287         }
288     }
289
290     /**
291      * Sets the value font.
292      *
293      * @param f the font.
294      */

295     public void setValueFont(Font JavaDoc f) {
296         if (this.plot != null) {
297             this.plot.setValueFont(f);
298         }
299     }
300
301     /**
302      * Returns the tick label font.
303      *
304      * @return The tick label font.
305      */

306     public Font JavaDoc getTickLabelFont() {
307         ValueAxis axis = this.plot.getRangeAxis();
308         return axis.getTickLabelFont();
309     }
310
311     /**
312      * Sets the tick label font.
313      *
314      * @param font the font.
315      */

316     public void setTickLabelFont(Font JavaDoc font) {
317         ValueAxis axis = this.plot.getRangeAxis();
318         axis.setTickLabelFont(font);
319     }
320
321     /**
322      * Increases or decreases the tick font size.
323      *
324      * @param delta the change in size.
325      */

326     public void changeTickFontSize(int delta) {
327         Font JavaDoc f = getTickLabelFont();
328         String JavaDoc fName = f.getFontName();
329         Font JavaDoc newFont = new Font JavaDoc(fName, f.getStyle(), (f.getSize() + delta));
330         setTickLabelFont(newFont);
331     }
332
333     /**
334      * Sets the tick font style.
335      *
336      * @param style the style.
337      */

338     public void setTickFontStyle(int style) {
339         Font JavaDoc f = getTickLabelFont();
340         String JavaDoc fName = f.getFontName();
341         Font JavaDoc newFont = new Font JavaDoc(fName, style, f.getSize());
342         setTickLabelFont(newFont);
343     }
344
345     /**
346      * Sets the flag that controls whether or not the display range follows the
347      * data value.
348      *
349      * @param flag the new value of the flag.
350      */

351     public void setFollowDataInSubranges(boolean flag) {
352         this.plot.setFollowDataInSubranges(flag);
353     }
354
355     /**
356      * Sets the flag that controls whether or not value lines are displayed.
357      *
358      * @param b the new flag value.
359      */

360     public void setShowValueLines(boolean b) {
361         this.plot.setShowValueLines(b);
362     }
363
364     /**
365      * Sets the location for the axis.
366      *
367      * @param location the location.
368      */

369     public void setShowAxisLocation(int location) {
370         this.plot.setAxisLocation(location);
371     }
372
373     /**
374      * Returns the location for the axis.
375      *
376      * @return The location.
377      */

378     public int getShowAxisLocation() {
379       return this.plot.getAxisLocation();
380     }
381
382 }
383
Popular Tags