KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > MultipleAxisDemo1


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ----------------------
23  * MultipleAxisDemo1.java
24  * ----------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: MultipleAxisDemo1.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 15-Jul-2002 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.Spacer;
46 import org.jfree.chart.TextTitle;
47 import org.jfree.chart.axis.AxisLocation;
48 import org.jfree.chart.axis.NumberAxis;
49 import org.jfree.chart.plot.PlotOrientation;
50 import org.jfree.chart.plot.XYPlot;
51 import org.jfree.chart.renderer.StandardXYItemRenderer;
52 import org.jfree.data.XYDataset;
53 import org.jfree.data.time.Minute;
54 import org.jfree.data.time.RegularTimePeriod;
55 import org.jfree.data.time.TimeSeries;
56 import org.jfree.data.time.TimeSeriesCollection;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RefineryUtilities;
59
60 /**
61  * An example of....
62  *
63  * @author David Gilbert
64  */

65 public class MultipleAxisDemo1 extends ApplicationFrame {
66
67     /**
68      * A demonstration application showing how to create a time series chart with muliple axes.
69      *
70      * @param title the frame title.
71      */

72     public MultipleAxisDemo1(String JavaDoc title) {
73
74         super(title);
75         JFreeChart chart = createChart();
76         ChartPanel chartPanel = new ChartPanel(chart);
77         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(600, 270));
78         chartPanel.setHorizontalZoom(true);
79         chartPanel.setVerticalZoom(true);
80         setContentPane(chartPanel);
81
82     }
83
84     /**
85      * Creates the demo chart.
86      *
87      * @return The chart.
88      */

89     private JFreeChart createChart() {
90
91         XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 200);
92         
93         JFreeChart chart = ChartFactory.createTimeSeriesChart(
94             "Multiple Axis Demo 1",
95             "Time of Day",
96             "Primary Range Axis",
97             dataset1,
98             true,
99             true,
100             false
101         );
102
103         chart.setBackgroundPaint(Color.white);
104         chart.addSubtitle(new TextTitle("Four datasets and four range axes."));
105         XYPlot plot = chart.getXYPlot();
106         plot.setOrientation(PlotOrientation.VERTICAL);
107         plot.setBackgroundPaint(Color.lightGray);
108         plot.setDomainGridlinePaint(Color.white);
109         plot.setRangeGridlinePaint(Color.white);
110         
111         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
112         
113         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
114         renderer.setPaint(Color.black);
115        
116         // AXIS 2
117
NumberAxis axis2 = new NumberAxis("Range Axis 2");
118         axis2.setAutoRangeIncludesZero(false);
119         axis2.setLabelPaint(Color.red);
120         axis2.setTickLabelPaint(Color.red);
121         plot.setSecondaryRangeAxis(0, axis2);
122         plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
123
124         XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170);
125         plot.setSecondaryDataset(0, dataset2);
126         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
127         plot.setSecondaryRenderer(0, new StandardXYItemRenderer());
128         plot.getSecondaryRenderer(0).setSeriesPaint(0, Color.red);
129         
130         // AXIS 3
131
NumberAxis axis3 = new NumberAxis("Range Axis 3");
132         axis3.setLabelPaint(Color.blue);
133         axis3.setTickLabelPaint(Color.blue);
134         plot.setSecondaryRangeAxis(1, axis3);
135
136         XYDataset dataset3 = createDataset("Series 3", 10000.0, new Minute(), 170);
137         plot.setSecondaryDataset(1, dataset3);
138         plot.mapSecondaryDatasetToRangeAxis(1, new Integer JavaDoc(1));
139         
140         plot.setSecondaryRenderer(1, new StandardXYItemRenderer());
141         plot.getSecondaryRenderer(1).setSeriesPaint(0, Color.blue);
142
143         // AXIS 4
144
NumberAxis axis4 = new NumberAxis("Range Axis 4");
145         axis4.setLabelPaint(Color.green);
146         axis4.setTickLabelPaint(Color.green);
147         plot.setSecondaryRangeAxis(2, axis4);
148         
149         XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200);
150         plot.setSecondaryDataset(2, dataset4);
151         plot.mapSecondaryDatasetToRangeAxis(2, new Integer JavaDoc(2));
152         
153         plot.setSecondaryRenderer(2, new StandardXYItemRenderer());
154         plot.getSecondaryRenderer(2).setSeriesPaint(0, Color.green);
155                 
156         return chart;
157     }
158     
159     // ****************************************************************************
160
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
161
// * Please note that commercial support and documentation is available from: *
162
// * *
163
// * http://www.object-refinery.com/jfreechart/support.html *
164
// * *
165
// * This is not only a great service for developers, but is a VERY IMPORTANT *
166
// * source of funding for the JFreeChart project. Please support us so that *
167
// * we can continue developing free software. *
168
// ****************************************************************************
169

170     /**
171      * Creates a sample dataset.
172      *
173      * @param name the dataset name.
174      * @param base the starting value.
175      * @param start the starting period.
176      * @param count the number of values to generate.
177      *
178      * @return The dataset.
179      */

180     private XYDataset createDataset(String JavaDoc name, double base, RegularTimePeriod start, int count) {
181
182         TimeSeries series = new TimeSeries(name, start.getClass());
183         RegularTimePeriod period = start;
184         double value = base;
185         for (int i = 0; i < count; i++) {
186             series.add(period, value);
187             period = period.next();
188             value = value * (1 + (Math.random() - 0.495) / 10.0);
189         }
190
191         TimeSeriesCollection dataset = new TimeSeriesCollection();
192         dataset.addSeries(series);
193
194         return dataset;
195
196     }
197
198     /**
199      * Starting point for the demonstration application.
200      *
201      * @param args ignored.
202      */

203     public static void main(String JavaDoc[] args) {
204
205         MultipleAxisDemo1 demo = new MultipleAxisDemo1("Multiple Axis Demo 1");
206         demo.pack();
207         RefineryUtilities.centerFrameOnScreen(demo);
208         demo.setVisible(true);
209
210     }
211
212 }
213
Popular Tags