KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Font JavaDoc;
41
42 import org.jfree.chart.ChartPanel;
43 import org.jfree.chart.JFreeChart;
44 import org.jfree.chart.annotations.XYTextAnnotation;
45 import org.jfree.chart.axis.DateAxis;
46 import org.jfree.chart.axis.DateTickMarkPosition;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.axis.ValueAxis;
49 import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
50 import org.jfree.chart.plot.PlotOrientation;
51 import org.jfree.chart.plot.XYPlot;
52 import org.jfree.chart.renderer.StandardXYItemRenderer;
53 import org.jfree.chart.renderer.XYBarRenderer;
54 import org.jfree.chart.renderer.XYItemRenderer;
55 import org.jfree.data.IntervalXYDataset;
56 import org.jfree.data.XYDataset;
57 import org.jfree.data.time.Day;
58 import org.jfree.data.time.TimePeriodAnchor;
59 import org.jfree.data.time.TimeSeries;
60 import org.jfree.data.time.TimeSeriesCollection;
61 import org.jfree.date.SerialDate;
62 import org.jfree.ui.ApplicationFrame;
63 import org.jfree.ui.RefineryUtilities;
64
65 /**
66  * A demonstration application showing an overlaid XY plot with dual range axes.
67  *
68  * @author David Gilbert
69  */

70 public class OverlaidXYPlotDemo2 extends ApplicationFrame {
71
72     /**
73      * Constructs a new demonstration application.
74      *
75      * @param title the frame title.
76      */

77     public OverlaidXYPlotDemo2(String JavaDoc title) {
78
79         super(title);
80         JFreeChart chart = createOverlaidChart();
81         ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
82         panel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
83         setContentPane(panel);
84
85     }
86
87     // ****************************************************************************
88
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
89
// * Please note that commercial support and documentation is available from: *
90
// * *
91
// * http://www.object-refinery.com/jfreechart/support.html *
92
// * *
93
// * This is not only a great service for developers, but is a VERY IMPORTANT *
94
// * source of funding for the JFreeChart project. Please support us so that *
95
// * we can continue developing free software. *
96
// ****************************************************************************
97

98     /**
99      * Creates an overlaid chart.
100      *
101      * @return The chart.
102      */

103     private JFreeChart createOverlaidChart() {
104
105         DateAxis domainAxis = new DateAxis("Date");
106         domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
107         ValueAxis rangeAxis = new NumberAxis("Value");
108
109         // create plot...
110
IntervalXYDataset data1 = createDataset1();
111         XYItemRenderer renderer1 = new XYBarRenderer(0.20);
112         renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
113         XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
114         double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond();
115         XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0);
116         annotation.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
117         plot.addAnnotation(annotation);
118
119         ValueAxis rangeAxis2 = new NumberAxis("Value 2");
120         plot.setSecondaryRangeAxis(0, rangeAxis2);
121
122         // create subplot 2...
123
XYDataset data2A = createDataset2A();
124         XYItemRenderer renderer2A = new StandardXYItemRenderer();
125         renderer2A.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
126         plot.setSecondaryDataset(0, data2A);
127         plot.setSecondaryRenderer(0, renderer2A);
128         
129         XYDataset data2B = createDataset2B();
130         plot.setSecondaryDataset(1, data2B);
131         plot.setSecondaryRenderer(1, new StandardXYItemRenderer());
132         plot.mapSecondaryDatasetToRangeAxis(1, new Integer JavaDoc(0));
133
134         plot.setOrientation(PlotOrientation.VERTICAL);
135         
136         // return a new chart containing the overlaid plot...
137
return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
138
139     }
140
141     /**
142      * Creates a sample dataset.
143      *
144      * @return The dataset.
145      */

146     private IntervalXYDataset createDataset1() {
147
148         // create dataset 1...
149
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
150         series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3);
151         series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4);
152         series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3);
153         series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3);
154         series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4);
155         series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3);
156         series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5);
157         series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3);
158         series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4);
159         series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6);
160         series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3);
161         series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2);
162         series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2);
163         series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2);
164         series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2);
165
166         TimeSeriesCollection result = new TimeSeriesCollection(series1);
167         return result;
168
169     }
170
171     /**
172      * Creates a sample dataset.
173      *
174      * @return The dataset.
175      */

176     private XYDataset createDataset2A() {
177
178         // create dataset 2...
179
TimeSeries series2 = new TimeSeries("Series 2", Day.class);
180
181         series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2);
182         series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3);
183         series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5);
184         series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3);
185         series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0);
186         series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3);
187         series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2);
188         series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2);
189         series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9);
190         series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2);
191         series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3);
192         series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6);
193         series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7);
194         series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9);
195
196         TimeSeriesCollection result = new TimeSeriesCollection(series2);
197         result.setXPosition(TimePeriodAnchor.MIDDLE);
198         return result;
199
200     }
201
202     /**
203      * Creates a sample dataset.
204      *
205      * @return The dataset.
206      */

207     private XYDataset createDataset2B() {
208
209         // create dataset 2...
210
TimeSeries series2 = new TimeSeries("Series 2B", Day.class);
211
212         series2.add(new Day(3, SerialDate.MARCH, 2002), 43.9);
213         series2.add(new Day(4, SerialDate.MARCH, 2002), 72.6);
214         series2.add(new Day(5, SerialDate.MARCH, 2002), 89.4);
215         series2.add(new Day(6, SerialDate.MARCH, 2002), 23.8);
216         series2.add(new Day(7, SerialDate.MARCH, 2002), 45.0);
217         series2.add(new Day(8, SerialDate.MARCH, 2002), 65.8);
218         series2.add(new Day(9, SerialDate.MARCH, 2002), 92.1);
219         series2.add(new Day(10, SerialDate.MARCH, 2002), 84.7);
220         series2.add(new Day(11, SerialDate.MARCH, 2002), 77.2);
221         series2.add(new Day(12, SerialDate.MARCH, 2002), 65.1);
222         series2.add(new Day(13, SerialDate.MARCH, 2002), 78.5);
223         series2.add(new Day(14, SerialDate.MARCH, 2002), 75.3);
224         series2.add(new Day(15, SerialDate.MARCH, 2002), 69.9);
225         series2.add(new Day(20, SerialDate.MARCH, 2002), 56.6);
226
227         TimeSeriesCollection result = new TimeSeriesCollection(series2);
228         result.setXPosition(TimePeriodAnchor.MIDDLE);
229         return result;
230
231     }
232
233     /**
234      * Starting point for the demonstration application.
235      *
236      * @param args ignored.
237      */

238     public static void main(String JavaDoc[] args) {
239
240         OverlaidXYPlotDemo2 demo = new OverlaidXYPlotDemo2("Overlaid XYPlot Demo");
241         demo.pack();
242         RefineryUtilities.centerFrameOnScreen(demo);
243         demo.setVisible(true);
244
245     }
246
247 }
248
Popular Tags