KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedXYPlotDemo2.java
24  * ------------------------
25  * (C) Copyright 2002, 2003 by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited).
28  * Contributor(s): -;
29  *
30  * $Id $
31  *
32  * Changes
33  * -------
34  * 23-Apr-2002 : Version 1 (DG);
35  * 23-May-2002 : Renamed MultiPlotDemo --> CombinedXYPlotDemo (DG);
36  * 25-Jun-2002 : Removed unnecessary imports (DG);
37  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  *
39  */

40
41 package org.jfree.chart.demo;
42
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.axis.DateAxis;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.labels.TimeSeriesToolTipGenerator;
48 import org.jfree.chart.plot.CombinedRangeXYPlot;
49 import org.jfree.chart.plot.XYPlot;
50 import org.jfree.chart.renderer.StandardXYItemRenderer;
51 import org.jfree.chart.renderer.XYBarRenderer;
52 import org.jfree.chart.renderer.XYItemRenderer;
53 import org.jfree.data.IntervalXYDataset;
54 import org.jfree.data.XYDataset;
55 import org.jfree.data.time.Day;
56 import org.jfree.data.time.TimeSeries;
57 import org.jfree.data.time.TimeSeriesCollection;
58 import org.jfree.date.SerialDate;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * A demonstration application showing how to create a horizontal combined chart. A
64  * bar chart is displayed on the left, and a line chart on the right.
65  *
66  * @author David Gilbert
67  */

68 public class CombinedXYPlotDemo2 extends ApplicationFrame {
69
70     /**
71      * Constructs a new demonstration application.
72      *
73      * @param title the frame title.
74      */

75     public CombinedXYPlotDemo2(String JavaDoc title) {
76
77         super(title);
78         JFreeChart chart = createCombinedChart();
79         ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
80         panel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
81         setContentPane(panel);
82
83     }
84
85     /**
86      * Creates a combined XYPlot chart.
87      *
88      * @return the combined chart.
89      */

90     private JFreeChart createCombinedChart() {
91
92         // create subplot 1...
93
IntervalXYDataset data1 = createDataset1();
94         XYItemRenderer renderer1 = new XYBarRenderer(0.20);
95         renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0,000.0"));
96         XYPlot subplot1 = new XYPlot(data1, new DateAxis("Date"), null, renderer1);
97
98         // create subplot 2...
99
XYDataset data2 = createDataset2();
100         XYItemRenderer renderer2 = new StandardXYItemRenderer();
101         renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0,000.0"));
102         XYPlot subplot2 = new XYPlot(data2, new DateAxis("Date"), null, renderer2);
103
104         // create a parent plot...
105
CombinedRangeXYPlot plot = new CombinedRangeXYPlot(new NumberAxis("Value"));
106
107         // add the subplots...
108
plot.add(subplot1, 1);
109         plot.add(subplot2, 1);
110
111         // return a new chart containing the overlaid plot...
112
return new JFreeChart("Combined (Range) XY Plot",
113                               JFreeChart.DEFAULT_TITLE_FONT, plot, true);
114
115     }
116
117     // ****************************************************************************
118
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
119
// * Please note that commercial support and documentation is available from: *
120
// * *
121
// * http://www.object-refinery.com/jfreechart/support.html *
122
// * *
123
// * This is not only a great service for developers, but is a VERY IMPORTANT *
124
// * source of funding for the JFreeChart project. Please support us so that *
125
// * we can continue developing free software. *
126
// ****************************************************************************
127

128     /**
129      * Creates a sample dataset.
130      *
131      * @return Series 1.
132      */

133     private IntervalXYDataset createDataset1() {
134
135         // create dataset 1...
136
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
137         series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3);
138         series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4);
139         series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3);
140         series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3);
141         series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4);
142         series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3);
143         series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5);
144         series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3);
145         series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4);
146         series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6);
147         series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3);
148         series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2);
149         series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2);
150         series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2);
151         series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2);
152
153         TimeSeriesCollection collection = new TimeSeriesCollection(series1);
154         collection.setDomainIsPointsInTime(false); // this tells the time series collection that
155
// we intend the data to represent time periods
156
// NOT points in time. This is required when
157
// determining the min/max values in the
158
// dataset's domain.
159
return collection;
160
161     }
162
163     /**
164      * Creates a sample dataset.
165      *
166      * @return Series 2.
167      */

168     private XYDataset createDataset2() {
169
170         // create dataset 2...
171
TimeSeries series2 = new TimeSeries("Series 2", Day.class);
172
173         series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2);
174         series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3);
175         series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5);
176         series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3);
177         series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0);
178         series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3);
179         series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2);
180         series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2);
181         series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9);
182         series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2);
183         series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3);
184         series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6);
185         series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7);
186         series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9);
187
188         return new TimeSeriesCollection(series2);
189
190     }
191
192     /**
193      * Starting point for the demonstration application.
194      *
195      * @param args ignored.
196      */

197     public static void main(String JavaDoc[] args) {
198
199         CombinedXYPlotDemo2 demo = new CombinedXYPlotDemo2("Combined XY Plot Demo");
200         demo.pack();
201         RefineryUtilities.centerFrameOnScreen(demo);
202         demo.setVisible(true);
203
204     }
205
206 }
207
Popular Tags