    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, 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     * TimeSeriesChartDemo1.java
    029     * -------------------------
    030     * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   ;
    034     *
    035     * $Id: TimeSeriesChartDemo1.java,v 1.2.2.6 2007/01/17 15:16:43 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
    040     *               the JFreeChart Developer Guide (DG);
    041     *
    042     */
    043
    044    package org.jfree.chart.demo;
    045
    046    import java.awt.Color;
    047    import java.text.SimpleDateFormat;
    048
    049    import javax.swing.JPanel;
    050
    051    import org.jfree.chart.ChartFactory;
    052    import org.jfree.chart.ChartPanel;
    053    import org.jfree.chart.JFreeChart;
    054    import org.jfree.chart.axis.DateAxis;
    055    import org.jfree.chart.plot.XYPlot;
    056    import org.jfree.chart.renderer.xy.XYItemRenderer;
    057    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
    058    import org.jfree.data.time.Month;
    059    import org.jfree.data.time.TimeSeries;
    060    import org.jfree.data.time.TimeSeriesCollection;
    061    import org.jfree.data.xy.XYDataset;
    062    import org.jfree.ui.ApplicationFrame;
    063    import org.jfree.ui.RectangleInsets;
    064    import org.jfree.ui.RefineryUtilities;
    065
    066    /**
    067     * An example of a time series chart.  For the most part, default settings are
    068     * used, except that the renderer is modified to show filled shapes (as well as
    069     * lines) at each data point.
    070     */
    071    public class TimeSeriesChartDemo1 extends ApplicationFrame {
    072
    073        /**
    074         * A demonstration application showing how to create a simple time series
    075         * chart.  This example uses monthly data.
    076         *
    077         * @param title  the frame title.
    078         */
    079        public TimeSeriesChartDemo1(String title) {
    080            super(title);
    081            ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    082            chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    083            chartPanel.setMouseZoomable(true, false);
    084            setContentPane(chartPanel);
    085        }
    086
    087        /**
    088         * Creates a chart.
    089         *
    090         * @param dataset  a dataset.
    091         *
    092         * @return A chart.
    093         */
    094        private static JFreeChart createChart(XYDataset dataset) {
    095
    096            JFreeChart chart = ChartFactory.createTimeSeriesChart(
    097                "Legal & General Unit Trust Prices",  // title
    098                "Date",             // x-axis label
    099                "Price Per Unit",   // y-axis label
    100                dataset,            // data
    101                true,               // create legend?
    102                true,               // generate tooltips?
    103                false               // generate URLs?
    104            );
    105
    106            chart.setBackgroundPaint(Color.white);
    107
    108            XYPlot plot = (XYPlot) chart.getPlot();
    109            plot.setBackgroundPaint(Color.lightGray);
    110            plot.setDomainGridlinePaint(Color.white);
    111            plot.setRangeGridlinePaint(Color.white);
    112            plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    113            plot.setDomainCrosshairVisible(true);
    114            plot.setRangeCrosshairVisible(true);
    115
    116            XYItemRenderer r = plot.getRenderer();
    117            if (r instanceof XYLineAndShapeRenderer) {
    118                XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
    119                renderer.setBaseShapesVisible(true);
    120                renderer.setBaseShapesFilled(true);
    121            }
    122
    123            DateAxis axis = (DateAxis) plot.getDomainAxis();
    124            axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
    125
    126            return chart;
    127
    128        }
    129
    130        /**
    131         * Creates a dataset, consisting of two series of monthly data.
    132         *
    133         * @return The dataset.
    134         */
    135        private static XYDataset createDataset() {
    136
    137            TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
    138            s1.add(new Month(2, 2001), 181.8);
    139            s1.add(new Month(3, 2001), 167.3);
    140            s1.add(new Month(4, 2001), 153.8);
    141            s1.add(new Month(5, 2001), 167.6);
    142            s1.add(new Month(6, 2001), 158.8);
    143            s1.add(new Month(7, 2001), 148.3);
    144            s1.add(new Month(8, 2001), 153.9);
    145            s1.add(new Month(9, 2001), 142.7);
    146            s1.add(new Month(10, 2001), 123.2);
    147            s1.add(new Month(11, 2001), 131.8);
    148            s1.add(new Month(12, 2001), 139.6);
    149            s1.add(new Month(1, 2002), 142.9);
    150            s1.add(new Month(2, 2002), 138.7);
    151            s1.add(new Month(3, 2002), 137.3);
    152            s1.add(new Month(4, 2002), 143.9);
    153            s1.add(new Month(5, 2002), 139.8);
    154            s1.add(new Month(6, 2002), 137.0);
    155            s1.add(new Month(7, 2002), 132.8);
    156
    157            TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
    158            s2.add(new Month(2, 2001), 129.6);
    159            s2.add(new Month(3, 2001), 123.2);
    160            s2.add(new Month(4, 2001), 117.2);
    161            s2.add(new Month(5, 2001), 124.1);
    162            s2.add(new Month(6, 2001), 122.6);
    163            s2.add(new Month(7, 2001), 119.2);
    164            s2.add(new Month(8, 2001), 116.5);
    165            s2.add(new Month(9, 2001), 112.7);
    166            s2.add(new Month(10, 2001), 101.5);
    167            s2.add(new Month(11, 2001), 106.1);
    168            s2.add(new Month(12, 2001), 110.3);
    169            s2.add(new Month(1, 2002), 111.7);
    170            s2.add(new Month(2, 2002), 111.0);
    171            s2.add(new Month(3, 2002), 109.6);
    172            s2.add(new Month(4, 2002), 113.2);
    173            s2.add(new Month(5, 2002), 111.6);
    174            s2.add(new Month(6, 2002), 108.8);
    175            s2.add(new Month(7, 2002), 101.6);
    176
    177            // ******************************************************************
    178            //  More than 150 demo applications are included with the JFreeChart
    179            //  Developer Guide...for more information, see:
    180            //
    181            //  >   http://www.object-refinery.com/jfreechart/guide.html
    182            //
    183            // ******************************************************************
    184
    185            TimeSeriesCollection dataset = new TimeSeriesCollection();
    186            dataset.addSeries(s1);
    187            dataset.addSeries(s2);
    188
    189            return dataset;
    190
    191        }
    192
    193        /**
    194         * Creates a panel for the demo (used by SuperDemo.java).
    195         *
    196         * @return A panel.
    197         */
    198        public static JPanel createDemoPanel() {
    199            JFreeChart chart = createChart(createDataset());
    200            return new ChartPanel(chart);
    201        }
    202
    203        /**
    204         * Starting point for the demonstration application.
    205         *
    206         * @param args  ignored.
    207         */
    208        public static void main(String[] args) {
    209
    210            TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1(
    211                    "Time Series Chart Demo 1");
    212            demo.pack();
    213            RefineryUtilities.centerFrameOnScreen(demo);
    214            demo.setVisible(true);
    215
    216        }
    217
    218    }