KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DualAxisDemo2.java
24  * ------------------
25  * (C) Copyright 2002, 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: DualAxisDemo2.java,v 1.8 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 19-Nov-2002 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.StandardLegend;
47 import org.jfree.chart.axis.DateAxis;
48 import org.jfree.chart.axis.NumberAxis;
49 import org.jfree.chart.plot.XYPlot;
50 import org.jfree.chart.renderer.StandardXYItemRenderer;
51 import org.jfree.chart.renderer.XYItemRenderer;
52 import org.jfree.data.XYDataset;
53 import org.jfree.data.time.Month;
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 a time series chart. For the most part, default settings are used, except that
61  * the renderer is modified to show filled shapes (as well as lines) at each data point.
62  *
63  * @author David Gilbert
64  */

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

72     public DualAxisDemo2(String JavaDoc title) {
73
74         super(title);
75
76         // create a title...
77
String JavaDoc chartTitle = "Dual Axis Demo 2";
78         XYDataset dataset = createDataset1();
79
80         JFreeChart chart = ChartFactory.createTimeSeriesChart(
81             chartTitle,
82             "Date",
83             "Price Per Unit",
84             dataset,
85             true,
86             true,
87             false
88         );
89
90         StandardLegend legend = (StandardLegend) chart.getLegend();
91         legend.setDisplaySeriesShapes(true);
92         
93         XYPlot plot = chart.getXYPlot();
94         NumberAxis axis2 = new NumberAxis("Secondary");
95         axis2.setAutoRangeIncludesZero(false);
96         plot.setSecondaryRangeAxis(0, axis2);
97         plot.setSecondaryDataset(0, createDataset2());
98         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
99         XYItemRenderer renderer = plot.getRenderer();
100         if (renderer instanceof StandardXYItemRenderer) {
101             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
102             rr.setPlotShapes(true);
103             rr.setShapesFilled(true);
104         }
105         
106         StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
107         renderer2.setSeriesPaint(0, Color.black);
108         renderer2.setPlotShapes(true);
109         plot.setSecondaryRenderer(0, renderer2);
110         
111         DateAxis axis = (DateAxis) plot.getDomainAxis();
112         axis.setDateFormatOverride(new SimpleDateFormat JavaDoc("MMM-yyyy"));
113         
114         ChartPanel chartPanel = new ChartPanel(chart);
115         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
116         setContentPane(chartPanel);
117
118     }
119
120     /**
121      * Creates a sample dataset.
122      *
123      * @return The dataset.
124      */

125     private XYDataset createDataset1() {
126
127         TimeSeries s1 = new TimeSeries("Random Data 1", Month.class);
128         s1.add(new Month(2, 2001), 181.8);
129         s1.add(new Month(3, 2001), 167.3);
130         s1.add(new Month(4, 2001), 153.8);
131         s1.add(new Month(5, 2001), 167.6);
132         s1.add(new Month(6, 2001), 158.8);
133         s1.add(new Month(7, 2001), 148.3);
134         s1.add(new Month(8, 2001), 153.9);
135         s1.add(new Month(9, 2001), 142.7);
136         s1.add(new Month(10, 2001), 123.2);
137         s1.add(new Month(11, 2001), 131.8);
138         s1.add(new Month(12, 2001), 139.6);
139         s1.add(new Month(1, 2002), 142.9);
140         s1.add(new Month(2, 2002), 138.7);
141         s1.add(new Month(3, 2002), 137.3);
142         s1.add(new Month(4, 2002), 143.9);
143         s1.add(new Month(5, 2002), 139.8);
144         s1.add(new Month(6, 2002), 137.0);
145         s1.add(new Month(7, 2002), 132.8);
146
147         TimeSeriesCollection dataset = new TimeSeriesCollection();
148         dataset.addSeries(s1);
149
150         return dataset;
151
152     }
153
154     /**
155      * Creates a sample dataset.
156      *
157      * @return The dataset.
158      */

159     private XYDataset createDataset2() {
160
161
162         TimeSeries s2 = new TimeSeries("Random Data 2", Month.class);
163         s2.add(new Month(2, 2001), 429.6);
164         s2.add(new Month(3, 2001), 323.2);
165         s2.add(new Month(4, 2001), 417.2);
166         s2.add(new Month(5, 2001), 624.1);
167         s2.add(new Month(6, 2001), 422.6);
168         s2.add(new Month(7, 2001), 619.2);
169         s2.add(new Month(8, 2001), 416.5);
170         s2.add(new Month(9, 2001), 512.7);
171         s2.add(new Month(10, 2001), 501.5);
172         s2.add(new Month(11, 2001), 306.1);
173         s2.add(new Month(12, 2001), 410.3);
174         s2.add(new Month(1, 2002), 511.7);
175         s2.add(new Month(2, 2002), 611.0);
176         s2.add(new Month(3, 2002), 709.6);
177         s2.add(new Month(4, 2002), 613.2);
178         s2.add(new Month(5, 2002), 711.6);
179         s2.add(new Month(6, 2002), 708.8);
180         s2.add(new Month(7, 2002), 501.6);
181
182         TimeSeriesCollection dataset = new TimeSeriesCollection();
183         dataset.addSeries(s2);
184
185         return dataset;
186
187     }
188
189     // ****************************************************************************
190
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
191
// * Please note that commercial support and documentation is available from: *
192
// * *
193
// * http://www.object-refinery.com/jfreechart/support.html *
194
// * *
195
// * This is not only a great service for developers, but is a VERY IMPORTANT *
196
// * source of funding for the JFreeChart project. Please support us so that *
197
// * we can continue developing free software. *
198
// ****************************************************************************
199

200     /**
201      * Starting point for the demonstration application.
202      *
203      * @param args ignored.
204      */

205     public static void main(String JavaDoc[] args) {
206
207         DualAxisDemo2 demo = new DualAxisDemo2("Dual Axis Demo 2");
208         demo.pack();
209         RefineryUtilities.centerFrameOnScreen(demo);
210         demo.setVisible(true);
211
212     }
213
214 }
215
Popular Tags