KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedXYPlotDemo4.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  * 29-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.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 CombinedXYPlotDemo4 extends ApplicationFrame {
64
65     /**
66      * Constructs a new demonstration application.
67      *
68      * @param title the frame title.
69      */

70     public CombinedXYPlotDemo4(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         // add secondary axis
95
subplot1.setSecondaryDataset(0, createDataset2());
96         NumberAxis axis2 = new NumberAxis("Range Axis 2");
97         axis2.setAutoRangeIncludesZero(false);
98         subplot1.setSecondaryRangeAxis(0, axis2);
99         subplot1.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT);
100         subplot1.setSecondaryRenderer(0, new StandardXYItemRenderer());
101         subplot1.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
102
103         XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
104         annotation.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
105         annotation.setRotationAngle(Math.PI / 4.0);
106         subplot1.addAnnotation(annotation);
107         
108         // create subplot 2...
109
XYDataset data2 = createDataset2();
110         XYItemRenderer renderer2 = new StandardXYItemRenderer();
111         NumberAxis rangeAxis2 = new NumberAxis("Range 2");
112         rangeAxis2.setAutoRangeIncludesZero(false);
113         XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
114         subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
115
116         // parent plot...
117
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
118         plot.setGap(10.0);
119         
120         // add the subplots...
121
plot.add(subplot1, 1);
122         plot.add(subplot2, 1);
123         plot.setOrientation(PlotOrientation.VERTICAL);
124
125         // return a new chart containing the overlaid plot...
126
return new JFreeChart("CombinedDomainXYPlot Demo",
127                               JFreeChart.DEFAULT_TITLE_FONT, plot, true);
128
129     }
130
131     /**
132      * Creates a sample dataset.
133      *
134      * @return Series 1.
135      */

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

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

220     /**
221      * Starting point for the demonstration application.
222      *
223      * @param args ignored.
224      */

225     public static void main(String JavaDoc[] args) {
226
227         CombinedXYPlotDemo4 demo = new CombinedXYPlotDemo4("CombinedDomainXYPlot Demo");
228         demo.pack();
229         RefineryUtilities.centerFrameOnScreen(demo);
230         demo.setVisible(true);
231
232     }
233
234 }
235
Popular Tags