KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MultipleAxisDemo2.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: MultipleAxisDemo2.java,v 1.5 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.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 MultipleAxisDemo2 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 MultipleAxisDemo2(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        
116         // RANGE AXIS 2
117
NumberAxis yAxis2 = new NumberAxis("Range Axis 2");
118         plot.setSecondaryRangeAxis(0, yAxis2);
119         plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT);
120
121         XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170);
122         plot.setSecondaryDataset(0, dataset2);
123         plot.mapSecondaryDatasetToDomainAxis(0, new Integer JavaDoc(0));
124         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
125         
126         return chart;
127         
128     }
129     
130     // ****************************************************************************
131
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
132
// * Please note that commercial support and documentation is available from: *
133
// * *
134
// * http://www.object-refinery.com/jfreechart/support.html *
135
// * *
136
// * This is not only a great service for developers, but is a VERY IMPORTANT *
137
// * source of funding for the JFreeChart project. Please support us so that *
138
// * we can continue developing free software. *
139
// ****************************************************************************
140

141     /**
142      * Creates a sample dataset.
143      *
144      * @param name the dataset name.
145      * @param base the starting value.
146      * @param start the starting period.
147      * @param count the number of values to generate.
148      *
149      * @return The dataset.
150      */

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

174     public static void main(String JavaDoc[] args) {
175
176         MultipleAxisDemo2 demo = new MultipleAxisDemo2("Multiple Axis Demo 2");
177         demo.pack();
178         RefineryUtilities.centerFrameOnScreen(demo);
179         demo.setVisible(true);
180
181     }
182
183 }
184
Popular Tags