KickJava   Java API By Example, From Geeks To Geeks.

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


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  * OverlaidXYPlotDemo.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: OverlaidXYPlotDemo.java,v 1.17 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 28-Mar-2002 : Version 1 (DG);
35  * 23-Apr-2002 : Modified to use new OverlaidXYPlot class (DG);
36  * 31-May-2002 : Changed plot background color to yellow, to check that it works (DG);
37  * 13-Jun-2002 : Renamed OverlaidPlotDemo-->OverlaidXYPlotDemo (DG);
38  * 25-Jun-2002 : Removed unnecessary imports (DG);
39  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  *
41  */

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

73 public class OverlaidXYPlotDemo extends ApplicationFrame {
74
75     /**
76      * Constructs a new demonstration application.
77      *
78      * @param title the frame title.
79      */

80     public OverlaidXYPlotDemo(String JavaDoc title) {
81
82         super(title);
83         JFreeChart chart = createOverlaidChart();
84         ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
85         panel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
86         setContentPane(panel);
87
88     }
89
90     /**
91      * Creates an overlaid chart.
92      *
93      * @return The chart.
94      */

95     private JFreeChart createOverlaidChart() {
96
97         // create plot ...
98
IntervalXYDataset data1 = createDataset1();
99         XYItemRenderer renderer1 = new XYBarRenderer(0.20);
100         renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
101         DateAxis domainAxis = new DateAxis("Date");
102         domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
103         ValueAxis rangeAxis = new NumberAxis("Value");
104         XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
105         double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond();
106         XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0);
107         annotation.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
108         plot.addAnnotation(annotation);
109
110         // add a second dataset and renderer...
111
XYDataset data2 = createDataset2();
112         XYItemRenderer renderer2 = new StandardXYItemRenderer();
113         renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
114         plot.setSecondaryDataset(0, data2);
115         plot.setSecondaryRenderer(0, renderer2);
116
117         // return a new chart containing the overlaid plot...
118
return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
119
120     }
121
122     /**
123      * Creates a sample dataset.
124      *
125      * @return The dataset.
126      */

127     private IntervalXYDataset createDataset1() {
128
129         // create dataset 1...
130
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
131         series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3);
132         series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4);
133         series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3);
134         series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3);
135         series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4);
136         series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3);
137         series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5);
138         series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3);
139         series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4);
140         series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6);
141         series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3);
142         series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2);
143         series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2);
144         series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2);
145         series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2);
146
147         return new TimeSeriesCollection(series1);
148
149     }
150
151     // ****************************************************************************
152
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
153
// * Please note that commercial support and documentation is available from: *
154
// * *
155
// * http://www.object-refinery.com/jfreechart/support.html *
156
// * *
157
// * This is not only a great service for developers, but is a VERY IMPORTANT *
158
// * source of funding for the JFreeChart project. Please support us so that *
159
// * we can continue developing free software. *
160
// ****************************************************************************
161

162     /**
163      * Creates a sample dataset.
164      *
165      * @return The dataset.
166      */

167     private XYDataset createDataset2() {
168
169         // create dataset 2...
170
TimeSeries series2 = new TimeSeries("Series 2", Day.class);
171
172         series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2);
173         series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3);
174         series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5);
175         series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3);
176         series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0);
177         series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3);
178         series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2);
179         series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2);
180         series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9);
181         series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2);
182         series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3);
183         series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6);
184         series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7);
185         series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9);
186
187         TimeSeriesCollection tsc = new TimeSeriesCollection(series2);
188         return tsc;
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         OverlaidXYPlotDemo demo = new OverlaidXYPlotDemo("Overlaid XYPlot Demo");
200         demo.pack();
201         RefineryUtilities.centerFrameOnScreen(demo);
202         demo.setVisible(true);
203
204     }
205
206 }
207
Popular Tags