Source for org.jfree.chart.demo.TimeSeriesChartDemo1

   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:  * TimeSeriesChartDemo1.java
  29:  * -------------------------
  30:  * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   ;
  34:  *
  35:  * $Id: TimeSeriesChartDemo1.java,v 1.2.2.6 2007/01/17 15:16:43 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
  40:  *               the JFreeChart Developer Guide (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.demo;
  45: 
  46: import java.awt.Color;
  47: import java.text.SimpleDateFormat;
  48: 
  49: import javax.swing.JPanel;
  50: 
  51: import org.jfree.chart.ChartFactory;
  52: import org.jfree.chart.ChartPanel;
  53: import org.jfree.chart.JFreeChart;
  54: import org.jfree.chart.axis.DateAxis;
  55: import org.jfree.chart.plot.XYPlot;
  56: import org.jfree.chart.renderer.xy.XYItemRenderer;
  57: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  58: import org.jfree.data.time.Month;
  59: import org.jfree.data.time.TimeSeries;
  60: import org.jfree.data.time.TimeSeriesCollection;
  61: import org.jfree.data.xy.XYDataset;
  62: import org.jfree.ui.ApplicationFrame;
  63: import org.jfree.ui.RectangleInsets;
  64: import org.jfree.ui.RefineryUtilities;
  65: 
  66: /**
  67:  * An example of a time series chart.  For the most part, default settings are 
  68:  * used, except that the renderer is modified to show filled shapes (as well as 
  69:  * lines) at each data point.
  70:  */
  71: public class TimeSeriesChartDemo1 extends ApplicationFrame {
  72: 
  73:     /**
  74:      * A demonstration application showing how to create a simple time series 
  75:      * chart.  This example uses monthly data.
  76:      *
  77:      * @param title  the frame title.
  78:      */
  79:     public TimeSeriesChartDemo1(String title) {
  80:         super(title);
  81:         ChartPanel chartPanel = (ChartPanel) createDemoPanel();
  82:         chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
  83:         chartPanel.setMouseZoomable(true, false);
  84:         setContentPane(chartPanel);
  85:     }
  86: 
  87:     /**
  88:      * Creates a chart.
  89:      * 
  90:      * @param dataset  a dataset.
  91:      * 
  92:      * @return A chart.
  93:      */
  94:     private static JFreeChart createChart(XYDataset dataset) {
  95: 
  96:         JFreeChart chart = ChartFactory.createTimeSeriesChart(
  97:             "Legal & General Unit Trust Prices",  // title
  98:             "Date",             // x-axis label
  99:             "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: }