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 * SWTTimeSeriesDemo.java
029 * ---------------------
030 * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Henry Proudhon (henry.proudhon AT ensmp.fr);
034 *
035 * Changes
036 * -------
037 * 30-Jan-2007 : New class derived from TimeSeriesDemo.java (HP);
038 *
039 */
040 package org.jfree.experimental.chart.swt.demo;
041
042 import java.awt.Color;
043 import java.text.SimpleDateFormat;
044
045 import org.eclipse.swt.SWT;
046 import org.eclipse.swt.layout.FillLayout;
047 import org.eclipse.swt.widgets.Display;
048 import org.eclipse.swt.widgets.Shell;
049 import org.jfree.chart.ChartFactory;
050 import org.jfree.chart.JFreeChart;
051 import org.jfree.chart.axis.DateAxis;
052 import org.jfree.chart.plot.XYPlot;
053 import org.jfree.chart.renderer.xy.XYItemRenderer;
054 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
055 import org.jfree.data.time.Month;
056 import org.jfree.data.time.TimeSeries;
057 import org.jfree.data.time.TimeSeriesCollection;
058 import org.jfree.data.xy.XYDataset;
059 import org.jfree.experimental.chart.swt.ChartComposite;
060 import org.jfree.ui.RectangleInsets;
061
062 /**
063 * An example of a time series chart. For the most part, default settings are
064 * used, except that the renderer is modified to show filled shapes (as well as
065 * lines) at each data point.
066 */
067 public class SWTTimeSeriesDemo
068 {
069
070 /**
071 * Creates a chart.
072 *
073 * @param dataset a dataset.
074 *
075 * @return A chart.
076 */
077 private static JFreeChart createChart(XYDataset dataset) {
078
079 JFreeChart chart = ChartFactory.createTimeSeriesChart(
080 "Legal & General Unit Trust Prices", // title
081 "Date", // x-axis label
082 "Price Per Unit", // y-axis label
083 dataset, // data
084 true, // create legend?
085 true, // generate tooltips?
086 false // generate URLs?
087 );
088
089 chart.setBackgroundPaint(Color.white);
090
091 XYPlot plot = (XYPlot) chart.getPlot();
092 plot.setBackgroundPaint(Color.lightGray);
093 plot.setDomainGridlinePaint(Color.white);
094 plot.setRangeGridlinePaint(Color.white);
095 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
096 plot.setDomainCrosshairVisible(true);
097 plot.setRangeCrosshairVisible(true);
098
099 XYItemRenderer r = plot.getRenderer();
100 if (r instanceof XYLineAndShapeRenderer) {
101 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
102 renderer.setBaseShapesVisible(true);
103 renderer.setBaseShapesFilled(true);
104 }
105
106 DateAxis axis = (DateAxis) plot.getDomainAxis();
107 axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
108
109 return chart;
110
111 }
112
113 /**
114 * Creates a dataset, consisting of two series of monthly data.
115 *
116 * @return The dataset.
117 */
118 private static XYDataset createDataset() {
119
120 TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
121 s1.add(new Month(2, 2001), 181.8);
122 s1.add(new Month(3, 2001), 167.3);
123 s1.add(new Month(4, 2001), 153.8);
124 s1.add(new Month(5, 2001), 167.6);
125 s1.add(new Month(6, 2001), 158.8);
126 s1.add(new Month(7, 2001), 148.3);
127 s1.add(new Month(8, 2001), 153.9);
128 s1.add(new Month(9, 2001), 142.7);
129 s1.add(new Month(10, 2001), 123.2);
130 s1.add(new Month(11, 2001), 131.8);
131 s1.add(new Month(12, 2001), 139.6);
132 s1.add(new Month(1, 2002), 142.9);
133 s1.add(new Month(2, 2002), 138.7);
134 s1.add(new Month(3, 2002), 137.3);
135 s1.add(new Month(4, 2002), 143.9);
136 s1.add(new Month(5, 2002), 139.8);
137 s1.add(new Month(6, 2002), 137.0);
138 s1.add(new Month(7, 2002), 132.8);
139
140 TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
141 s2.add(new Month(2, 2001), 129.6);
142 s2.add(new Month(3, 2001), 123.2);
143 s2.add(new Month(4, 2001), 117.2);
144 s2.add(new Month(5, 2001), 124.1);
145 s2.add(new Month(6, 2001), 122.6);
146 s2.add(new Month(7, 2001), 119.2);
147 s2.add(new Month(8, 2001), 116.5);
148 s2.add(new Month(9, 2001), 112.7);
149 s2.add(new Month(10, 2001), 101.5);
150 s2.add(new Month(11, 2001), 106.1);
151 s2.add(new Month(12, 2001), 110.3);
152 s2.add(new Month(1, 2002), 111.7);
153 s2.add(new Month(2, 2002), 111.0);
154 s2.add(new Month(3, 2002), 109.6);
155 s2.add(new Month(4, 2002), 113.2);
156 s2.add(new Month(5, 2002), 111.6);
157 s2.add(new Month(6, 2002), 108.8);
158 s2.add(new Month(7, 2002), 101.6);
159
160 TimeSeriesCollection dataset = new TimeSeriesCollection();
161 dataset.addSeries(s1);
162 dataset.addSeries(s2);
163
164 return dataset;
165 }
166
167 /**
168 * Starting point for the demonstration application.
169 *
170 * @param args ignored.
171 */
172 public static void main(String[] args) {
173 final JFreeChart chart = createChart(createDataset());
174 final Display display = new Display();
175 Shell shell = new Shell(display);
176 shell.setSize(600, 300);
177 shell.setLayout(new FillLayout());
178 shell.setText("Time series demo for jfreechart running with SWT");
179 ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart, true);
180 frame.setDisplayToolTips(true);
181 frame.setHorizontalAxisTrace(false);
182 frame.setVerticalAxisTrace(false);
183 shell.open();
184 while (!shell.isDisposed()) {
185 if (!display.readAndDispatch())
186 display.sleep();
187 }
188 }
189
190 }