KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > swt > demo > SWTMultipleAxisDemo1


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------------
28  * SWTMultipleAxisDemo1.java
29  * -------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Henry Proudhon;
34  *
35  * Changes
36  * -------
37  * 23-Aug-2006 : New class (HP);
38  *
39  */

40
41 package org.jfree.experimental.chart.swt.demo;
42
43 import java.awt.Color JavaDoc;
44
45 import javax.swing.JPanel JavaDoc;
46
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.layout.FillLayout;
49 import org.eclipse.swt.widgets.Display;
50 import org.eclipse.swt.widgets.Shell;
51 import org.jfree.chart.ChartFactory;
52 import org.jfree.chart.ChartPanel;
53 import org.jfree.chart.JFreeChart;
54 import org.jfree.chart.axis.AxisLocation;
55 import org.jfree.chart.axis.NumberAxis;
56 import org.jfree.chart.plot.PlotOrientation;
57 import org.jfree.chart.plot.XYPlot;
58 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
59 import org.jfree.chart.renderer.xy.XYItemRenderer;
60 import org.jfree.chart.title.TextTitle;
61 import org.jfree.data.time.Minute;
62 import org.jfree.data.time.RegularTimePeriod;
63 import org.jfree.data.time.TimeSeries;
64 import org.jfree.data.time.TimeSeriesCollection;
65 import org.jfree.data.xy.XYDataset;
66 import org.jfree.experimental.chart.swt.ChartComposite;
67 import org.jfree.ui.RectangleInsets;
68
69 /**
70  * This demo shows a time series chart that has multiple range axes.
71  */

72 public class SWTMultipleAxisDemo1
73 {
74     /**
75      * Creates the demo chart.
76      *
77      * @return The chart.
78      */

79     private static JFreeChart createChart() {
80
81         XYDataset dataset1 = createDataset(
82             "Series 1", 100.0, new Minute(), 200
83         );
84         
85         JFreeChart chart = ChartFactory.createTimeSeriesChart(
86             "Multiple Axis Demo 3",
87             "Time of Day",
88             "Primary Range Axis",
89             dataset1,
90             true,
91             true,
92             false
93         );
94
95         chart.setBackgroundPaint( Color.white );
96         chart.setBorderVisible( true );
97         chart.setBorderPaint( Color.BLACK );
98         TextTitle subtitle = new TextTitle("Four datasets and four range axes.");
99         chart.addSubtitle( subtitle );
100         XYPlot plot = chart.getXYPlot();
101         plot.setOrientation(PlotOrientation.VERTICAL);
102         plot.setBackgroundPaint(Color.lightGray);
103         plot.setDomainGridlinePaint(Color.white);
104         plot.setRangeGridlinePaint(Color.white);
105         
106         plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
107         plot.getRangeAxis().setFixedDimension(15.0);
108         XYItemRenderer renderer = plot.getRenderer();
109         renderer.setPaint(Color.black);
110        
111         // AXIS 2
112
NumberAxis axis2 = new NumberAxis("Range Axis 2");
113         axis2.setFixedDimension(10.0);
114         axis2.setAutoRangeIncludesZero(false);
115         axis2.setLabelPaint(Color.red);
116         axis2.setTickLabelPaint(Color.red);
117         plot.setRangeAxis(1, axis2);
118         plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
119
120         XYDataset dataset2 = createDataset(
121             "Series 2", 1000.0, new Minute(), 170
122         );
123         plot.setDataset(1, dataset2);
124         plot.mapDatasetToRangeAxis(1, 1);
125         XYItemRenderer renderer2 = new StandardXYItemRenderer();
126         renderer2.setSeriesPaint(0, Color.red);
127         plot.setRenderer(1, renderer2);
128         
129         // AXIS 3
130
NumberAxis axis3 = new NumberAxis("Range Axis 3");
131         axis3.setLabelPaint(Color.blue);
132         axis3.setTickLabelPaint(Color.blue);
133         //axis3.setPositiveArrowVisible( true );
134
plot.setRangeAxis(2, axis3);
135
136         XYDataset dataset3 = createDataset(
137             "Series 3", 10000.0, new Minute(), 170
138         );
139         plot.setDataset(2, dataset3);
140         plot.mapDatasetToRangeAxis(2, 2);
141         XYItemRenderer renderer3 = new StandardXYItemRenderer();
142         renderer3.setSeriesPaint(0, Color.blue);
143         plot.setRenderer(2, renderer3);
144
145         // AXIS 4
146
NumberAxis axis4 = new NumberAxis("Range Axis 4");
147         axis4.setLabelPaint(Color.green);
148         axis4.setTickLabelPaint(Color.green);
149         plot.setRangeAxis(3, axis4);
150         
151         XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200);
152         plot.setDataset(3, dataset4);
153         plot.mapDatasetToRangeAxis(3, 3);
154         
155         XYItemRenderer renderer4 = new StandardXYItemRenderer();
156         renderer4.setSeriesPaint(0, Color.green);
157         plot.setRenderer(3, renderer4);
158                 
159         return chart;
160     }
161     
162     /**
163      * Creates a sample dataset.
164      *
165      * @param name the dataset name.
166      * @param base the starting value.
167      * @param start the starting period.
168      * @param count the number of values to generate.
169      *
170      * @return The dataset.
171      */

172     private static XYDataset createDataset(String JavaDoc name, double base,
173                                            RegularTimePeriod start, int count) {
174
175         TimeSeries series = new TimeSeries(name, start.getClass());
176         RegularTimePeriod period = start;
177         double value = base;
178         for (int i = 0; i < count; i++) {
179             series.add(period, value);
180             period = period.next();
181             value = value * (1 + (Math.random() - 0.495) / 10.0);
182         }
183
184         TimeSeriesCollection dataset = new TimeSeriesCollection();
185         dataset.addSeries(series);
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 JavaDoc createDemoPanel() {
197         JFreeChart chart = createChart();
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 JavaDoc[] args )
207     {
208         final JFreeChart chart = createChart();
209         final Display display = new Display();
210         Shell shell = new Shell( display );
211         shell.setSize( 600, 300 );
212         shell.setLayout( new FillLayout() );
213         shell.setText( "Test for jfreechart running with SWT" );
214         ChartComposite frame = new ChartComposite( shell, SWT.NONE, null, true );
215         // by using setChart, the chartComposite is made to listen to chart changes
216
frame.setChart( chart );
217         shell.open();
218         while ( !shell.isDisposed() )
219         {
220             if ( !display.readAndDispatch() )
221                 display.sleep();
222         }
223     }
224
225 }
226
227
Popular Tags