Source for org.jfree.chart.plot.JThermometer

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -----------------
  28:  * JThermometer.java
  29:  * -----------------
  30:  * A plot that displays a single value in a thermometer type display.
  31:  *
  32:  * (C) Copyright 2000-2007, Australian Antarctic Division and Contributors.
  33:  *
  34:  * Original Author:  Bryan Scott.
  35:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  36:  *                   Irv Thomae;
  37:  *
  38:  * Changes (from 17-Sep-2002)
  39:  * --------------------------
  40:  * 17-Sep-2002 : Reviewed with Checkstyle utility (DG);
  41:  * 18-Sep-2003 : Integrated new methods contributed by Irv Thomae (DG);
  42:  * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
  43:  * 31-May-2005 : Fixed typo in method name (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.chart.plot;
  48: 
  49: import java.awt.CardLayout;
  50: import java.awt.Color;
  51: import java.awt.Font;
  52: import java.awt.Paint;
  53: import java.io.Serializable;
  54: import java.text.DecimalFormat;
  55: 
  56: import javax.swing.JPanel;
  57: 
  58: import org.jfree.chart.ChartPanel;
  59: import org.jfree.chart.JFreeChart;
  60: import org.jfree.chart.axis.ValueAxis;
  61: import org.jfree.chart.title.TextTitle;
  62: import org.jfree.chart.title.Title;
  63: import org.jfree.data.general.DefaultValueDataset;
  64: import org.jfree.ui.RectangleInsets;
  65: 
  66: /**
  67:  * An initial quick and dirty.  The concept behind this class would be to
  68:  * generate a gui bean that could be used within JBuilder, Netbeans etc...
  69:  */
  70: public class JThermometer extends JPanel implements Serializable {
  71: 
  72:     /** For serialization. */
  73:     private static final long serialVersionUID = 1079905665515589820L;
  74:     
  75:     /** The dataset. */
  76:     private DefaultValueDataset data;
  77: 
  78:     /** The chart. */
  79:     private JFreeChart chart;
  80: 
  81:     /** The chart panel. */
  82:     private ChartPanel panel;
  83: 
  84:     /** The thermometer plot. */
  85:     private ThermometerPlot plot = new ThermometerPlot();
  86: 
  87:     /**
  88:      * Default constructor.
  89:      */
  90:     public JThermometer() {
  91:         super(new CardLayout());
  92:         this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
  93:         this.data = new DefaultValueDataset();
  94:         this.plot.setDataset(this.data);
  95:         this.chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, 
  96:                 this.plot, false);
  97:         this.panel = new ChartPanel(this.chart);
  98:         add(this.panel, "Panel");
  99:         setBackground(getBackground());
 100:     }
 101: 
 102:     /**
 103:      * Adds a subtitle to the chart.
 104:      *
 105:      * @param subtitle  the subtitle.
 106:      */
 107:     public void addSubtitle(Title subtitle) {
 108:         this.chart.addSubtitle(subtitle);
 109:     }
 110: 
 111:     /**
 112:      * Adds a subtitle to the chart.
 113:      *
 114:      * @param subtitle  the subtitle.
 115:      */
 116:     public void addSubtitle(String subtitle) {
 117:         this.chart.addSubtitle(new TextTitle(subtitle));
 118:     }
 119: 
 120:     /**
 121:      * Adds a subtitle to the chart.
 122:      *
 123:      * @param subtitle  the subtitle.
 124:      * @param font  the subtitle font.
 125:      */
 126:     public void addSubtitle(String subtitle, Font font) {
 127:         this.chart.addSubtitle(new TextTitle(subtitle, font));
 128:     }
 129: 
 130:     /**
 131:      * Sets the value format for the thermometer.
 132:      *
 133:      * @param df  the formatter.
 134:      */
 135:     public void setValueFormat(DecimalFormat df) {
 136:         this.plot.setValueFormat(df);
 137:     }
 138: 
 139:     /**
 140:      * Sets the lower and upper bounds for the thermometer.
 141:      *
 142:      * @param lower  the lower bound.
 143:      * @param upper  the upper bound.
 144:      */
 145:     public void setRange(double lower, double upper) {
 146:         this.plot.setRange(lower, upper);
 147:     }
 148: 
 149:     /**
 150:      * Sets the range.
 151:      *
 152:      * @param range  the range type.
 153:      * @param displayLow  the low value.
 154:      * @param displayHigh  the high value.
 155:      */
 156:     public void setSubrangeInfo(int range, double displayLow, 
 157:                                 double displayHigh) {
 158:         this.plot.setSubrangeInfo(range, displayLow, displayHigh);
 159:     }
 160: 
 161:     /**
 162:      * Sets the range.
 163:      *
 164:      * @param range  the range type.
 165:      * @param rangeLow  the low value for the range.
 166:      * @param rangeHigh  the high value for the range.
 167:      * @param displayLow  the low value for display.
 168:      * @param displayHigh  the high value for display.
 169:      */
 170:     public void setSubrangeInfo(int range,
 171:                              double rangeLow, double rangeHigh,
 172:                              double displayLow, double displayHigh) {
 173: 
 174:         this.plot.setSubrangeInfo(range, rangeLow, rangeHigh, displayLow, 
 175:                 displayHigh);
 176: 
 177:     }
 178: 
 179:     /**
 180:      * Sets the location at which the temperature value is displayed.
 181:      *
 182:      * @param loc  the location.
 183:      */
 184:     public void setValueLocation(int loc) {
 185:         this.plot.setValueLocation(loc);
 186:         this.panel.repaint();
 187:     }
 188: 
 189:     /**
 190:      * Sets the value paint.
 191:      *
 192:      * @param paint  the paint.
 193:      */
 194:     public void setValuePaint(Paint paint) {
 195:         this.plot.setValuePaint(paint);
 196:     }
 197: 
 198:     /**
 199:      * Returns the value of the thermometer.
 200:      *
 201:      * @return The value.
 202:      */
 203:     public Number getValue() {
 204:         if (this.data != null) {
 205:             return this.data.getValue();
 206:         }
 207:         else {
 208:             return null;
 209:         }
 210:     }
 211: 
 212:     /**
 213:      * Sets the value of the thermometer.
 214:      *
 215:      * @param value  the value.
 216:      */
 217:     public void setValue(double value) {
 218:         setValue(new Double(value));
 219:     }
 220: 
 221:     /**
 222:      * Sets the value of the thermometer.
 223:      *
 224:      * @param value  the value.
 225:      */
 226:     public void setValue(Number value) {
 227:         if (this.data != null) {
 228:             this.data.setValue(value);
 229:         }
 230:     }
 231: 
 232:     /**
 233:      * Sets the unit type.
 234:      *
 235:      * @param i  the unit type.
 236:      */
 237:     public void setUnits(int i) {
 238:         if (this.plot != null) {
 239:             this.plot.setUnits(i);
 240:         }
 241:     }
 242: 
 243:     /**
 244:      * Sets the outline paint.
 245:      *
 246:      * @param p  the paint.
 247:      */
 248:     public void setOutlinePaint(Paint p) {
 249:         if (this.plot != null) {
 250:             this.plot.setOutlinePaint(p);
 251:         }
 252:     }
 253: 
 254:     /**
 255:      * Sets the foreground color.
 256:      *
 257:      * @param fg  the foreground color.
 258:      */
 259:     public void setForeground(Color fg) {
 260:         super.setForeground(fg);
 261:         if (this.plot != null) {
 262:             this.plot.setThermometerPaint(fg);
 263:         }
 264:     }
 265: 
 266:     /**
 267:      * Sets the background color.
 268:      *
 269:      * @param bg  the background color.
 270:      */
 271:     public void setBackground(Color bg) {
 272:         super.setBackground(bg);
 273:         if (this.plot != null) {
 274:             this.plot.setBackgroundPaint(bg);
 275:         }
 276:         if (this.chart != null) {
 277:             this.chart.setBackgroundPaint(bg);
 278:         }
 279:         if (this.panel != null) {
 280:             this.panel.setBackground(bg);
 281:         }
 282:     }
 283: 
 284:     /**
 285:      * Sets the value font.
 286:      *
 287:      * @param f  the font.
 288:      */
 289:     public void setValueFont(Font f) {
 290:         if (this.plot != null) {
 291:             this.plot.setValueFont(f);
 292:         }
 293:     }
 294: 
 295:     /**
 296:      * Returns the tick label font.
 297:      *
 298:      * @return The tick label font.
 299:      */
 300:     public Font getTickLabelFont() {
 301:         ValueAxis axis = this.plot.getRangeAxis();
 302:         return axis.getTickLabelFont();
 303:     }
 304: 
 305:     /**
 306:      * Sets the tick label font.
 307:      *
 308:      * @param font  the font.
 309:      */
 310:     public void setTickLabelFont(Font font) {
 311:         ValueAxis axis = this.plot.getRangeAxis();
 312:         axis.setTickLabelFont(font);
 313:     }
 314: 
 315:     /**
 316:      * Increases or decreases the tick font size.
 317:      *
 318:      * @param delta  the change in size.
 319:      */
 320:     public void changeTickFontSize(int delta) {
 321:         Font f = getTickLabelFont();
 322:         String fName = f.getFontName();
 323:         Font newFont = new Font(fName, f.getStyle(), (f.getSize() + delta));
 324:         setTickLabelFont(newFont);
 325:     }
 326: 
 327:     /**
 328:      * Sets the tick font style.
 329:      *
 330:      * @param style  the style.
 331:      */
 332:     public void setTickFontStyle(int style) {
 333:         Font f = getTickLabelFont();
 334:         String fName = f.getFontName();
 335:         Font newFont = new Font(fName, style, f.getSize());
 336:         setTickLabelFont(newFont);
 337:     }
 338: 
 339:     /**
 340:      * Sets the flag that controls whether or not the display range follows the
 341:      * data value.
 342:      *
 343:      * @param flag  the new value of the flag.
 344:      */
 345:     public void setFollowDataInSubranges(boolean flag) {
 346:         this.plot.setFollowDataInSubranges(flag);
 347:     }
 348: 
 349:     /**
 350:      * Sets the flag that controls whether or not value lines are displayed.
 351:      *
 352:      * @param b  the new flag value.
 353:      */
 354:     public void setShowValueLines(boolean b) {
 355:         this.plot.setShowValueLines(b);
 356:     }
 357: 
 358:     /**
 359:      * Sets the location for the axis.
 360:      * 
 361:      * @param location  the location.
 362:      */
 363:     public void setShowAxisLocation(int location) {
 364:         this.plot.setAxisLocation(location);
 365:     }
 366: 
 367:     /**
 368:      * Returns the location for the axis.
 369:      * 
 370:      * @return The location.
 371:      */
 372:     public int getShowAxisLocation() {
 373:       return this.plot.getAxisLocation();
 374:     }
 375: 
 376: }