KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MultipleAxisDemo3.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: MultipleAxisDemo3.java,v 1.2 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 16-Sep-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.axis.AxisLocation;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.plot.PlotOrientation;
49 import org.jfree.chart.plot.XYPlot;
50 import org.jfree.chart.renderer.StandardXYItemRenderer;
51 import org.jfree.data.XYDataset;
52 import org.jfree.data.time.Minute;
53 import org.jfree.data.time.RegularTimePeriod;
54 import org.jfree.data.time.TimeSeries;
55 import org.jfree.data.time.TimeSeriesCollection;
56 import org.jfree.ui.ApplicationFrame;
57 import org.jfree.ui.RefineryUtilities;
58
59 /**
60  * An example of....
61  *
62  * @author David Gilbert
63  */

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

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

86     private JFreeChart createChart() {
87
88         XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 200);
89         
90         JFreeChart chart = ChartFactory.createTimeSeriesChart(
91             "Multiple Axis Demo 2",
92             "Time of Day",
93             "Primary Range Axis",
94             dataset1,
95             true,
96             true,
97             false
98         );
99
100         chart.setBackgroundPaint(Color.white);
101         XYPlot plot = chart.getXYPlot();
102         plot.setOrientation(PlotOrientation.VERTICAL);
103         plot.setBackgroundPaint(Color.lightGray);
104         plot.setDomainGridlinePaint(Color.white);
105         plot.setRangeGridlinePaint(Color.white);
106         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
107         
108         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
109         renderer.setPaint(Color.black);
110        
111         // DOMAIN AXIS 2
112
NumberAxis xAxis2 = new NumberAxis("Domain Axis 2");
113         xAxis2.setAutoRangeIncludesZero(false);
114         plot.setSecondaryDomainAxis(0, xAxis2);
115         plot.setSecondaryDomainAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
116        
117         // DOMAIN AXIS 3
118
NumberAxis xAxis3 = new NumberAxis("Domain Axis 3");
119         xAxis2.setAutoRangeIncludesZero(false);
120         plot.setSecondaryDomainAxis(1, xAxis3);
121         plot.setSecondaryDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
122
123         // RANGE AXIS 2
124
NumberAxis yAxis2 = new NumberAxis("Range Axis 2");
125         plot.setSecondaryRangeAxis(0, yAxis2);
126         plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT);
127
128         XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170);
129         plot.setSecondaryDataset(0, dataset2);
130         plot.mapSecondaryDatasetToDomainAxis(0, new Integer JavaDoc(0));
131         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
132         
133         return chart;
134         
135     }
136     
137     // ****************************************************************************
138
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
139
// * Please note that commercial support and documentation is available from: *
140
// * *
141
// * http://www.object-refinery.com/jfreechart/support.html *
142
// * *
143
// * This is not only a great service for developers, but is a VERY IMPORTANT *
144
// * source of funding for the JFreeChart project. Please support us so that *
145
// * we can continue developing free software. *
146
// ****************************************************************************
147

148     /**
149      * Creates a sample dataset.
150      *
151      * @param name the dataset name.
152      * @param base the starting value.
153      * @param start the starting period.
154      * @param count the number of values to generate.
155      *
156      * @return The dataset.
157      */

158     private XYDataset createDataset(String JavaDoc name, double base, RegularTimePeriod start, int count) {
159
160         TimeSeries series = new TimeSeries(name, start.getClass());
161         RegularTimePeriod period = start;
162         double value = base;
163         for (int i = 0; i < count; i++) {
164             series.add(period, value);
165             period = period.next();
166             value = value * (1 + (Math.random() - 0.495) / 10.0);
167         }
168
169         TimeSeriesCollection dataset = new TimeSeriesCollection();
170         dataset.addSeries(series);
171
172         return dataset;
173
174     }
175
176     /**
177      * Starting point for the demonstration application.
178      *
179      * @param args ignored.
180      */

181     public static void main(String JavaDoc[] args) {
182
183         MultipleAxisDemo3 demo = new MultipleAxisDemo3("Multiple Axis Demo 3");
184         demo.pack();
185         RefineryUtilities.centerFrameOnScreen(demo);
186         demo.setVisible(true);
187
188     }
189
190 }
191
Popular Tags