KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.chart.demo;
39
40 import java.text.SimpleDateFormat JavaDoc;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.StandardLegend;
46 import org.jfree.chart.axis.DateAxis;
47 import org.jfree.chart.plot.XYPlot;
48 import org.jfree.chart.renderer.StandardXYItemRenderer;
49 import org.jfree.chart.renderer.XYItemRenderer;
50 import org.jfree.data.MovingAverage;
51 import org.jfree.data.XYDataset;
52 import org.jfree.data.time.Month;
53 import org.jfree.data.time.TimeSeries;
54 import org.jfree.data.time.TimeSeriesCollection;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * An example showing the calculation of a moving average for a time series.
60  *
61  * @author David Gilbert
62  */

63 public class MovingAverageDemo extends ApplicationFrame {
64
65     /**
66      * A moving average demo.
67      *
68      * @param title the frame title.
69      */

70     public MovingAverageDemo(String JavaDoc title) {
71
72         super(title);
73
74         // create a title...
75
String JavaDoc chartTitle = "Legal & General Unit Trust Prices";
76         XYDataset dataset = createDataset();
77
78         JFreeChart chart = ChartFactory.createTimeSeriesChart(
79             chartTitle,
80             "Date",
81             "Price Per Unit",
82             dataset,
83             true,
84             true,
85             false
86         );
87
88         StandardLegend legend = (StandardLegend) chart.getLegend();
89         legend.setDisplaySeriesShapes(true);
90         XYPlot plot = chart.getXYPlot();
91         XYItemRenderer renderer = plot.getRenderer();
92         if (renderer instanceof StandardXYItemRenderer) {
93             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
94             rr.setPlotShapes(true);
95             rr.setShapesFilled(true);
96         }
97         DateAxis axis = (DateAxis) plot.getDomainAxis();
98         axis.setDateFormatOverride(new SimpleDateFormat JavaDoc("MMM-yyyy"));
99         ChartPanel chartPanel = new ChartPanel(chart);
100         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
101         setContentPane(chartPanel);
102
103     }
104
105     /**
106      * Creates a dataset, one series containing unit trust prices, the other a moving average.
107      *
108      * @return the dataset.
109      */

110     public XYDataset createDataset() {
111
112         TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
113         s1.add(new Month(2, 2001), 181.8);
114         s1.add(new Month(3, 2001), 167.3);
115         s1.add(new Month(4, 2001), 153.8);
116         s1.add(new Month(5, 2001), 167.6);
117         s1.add(new Month(6, 2001), 158.8);
118         s1.add(new Month(7, 2001), 148.3);
119         s1.add(new Month(8, 2001), 153.9);
120         s1.add(new Month(9, 2001), 142.7);
121         s1.add(new Month(10, 2001), 123.2);
122         s1.add(new Month(11, 2001), 131.8);
123         s1.add(new Month(12, 2001), 139.6);
124         s1.add(new Month(1, 2002), 142.9);
125         s1.add(new Month(2, 2002), 138.7);
126         s1.add(new Month(3, 2002), 137.3);
127         s1.add(new Month(4, 2002), 143.9);
128         s1.add(new Month(5, 2002), 139.8);
129         s1.add(new Month(6, 2002), 137.0);
130         s1.add(new Month(7, 2002), 132.8);
131
132         // ****************************************************************************
133
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
134
// * Please note that commercial support and documentation is available from: *
135
// * *
136
// * http://www.object-refinery.com/jfreechart/support.html *
137
// * *
138
// * This is not only a great service for developers, but is a VERY IMPORTANT *
139
// * source of funding for the JFreeChart project. Please support us so that *
140
// * we can continue developing free software. *
141
// ****************************************************************************
142

143         TimeSeries s2 = MovingAverage.createMovingAverage(s1, "Six Month Moving Average", 6, 0);
144
145         TimeSeriesCollection dataset = new TimeSeriesCollection();
146         dataset.addSeries(s1);
147         dataset.addSeries(s2);
148         
149         return dataset;
150
151     }
152
153     /**
154      * Starting point for the demonstration application.
155      *
156      * @param args ignored.
157      */

158     public static void main(String JavaDoc[] args) {
159
160         MovingAverageDemo demo = new MovingAverageDemo("Moving Average Demo 1");
161         demo.pack();
162         RefineryUtilities.centerFrameOnScreen(demo);
163         demo.setVisible(true);
164
165     }
166
167 }
168
Popular Tags