    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -----------------
    028     * JThermometer.java
    029     * -----------------
    030     * A plot that displays a single value in a thermometer type display.
    031     *
    032     * (C) Copyright 2000-2005, 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     * Copyright (c) 2002
    071     * Australian Antarctic Division
    072     *
    073     * @author Bryan Scott
    074     */
    075    public class JThermometer extends JPanel implements Serializable {
    076
    077        /** For serialization. */
    078        private static final long serialVersionUID = 1079905665515589820L;
    079
    080        /** The dataset. */
    081        private DefaultValueDataset data;
    082
    083        /** The chart. */
    084        private JFreeChart chart;
    085
    086        /** The chart panel. */
    087        private ChartPanel panel;
    088
    089        /** The thermometer plot. */
    090        private ThermometerPlot plot = new ThermometerPlot();
    091
    092        /**
    093         * Default constructor.
    094         */
    095        public JThermometer() {
    096            super(new CardLayout());
    097            this.plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    098            this.data = new DefaultValueDataset();
    099            //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    }