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