001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Henry Proudhon (henry.proudhon AT insa-lyon.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: * Creates a chart.
071: *
072: * @param dataset a dataset.
073: *
074: * @return A chart.
075: */
076: private static JFreeChart createChart(XYDataset dataset) {
077:
078: JFreeChart chart = ChartFactory.createTimeSeriesChart(
079: "Legal & General Unit Trust Prices", // title
080: "Date", // x-axis label
081: "Price Per Unit", // y-axis label
082: dataset, // data
083: true, // create legend?
084: true, // generate tooltips?
085: false // generate URLs?
086: );
087:
088: chart.setBackgroundPaint(Color.white);
089:
090: XYPlot plot = (XYPlot) chart.getPlot();
091: plot.setBackgroundPaint(Color.lightGray);
092: plot.setDomainGridlinePaint(Color.white);
093: plot.setRangeGridlinePaint(Color.white);
094: plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
095: plot.setDomainCrosshairVisible(true);
096: plot.setRangeCrosshairVisible(true);
097:
098: XYItemRenderer r = plot.getRenderer();
099: if (r instanceof XYLineAndShapeRenderer) {
100: XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
101: renderer.setBaseShapesVisible(true);
102: renderer.setBaseShapesFilled(true);
103: }
104:
105: DateAxis axis = (DateAxis) plot.getDomainAxis();
106: axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
107:
108: return chart;
109:
110: }
111:
112: /**
113: * Creates a dataset, consisting of two series of monthly data.
114: *
115: * @return The dataset.
116: */
117: private static XYDataset createDataset() {
118:
119: TimeSeries s1 = new TimeSeries("L&G European Index Trust",
120: 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",
141: Month.class);
142: s2.add(new Month(2, 2001), 129.6);
143: s2.add(new Month(3, 2001), 123.2);
144: s2.add(new Month(4, 2001), 117.2);
145: s2.add(new Month(5, 2001), 124.1);
146: s2.add(new Month(6, 2001), 122.6);
147: s2.add(new Month(7, 2001), 119.2);
148: s2.add(new Month(8, 2001), 116.5);
149: s2.add(new Month(9, 2001), 112.7);
150: s2.add(new Month(10, 2001), 101.5);
151: s2.add(new Month(11, 2001), 106.1);
152: s2.add(new Month(12, 2001), 110.3);
153: s2.add(new Month(1, 2002), 111.7);
154: s2.add(new Month(2, 2002), 111.0);
155: s2.add(new Month(3, 2002), 109.6);
156: s2.add(new Month(4, 2002), 113.2);
157: s2.add(new Month(5, 2002), 111.6);
158: s2.add(new Month(6, 2002), 108.8);
159: s2.add(new Month(7, 2002), 101.6);
160:
161: TimeSeriesCollection dataset = new TimeSeriesCollection();
162: dataset.addSeries(s1);
163: dataset.addSeries(s2);
164:
165: return dataset;
166: }
167:
168: /**
169: * Starting point for the demonstration application.
170: *
171: * @param args ignored.
172: */
173: public static void main(String[] args) {
174: final JFreeChart chart = createChart(createDataset());
175: final Display display = new Display();
176: Shell shell = new Shell(display);
177: shell.setSize(600, 300);
178: shell.setLayout(new FillLayout());
179: shell
180: .setText("Time series demo for jfreechart running with SWT");
181: ChartComposite frame = new ChartComposite(shell, SWT.NONE,
182: chart, true);
183: frame.setDisplayToolTips(true);
184: frame.setHorizontalAxisTrace(false);
185: frame.setVerticalAxisTrace(false);
186: shell.open();
187: while (!shell.isDisposed()) {
188: if (!display.readAndDispatch())
189: display.sleep();
190: }
191: }
192:
193: }
|