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