KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > DefaultHighLowDataset


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  * DefaultHighLowDataset.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: DefaultHighLowDataset.java,v 1.3 2003/10/30 09:53:06 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 21-Mar-2002 : Version 1 (DG);
35  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  *
37  */

38
39 package org.jfree.data;
40
41 import java.util.Date JavaDoc;
42
43 /**
44  * A simple implementation of the HighLowDataset.
45  *
46  * @author David Gilbert
47  */

48 public class DefaultHighLowDataset extends AbstractSeriesDataset implements HighLowDataset {
49
50     /** The series name. */
51     private String JavaDoc seriesName;
52
53     /** Storage for the dates. */
54     private Date JavaDoc[] date;
55
56     /** Storage for the high values. */
57     private Number JavaDoc[] high;
58
59     /** Storage for the low values. */
60     private Number JavaDoc[] low;
61
62     /** Storage for the open values. */
63     private Number JavaDoc[] open;
64
65     /** Storage for the close values. */
66     private Number JavaDoc[] close;
67
68     /** Storage for the volume values. */
69     private Number JavaDoc[] volume;
70
71     /**
72      * Constructs a new high/low/open/close dataset.
73      * <p>
74      * The current implementation allows only one series in the dataset.
75      * This may be extended in a future version.
76      *
77      * @param seriesName the name of the series.
78      * @param date the dates.
79      * @param high the high values.
80      * @param low the low values.
81      * @param open the open values.
82      * @param close the close values.
83      * @param volume the volume values.
84      */

85     public DefaultHighLowDataset(String JavaDoc seriesName,
86                                  Date JavaDoc[] date,
87                                  double[] high, double[] low,
88                                  double[] open, double[] close,
89                                  double[] volume) {
90
91         this.seriesName = seriesName;
92         this.date = date;
93         this.high = createNumberArray(high);
94         this.low = createNumberArray(low);
95         this.open = createNumberArray(open);
96         this.close = createNumberArray(close);
97         this.volume = createNumberArray(volume);
98
99     }
100
101     /**
102      * Returns the name of the series stored in this dataset.
103      *
104      * @param i the index of the series. Currently ignored.
105      * @return the name of this series.
106      */

107     public String JavaDoc getSeriesName(int i) {
108         return this.seriesName;
109     }
110
111     /**
112      * Returns the x-value for one item in a series.
113      * <p>
114      * The value returned is a Long object generated from the underlying Date object.
115      *
116      * @param series the series (zero-based index).
117      * @param item the item (zero-based index).
118      *
119      * @return the x-value.
120      */

121     public Number JavaDoc getXValue(int series, int item) {
122         return new Long JavaDoc(date[item].getTime());
123     }
124
125     /**
126      * Returns the x-value for one item in a series, as a Date.
127      * <p>
128      * This method is provided for convenience only.
129      *
130      * @param series the series (zero-based index).
131      * @param item the item (zero-based index).
132      *
133      * @return the x-value as a Date.
134      */

135     public Date JavaDoc getXDate(int series, int item) {
136         return date[item];
137     }
138
139     /**
140      * Returns the y-value for one item in a series.
141      * <p>
142      * This method (from the XYDataset interface) is mapped to the getCloseValue(...) method.
143      *
144      * @param series the series (zero-based index).
145      * @param item the item (zero-based index).
146      *
147      * @return the y-value.
148      */

149     public Number JavaDoc getYValue(int series, int item) {
150         return getCloseValue(series, item);
151     }
152
153     /**
154      * Returns the high-value for one item in a series.
155      *
156      * @param series the series (zero-based index).
157      * @param item the item (zero-based index).
158      *
159      * @return the high-value.
160      */

161     public Number JavaDoc getHighValue(int series, int item) {
162         return high[item];
163     }
164
165     /**
166      * Returns the low-value for one item in a series.
167      *
168      * @param series the series (zero-based index).
169      * @param item the item (zero-based index).
170      *
171      * @return the low-value.
172      */

173     public Number JavaDoc getLowValue(int series, int item) {
174         return low[item];
175     }
176
177     /**
178      * Returns the open-value for one item in a series.
179      *
180      * @param series the series (zero-based index).
181      * @param item the item (zero-based index).
182      *
183      * @return the open-value.
184      */

185     public Number JavaDoc getOpenValue(int series, int item) {
186         return open[item];
187     }
188
189     /**
190      * Returns the close-value for one item in a series.
191      *
192      * @param series the series (zero-based index).
193      * @param item the item (zero-based index).
194      *
195      * @return the close-value.
196      */

197     public Number JavaDoc getCloseValue(int series, int item) {
198         return close[item];
199     }
200
201     /**
202      * Returns the volume-value for one item in a series.
203      *
204      * @param series the series (zero-based index).
205      * @param item the item (zero-based index).
206      *
207      * @return the volume-value.
208      */

209     public Number JavaDoc getVolumeValue(int series, int item) {
210         return volume[item];
211     }
212
213     /**
214      * Returns the number of series in the dataset.
215      * <p>
216      * This implementation only allows one series.
217      *
218      * @return the number of series.
219      */

220     public int getSeriesCount() {
221         return 1;
222     }
223
224     /**
225      * Returns the number of items in the specified series.
226      *
227      * @param series the index (zero-based) of the series.
228      *
229      * @return the number of items in the specified series.
230      */

231     public int getItemCount(int series) {
232         return date.length;
233     }
234
235     /**
236      * Constructs an array of Number objects from an array of doubles.
237      *
238      * @param data the double values to convert.
239      *
240      * @return data as array of Number.
241      */

242     public static Number JavaDoc[] createNumberArray(double[] data) {
243
244         Number JavaDoc[] result = new Number JavaDoc[data.length];
245
246         for (int i = 0; i < data.length; i++) {
247             result[i] = new Double JavaDoc(data[i]);
248         }
249
250         return result;
251
252     }
253
254 }
255
Popular Tags