KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedXYPlotDemo1.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 $
31  *
32  * Changes
33  * -------
34  * 13-Jan-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.AxisLocation;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.plot.CombinedDomainXYPlot;
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.chart.renderer.XYItemRenderer;
52 import org.jfree.data.XYDataset;
53 import org.jfree.data.XYSeries;
54 import org.jfree.data.XYSeriesCollection;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * A demonstration application showing how to create a vertical combined chart.
60  *
61  * @author David Gilbert
62  */

63 public class CombinedXYPlotDemo1 extends ApplicationFrame {
64
65     /**
66      * Constructs a new demonstration application.
67      *
68      * @param title the frame title.
69      */

70     public CombinedXYPlotDemo1(String JavaDoc title) {
71
72         super(title);
73         JFreeChart chart = createCombinedChart();
74         ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
75         panel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
76         setContentPane(panel);
77
78     }
79
80     /**
81      * Creates a combined chart.
82      *
83      * @return the combined chart.
84      */

85     private JFreeChart createCombinedChart() {
86
87         // create subplot 1...
88
XYDataset data1 = createDataset1();
89         XYItemRenderer renderer1 = new StandardXYItemRenderer();
90         NumberAxis rangeAxis1 = new NumberAxis("Range 1");
91         XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
92         subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
93         
94         XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
95         annotation.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
96         annotation.setRotationAngle(Math.PI / 4.0);
97         subplot1.addAnnotation(annotation);
98         
99         // create subplot 2...
100
XYDataset data2 = createDataset2();
101         XYItemRenderer renderer2 = new StandardXYItemRenderer();
102         NumberAxis rangeAxis2 = new NumberAxis("Range 2");
103         rangeAxis2.setAutoRangeIncludesZero(false);
104         XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
105         subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
106
107         // parent plot...
108
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
109         plot.setGap(10.0);
110         
111         // add the subplots...
112
plot.add(subplot1, 1);
113         plot.add(subplot2, 1);
114         plot.setOrientation(PlotOrientation.VERTICAL);
115
116         // return a new chart containing the overlaid plot...
117
return new JFreeChart("CombinedDomainXYPlot Demo",
118                               JFreeChart.DEFAULT_TITLE_FONT, plot, true);
119
120     }
121
122     /**
123      * Creates a sample dataset.
124      *
125      * @return Series 1.
126      */

127     private XYDataset createDataset1() {
128
129         // create dataset 1...
130
XYSeries series1 = new XYSeries("Series 1");
131         series1.add(10.0, 12353.3);
132         series1.add(20.0, 13734.4);
133         series1.add(30.0, 14525.3);
134         series1.add(40.0, 13984.3);
135         series1.add(50.0, 12999.4);
136         series1.add(60.0, 14274.3);
137         series1.add(70.0, 15943.5);
138         series1.add(80.0, 14845.3);
139         series1.add(90.0, 14645.4);
140         series1.add(100.0, 16234.6);
141         series1.add(110.0, 17232.3);
142         series1.add(120.0, 14232.2);
143         series1.add(130.0, 13102.2);
144         series1.add(140.0, 14230.2);
145         series1.add(150.0, 11235.2);
146
147         XYSeries series2 = new XYSeries("Series 2");
148         series2.add(10.0, 15000.3);
149         series2.add(20.0, 11000.4);
150         series2.add(30.0, 17000.3);
151         series2.add(40.0, 15000.3);
152         series2.add(50.0, 14000.4);
153         series2.add(60.0, 12000.3);
154         series2.add(70.0, 11000.5);
155         series2.add(80.0, 12000.3);
156         series2.add(90.0, 13000.4);
157         series2.add(100.0, 12000.6);
158         series2.add(110.0, 13000.3);
159         series2.add(120.0, 17000.2);
160         series2.add(130.0, 18000.2);
161         series2.add(140.0, 16000.2);
162         series2.add(150.0, 17000.2);
163
164         XYSeriesCollection collection = new XYSeriesCollection();
165         collection.addSeries(series1);
166         collection.addSeries(series2);
167         return collection;
168
169     }
170
171     /**
172      * Creates a sample dataset.
173      *
174      * @return Series 2.
175      */

176     private XYDataset createDataset2() {
177
178         // create dataset 2...
179
XYSeries series2 = new XYSeries("Series 3");
180
181         series2.add(10.0, 16853.2);
182         series2.add(20.0, 19642.3);
183         series2.add(30.0, 18253.5);
184         series2.add(40.0, 15352.3);
185         series2.add(50.0, 13532.0);
186         series2.add(100.0, 12635.3);
187         series2.add(110.0, 13998.2);
188         series2.add(120.0, 11943.2);
189         series2.add(130.0, 16943.9);
190         series2.add(140.0, 17843.2);
191         series2.add(150.0, 16495.3);
192         series2.add(160.0, 17943.6);
193         series2.add(170.0, 18500.7);
194         series2.add(180.0, 19595.9);
195
196         return new XYSeriesCollection(series2);
197
198     }
199
200     // ****************************************************************************
201
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
202
// * Please note that commercial support and documentation is available from: *
203
// * *
204
// * http://www.object-refinery.com/jfreechart/support.html *
205
// * *
206
// * This is not only a great service for developers, but is a VERY IMPORTANT *
207
// * source of funding for the JFreeChart project. Please support us so that *
208
// * we can continue developing free software. *
209
// ****************************************************************************
210

211     /**
212      * Starting point for the demonstration application.
213      *
214      * @param args ignored.
215      */

216     public static void main(String JavaDoc[] args) {
217
218         CombinedXYPlotDemo1 demo = new CombinedXYPlotDemo1("CombinedDomainXYPlot Demo");
219         demo.pack();
220         RefineryUtilities.centerFrameOnScreen(demo);
221         demo.setVisible(true);
222
223     }
224
225 }
226
Popular Tags