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-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
  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-2005, 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:  * Copyright (c) 2002
  71:  * Australian Antarctic Division
  72:  *
  73:  * @author Bryan Scott
  74:  */
  75: public class JThermometer extends JPanel implements Serializable {
  76: 
  77:     /** For serialization. */
  78:     private static final long serialVersionUID = 1079905665515589820L;
  79:     
  80:     /** The dataset. */
  81:     private DefaultValueDataset data;
  82: 
  83:     /** The chart. */
  84:     private JFreeChart chart;
  85: 
  86:     /** The chart panel. */
  87:     private ChartPanel panel;
  88: 
  89:     /** The thermometer plot. */
  90:     private ThermometerPlot plot = new ThermometerPlot();
  91: 
  92:     /**
  93:      * Default constructor.
  94:      */
  95:     public JThermometer() {
  96:         super(new CardLayout());
  97:         this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
  98:         this.data = new DefaultValueDataset();
  99:         //data.setRange(new Double(-60000), new Double(60000));
 100:         this.plot.setDataset(this.data);
 101:         this.chart = new JFreeChart(
 102:             null, JFreeChart.DEFAULT_TITLE_FONT, this.plot, false
 103:         );
 104:         this.panel = new ChartPanel(this.chart);
 105:         add(this.panel, "Panel");
 106:         setBackground(getBackground());
 107:     }
 108: 
 109:     /**
 110:      * Adds a subtitle to the chart.
 111:      *
 112:      * @param subtitle  the subtitle.
 113:      */
 114:     public void addSubtitle(Title subtitle) {
 115:         this.chart.addSubtitle(subtitle);
 116:     }
 117: 
 118:     /**
 119:      * Adds a subtitle to the chart.
 120:      *
 121:      * @param subtitle  the subtitle.
 122:      */
 123:     public void addSubtitle(String subtitle) {
 124:         this.chart.addSubtitle(new TextTitle(subtitle));
 125:     }
 126: 
 127:     /**
 128:      * Adds a subtitle to the chart.
 129:      *
 130:      * @param subtitle  the subtitle.
 131:      * @param font  the subtitle font.
 132:      */
 133:     public void addSubtitle(String subtitle, Font font) {
 134:         this.chart.addSubtitle(new TextTitle(subtitle, font));
 135:     }
 136: 
 137:     /**
 138:      * Sets the value format for the thermometer.
 139:      *
 140:      * @param df  the formatter.
 141:      */
 142:     public void setValueFormat(DecimalFormat df) {
 143:         this.plot.setValueFormat(df);
 144:     }
 145: 
 146:     /**
 147:      * Sets the lower and upper bounds for the thermometer.
 148:      *
 149:      * @param lower  the lower bound.
 150:      * @param upper  the upper bound.
 151:      */
 152:     public void setRange(double lower, double upper) {
 153:         this.plot.setRange(lower, upper);
 154:     }
 155: 
 156:     /**
 157:      * Sets the range.
 158:      *
 159:      * @param range  the range type.
 160:      * @param displayLow  the low value.
 161:      * @param displayHigh  the high value.
 162:      */
 163:     public void setSubrangeInfo(int range, double displayLow, 
 164:                                 double displayHigh) {
 165:         this.plot.setSubrangeInfo(range, displayLow, displayHigh);
 166:     }
 167: 
 168:     /**
 169:      * Sets the range.
 170:      *
 171:      * @param range  the range type.
 172:      * @param rangeLow  the low value for the range.
 173:      * @param rangeHigh  the high value for the range.
 174:      * @param displayLow  the low value for display.
 175:      * @param displayHigh  the high value for display.
 176:      */
 177:     public void setSubrangeInfo(int range,
 178:                              double rangeLow, double rangeHigh,
 179:                              double displayLow, double displayHigh) {
 180: 
 181:         this.plot.setSubrangeInfo(range, rangeLow, rangeHigh, displayLow, 
 182:                 displayHigh);
 183: 
 184:     }
 185: 
 186:     /**
 187:      * Sets the location at which the temperature value is displayed.
 188:      *
 189:      * @param loc  the location.
 190:      */
 191:     public void setValueLocation(int loc) {
 192:         this.plot.setValueLocation(loc);
 193:         this.panel.repaint();
 194:     }
 195: 
 196:     /**
 197:      * Sets the value paint.
 198:      *
 199:      * @param paint  the paint.
 200:      */
 201:     public void setValuePaint(Paint paint) {
 202:         this.plot.setValuePaint(paint);
 203:     }
 204: 
 205:     /**
 206:      * Returns the value of the thermometer.
 207:      *
 208:      * @return The value.
 209:      */
 210:     public Number getValue() {
 211:         if (this.data != null) {
 212:             return this.data.getValue();
 213:         }
 214:         else {
 215:             return null;
 216:         }
 217:     }
 218: 
 219:     /**
 220:      * Sets the value of the thermometer.
 221:      *
 222:      * @param value  the value.
 223:      */
 224:     public void setValue(double value) {
 225:         setValue(new Double(value));
 226:     }
 227: 
 228:     /**
 229:      * Sets the value of the thermometer.
 230:      *
 231:      * @param value  the value.
 232:      */
 233:     public void setValue(Number value) {
 234:         if (this.data != null) {
 235:             this.data.setValue(value);
 236:         }
 237:     }
 238: 
 239:     /**
 240:      * Sets the unit type.
 241:      *
 242:      * @param i  the unit type.
 243:      */
 244:     public void setUnits(int i) {
 245:         if (this.plot != null) {
 246:             this.plot.setUnits(i);
 247:         }
 248:     }
 249: 
 250:     /**
 251:      * Sets the outline paint.
 252:      *
 253:      * @param p  the paint.
 254:      */
 255:     public void setOutlinePaint(Paint p) {
 256:         if (this.plot != null) {
 257:             this.plot.setOutlinePaint(p);
 258:         }
 259:     }
 260: 
 261:     /**
 262:      * Sets the foreground color.
 263:      *
 264:      * @param fg  the foreground color.
 265:      */
 266:     public void setForeground(Color fg) {
 267:         super.setForeground(fg);
 268:         if (this.plot != null) {
 269:             this.plot.setThermometerPaint(fg);
 270:         }
 271:     }
 272: 
 273:     /**
 274:      * Sets the background color.
 275:      *
 276:      * @param bg  the background color.
 277:      */
 278:     public void setBackground(Color bg) {
 279:         super.setBackground(bg);
 280:         if (this.plot != null) {
 281:             this.plot.setBackgroundPaint(bg);
 282:         }
 283:         if (this.chart != null) {
 284:             this.chart.setBackgroundPaint(bg);
 285:         }
 286:         if (this.panel != null) {
 287:             this.panel.setBackground(bg);
 288:         }
 289:     }
 290: 
 291:     /**
 292:      * Sets the value font.
 293:      *
 294:      * @param f  the font.
 295:      */
 296:     public void setValueFont(Font f) {
 297:         if (this.plot != null) {
 298:             this.plot.setValueFont(f);
 299:         }
 300:     }
 301: 
 302:     /**
 303:      * Returns the tick label font.
 304:      *
 305:      * @return The tick label font.
 306:      */
 307:     public Font getTickLabelFont() {
 308:         ValueAxis axis = this.plot.getRangeAxis();
 309:         return axis.getTickLabelFont();
 310:     }
 311: 
 312:     /**
 313:      * Sets the tick label font.
 314:      *
 315:      * @param font  the font.
 316:      */
 317:     public void setTickLabelFont(Font font) {
 318:         ValueAxis axis = this.plot.getRangeAxis();
 319:         axis.setTickLabelFont(font);
 320:     }
 321: 
 322:     /**
 323:      * Increases or decreases the tick font size.
 324:      *
 325:      * @param delta  the change in size.
 326:      */
 327:     public void changeTickFontSize(int delta) {
 328:         Font f = getTickLabelFont();
 329:         String fName = f.getFontName();
 330:         Font newFont = new Font(fName, f.getStyle(), (f.getSize() + delta));
 331:         setTickLabelFont(newFont);
 332:     }
 333: 
 334:     /**
 335:      * Sets the tick font style.
 336:      *
 337:      * @param style  the style.
 338:      */
 339:     public void setTickFontStyle(int style) {
 340:         Font f = getTickLabelFont();
 341:         String fName = f.getFontName();
 342:         Font newFont = new Font(fName, style, f.getSize());
 343:         setTickLabelFont(newFont);
 344:     }
 345: 
 346:     /**
 347:      * Sets the flag that controls whether or not the display range follows the
 348:      * data value.
 349:      *
 350:      * @param flag  the new value of the flag.
 351:      */
 352:     public void setFollowDataInSubranges(boolean flag) {
 353:         this.plot.setFollowDataInSubranges(flag);
 354:     }
 355: 
 356:     /**
 357:      * Sets the flag that controls whether or not value lines are displayed.
 358:      *
 359:      * @param b  the new flag value.
 360:      */
 361:     public void setShowValueLines(boolean b) {
 362:         this.plot.setShowValueLines(b);
 363:     }
 364: 
 365:     /**
 366:      * Sets the location for the axis.
 367:      * 
 368:      * @param location  the location.
 369:      */
 370:     public void setShowAxisLocation(int location) {
 371:         this.plot.setAxisLocation(location);
 372:     }
 373: 
 374:     /**
 375:      * Returns the location for the axis.
 376:      * 
 377:      * @return The location.
 378:      */
 379:     public int getShowAxisLocation() {
 380:       return this.plot.getAxisLocation();
 381:     }
 382: 
 383: }