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: * SWTMultipleAxisDemo1.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;
034: *
035: * Changes
036: * -------
037: * 23-Aug-2006 : New class (HP);
038: *
039: */
040:
041: package org.jfree.experimental.chart.swt.demo;
042:
043: import java.awt.Color;
044:
045: import javax.swing.JPanel;
046:
047: import org.eclipse.swt.SWT;
048: import org.eclipse.swt.layout.FillLayout;
049: import org.eclipse.swt.widgets.Display;
050: import org.eclipse.swt.widgets.Shell;
051: import org.jfree.chart.ChartFactory;
052: import org.jfree.chart.ChartPanel;
053: import org.jfree.chart.JFreeChart;
054: import org.jfree.chart.axis.AxisLocation;
055: import org.jfree.chart.axis.NumberAxis;
056: import org.jfree.chart.plot.PlotOrientation;
057: import org.jfree.chart.plot.XYPlot;
058: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
059: import org.jfree.chart.renderer.xy.XYItemRenderer;
060: import org.jfree.chart.title.TextTitle;
061: import org.jfree.data.time.Minute;
062: import org.jfree.data.time.RegularTimePeriod;
063: import org.jfree.data.time.TimeSeries;
064: import org.jfree.data.time.TimeSeriesCollection;
065: import org.jfree.data.xy.XYDataset;
066: import org.jfree.experimental.chart.swt.ChartComposite;
067: import org.jfree.ui.RectangleInsets;
068:
069: /**
070: * This demo shows a time series chart that has multiple range axes.
071: */
072: public class SWTMultipleAxisDemo1 {
073: /**
074: * Creates the demo chart.
075: *
076: * @return The chart.
077: */
078: private static JFreeChart createChart() {
079:
080: XYDataset dataset1 = createDataset("Series 1", 100.0,
081: new Minute(), 200);
082:
083: JFreeChart chart = ChartFactory.createTimeSeriesChart(
084: "Multiple Axis Demo 3", "Time of Day",
085: "Primary Range Axis", dataset1, true, true, false);
086:
087: chart.setBackgroundPaint(Color.white);
088: chart.setBorderVisible(true);
089: chart.setBorderPaint(Color.BLACK);
090: TextTitle subtitle = new TextTitle(
091: "Four datasets and four range axes.");
092: chart.addSubtitle(subtitle);
093: XYPlot plot = (XYPlot) chart.getPlot();
094: plot.setOrientation(PlotOrientation.VERTICAL);
095: plot.setBackgroundPaint(Color.lightGray);
096: plot.setDomainGridlinePaint(Color.white);
097: plot.setRangeGridlinePaint(Color.white);
098:
099: plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
100: plot.getRangeAxis().setFixedDimension(15.0);
101: XYItemRenderer renderer = plot.getRenderer();
102: renderer.setSeriesPaint(0, Color.black);
103:
104: // AXIS 2
105: NumberAxis axis2 = new NumberAxis("Range Axis 2");
106: axis2.setFixedDimension(10.0);
107: axis2.setAutoRangeIncludesZero(false);
108: axis2.setLabelPaint(Color.red);
109: axis2.setTickLabelPaint(Color.red);
110: plot.setRangeAxis(1, axis2);
111: plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
112:
113: XYDataset dataset2 = createDataset("Series 2", 1000.0,
114: new Minute(), 170);
115: plot.setDataset(1, dataset2);
116: plot.mapDatasetToRangeAxis(1, 1);
117: XYItemRenderer renderer2 = new StandardXYItemRenderer();
118: renderer2.setSeriesPaint(0, Color.red);
119: plot.setRenderer(1, renderer2);
120:
121: // AXIS 3
122: NumberAxis axis3 = new NumberAxis("Range Axis 3");
123: axis3.setLabelPaint(Color.blue);
124: axis3.setTickLabelPaint(Color.blue);
125: //axis3.setPositiveArrowVisible( true );
126: plot.setRangeAxis(2, axis3);
127:
128: XYDataset dataset3 = createDataset("Series 3", 10000.0,
129: new Minute(), 170);
130: plot.setDataset(2, dataset3);
131: plot.mapDatasetToRangeAxis(2, 2);
132: XYItemRenderer renderer3 = new StandardXYItemRenderer();
133: renderer3.setSeriesPaint(0, Color.blue);
134: plot.setRenderer(2, renderer3);
135:
136: // AXIS 4
137: NumberAxis axis4 = new NumberAxis("Range Axis 4");
138: axis4.setLabelPaint(Color.green);
139: axis4.setTickLabelPaint(Color.green);
140: plot.setRangeAxis(3, axis4);
141:
142: XYDataset dataset4 = createDataset("Series 4", 25.0,
143: new Minute(), 200);
144: plot.setDataset(3, dataset4);
145: plot.mapDatasetToRangeAxis(3, 3);
146:
147: XYItemRenderer renderer4 = new StandardXYItemRenderer();
148: renderer4.setSeriesPaint(0, Color.green);
149: plot.setRenderer(3, renderer4);
150:
151: return chart;
152: }
153:
154: /**
155: * Creates a sample dataset.
156: *
157: * @param name the dataset name.
158: * @param base the starting value.
159: * @param start the starting period.
160: * @param count the number of values to generate.
161: *
162: * @return The dataset.
163: */
164: private static XYDataset createDataset(String name, double base,
165: RegularTimePeriod start, int count) {
166:
167: TimeSeries series = new TimeSeries(name, start.getClass());
168: RegularTimePeriod period = start;
169: double value = base;
170: for (int i = 0; i < count; i++) {
171: series.add(period, value);
172: period = period.next();
173: value = value * (1 + (Math.random() - 0.495) / 10.0);
174: }
175:
176: TimeSeriesCollection dataset = new TimeSeriesCollection();
177: dataset.addSeries(series);
178:
179: return dataset;
180:
181: }
182:
183: /**
184: * Creates a panel for the demo (used by SuperDemo.java).
185: *
186: * @return A panel.
187: */
188: public static JPanel createDemoPanel() {
189: JFreeChart chart = createChart();
190: return new ChartPanel(chart);
191: }
192:
193: /**
194: * Starting point for the demonstration application.
195: *
196: * @param args ignored.
197: */
198: public static void main(String[] args) {
199: final JFreeChart chart = createChart();
200: final Display display = new Display();
201: Shell shell = new Shell(display);
202: shell.setSize(600, 300);
203: shell.setLayout(new FillLayout());
204: shell.setText("Test for jfreechart running with SWT");
205: ChartComposite frame = new ChartComposite(shell, SWT.NONE,
206: chart, true);
207: frame.setDisplayToolTips(false);
208: frame.setHorizontalAxisTrace(true);
209: frame.setVerticalAxisTrace(true);
210: shell.open();
211: while (!shell.isDisposed()) {
212: if (!display.readAndDispatch())
213: display.sleep();
214: }
215: }
216:
217: }
|