    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025     * Other names may be trademarks of their respective owners.]
    026     *
    027     * -----------------
    028     * JThermometer.java
    029     * -----------------
    030     * A plot that displays a single value in a thermometer type display.
    031     *
    032     * (C) Copyright 2000-2008, Australian Antarctic Division and Contributors.
    033     *
    034     * Original Author:  Bryan Scott.
    035     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    036     *                   Irv Thomae;
    037     *
    038     * Changes (from 17-Sep-2002)
    039     * --------------------------
    040     * 17-Sep-2002 : Reviewed with Checkstyle utility (DG);
    041     * 18-Sep-2003 : Integrated new methods contributed by Irv Thomae (DG);
    042     * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
    043     * 31-May-2005 : Fixed typo in method name (DG);
    044     *
    045     */
    046
    047    package org.jfree.chart.plot;
    048
    049    import java.awt.CardLayout;
    050    import java.awt.Color;
    051    import java.awt.Font;
    052    import java.awt.Paint;
    053    import java.io.Serializable;
    054    import java.text.DecimalFormat;
    055
    056    import javax.swing.JPanel;
    057
    058    import org.jfree.chart.ChartPanel;
    059    import org.jfree.chart.JFreeChart;
    060    import org.jfree.chart.axis.ValueAxis;
    061    import org.jfree.chart.title.TextTitle;
    062    import org.jfree.chart.title.Title;
    063    import org.jfree.data.general.DefaultValueDataset;
    064    import org.jfree.ui.RectangleInsets;
    065
    066    /**
    067     * An initial quick and dirty.  The concept behind this class would be to
    068     * generate a gui bean that could be used within JBuilder, Netbeans etc...
    069     */
    070    public class JThermometer extends JPanel implements Serializable {
    071
    072        /** For serialization. */
    073        private static final long serialVersionUID = 1079905665515589820L;
    074
    075        /** The dataset. */
    076        private DefaultValueDataset data;
    077
    078        /** The chart. */
    079        private JFreeChart chart;
    080
    081        /** The chart panel. */
    082        private ChartPanel panel;
    083
    084        /** The thermometer plot. */
    085        private ThermometerPlot plot = new ThermometerPlot();
    086
    087        /**
    088         * Default constructor.
    089         */
    090        public JThermometer() {
    091            super(new CardLayout());
    092            this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    093            this.data = new DefaultValueDataset();
    094            this.plot.setDataset(this.data);
    095            this.chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT,
    096                    this.plot, false);
    097            this.panel = new ChartPanel(this.chart);
    098            add(this.panel, "Panel");
    099            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    }