KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > chartData > StockChartDataSet


1 /***********************************************************************************************
2  * File Info: $Id: StockChartDataSet.java,v 1.1 2003/05/17 16:58:11 nathaniel_auvil Exp $
3  * Copyright (C) 2002
4  * Author: Nathaniel G. Auvil
5  * Contributor(s):
6  *
7  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
8  *
9  * Redistribution and use of this software and associated documentation ("Software"), with or
10  * without modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain copyright statements and notices.
13  * Redistributions must also contain a copy of this document.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
16  * conditions and the following disclaimer in the documentation and/or other materials
17  * provided with the distribution.
18  *
19  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
20  * products derived from this Software without prior written permission of Nathaniel G.
21  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
22  *
23  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
24  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
25  * registered trademark of Nathaniel G. Auvil.
26  *
27  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
28  *
29  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
30  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  ************************************************************************************************/

39
40 package org.krysalis.jcharts.chartData;
41
42
43 import org.krysalis.jcharts.chartData.interfaces.IStockChartDataSet;
44 import org.krysalis.jcharts.properties.*;
45 import org.krysalis.jcharts.test.HTMLGenerator;
46 import org.krysalis.jcharts.test.HTMLTestable;
47 import org.krysalis.jcharts.types.ChartType;
48 import org.krysalis.jcharts.types.StockChartDataType;
49
50 import java.awt.*;
51
52
53 public class StockChartDataSet implements IStockChartDataSet, HTMLTestable
54 {
55     private ChartType chartType=ChartType.STOCK;
56
57     private double[] high;
58     private double[] low;
59     private double[] open;
60     private double[] close;
61
62     //---keep this value for reference sake. Start with high and low
63
private int numberOfDataSets=2;
64
65     private String JavaDoc[] legendLabels;
66     private Paint[] paints;
67     private StockChartProperties stockChartProperties;
68
69
70     /******************************************************************************************
71      * Constructor
72      *
73      * @param high
74      * @param highLegendLabel
75      * @param low
76      * @param lowLegendLabel
77      * @param stockChartProperties properties Object specific to the type of chart you are rendering.
78      * @throws ChartDataException performs a limited validation of the data
79      *******************************************************************************************/

80     public StockChartDataSet( double[] high,
81                                       String JavaDoc highLegendLabel,
82                                       double[] low,
83                                       String JavaDoc lowLegendLabel,
84                                       Paint highLowPaint,
85                                       StockChartProperties stockChartProperties ) throws ChartDataException
86     {
87         this.high=high;
88         this.low=low;
89
90         this.legendLabels=new String JavaDoc[ 5 ];
91         this.legendLabels[ StockChartDataType.HIGH.getInt() ]=highLegendLabel;
92         this.legendLabels[ StockChartDataType.LOW.getInt() ]=lowLegendLabel;
93
94         this.paints=new Paint[ 5 ];
95         this.paints[ StockChartDataType.HIGH.getInt() ]=highLowPaint;
96         this.paints[ StockChartDataType.LOW.getInt() ]=highLowPaint;
97
98         this.stockChartProperties=stockChartProperties;
99     }
100
101
102     /************************************************************************************************
103      * Performs a limited validation of data passed to Constructor.
104      *
105      * @throws ChartDataException
106      * @throws PropertyException
107      *************************************************************************************************/

108     public void validate() throws ChartDataException, PropertyException
109     {
110         if( high == null || low == null )
111         {
112             throw new ChartDataException( "The Hi/Low values can not be NULL." );
113         }
114
115         if( high.length != low.length )
116         {
117             throw new ChartDataException( "The Hi/Low Arrays must have equal length." );
118         }
119
120         if( this.paints[ StockChartDataType.HIGH.getInt() ] == null )
121         {
122             throw new ChartDataException( "The Hi/Low Paint implementation can not be NULL." );
123         }
124
125         this.stockChartProperties.validate( this );
126     }
127
128
129     /******************************************************************************************
130      * Returns the legend label for the passed index. This index corresponds to the DataSet
131      * for which label you want.
132      *
133      * @param index
134      * @return String
135      *******************************************************************************************/

136     public String JavaDoc getLegendLabel( int index )
137     {
138         return this.legendLabels[ index ];
139     }
140
141
142     /*********************************************************************************************
143      * Returns the number of Legend Labels to display.
144      *
145      * @return int
146      **********************************************************************************************/

147     public int getNumberOfLegendLabels()
148     {
149         return this.legendLabels.length;
150     }
151
152
153     /******************************************************************************************
154      * Returns the number of elements in the data set. All data sets must be of the same length
155      * so just look at the first one.
156      *
157      * @return int
158      *******************************************************************************************/

159     public int getNumberOfDataItems()
160     {
161         //---always have a high and a low
162
return this.high.length;
163     }
164
165
166     /***************************************************************************************************
167      * Sets the 'Close' values
168      *
169      * @param data
170      * @param legendLabel
171      * @param paint
172      **************************************************************************************************/

173     public void setCloseValues( double[] data, String JavaDoc legendLabel, Paint paint )
174     {
175         this.numberOfDataSets++;
176         this.close=data;
177         this.legendLabels[ StockChartDataType.CLOSE.getInt() ]=legendLabel;
178         this.paints[ StockChartDataType.CLOSE.getInt() ]=paint;
179     }
180
181
182     /***************************************************************************************************
183      * Sets the 'Open' values
184      *
185      * @param data
186      * @param legendLabel
187      * @param paint
188      **************************************************************************************************/

189     public void setOpenValues( double[] data, String JavaDoc legendLabel, Paint paint )
190     {
191         this.numberOfDataSets++;
192         this.open=data;
193         this.legendLabels[ StockChartDataType.OPEN.getInt() ]=legendLabel;
194         this.paints[ StockChartDataType.OPEN.getInt() ]=paint;
195     }
196
197
198     /*********************************************************************************************
199      * Sets the 'Volume' values
200      *
201      * @param data[]
202      * @param legendLabel
203      * @param paint
204      *********************************************************************************************
205      public void setVolumeValues( double[] data, String legendLabel, Paint paint )
206      {
207      this.numberOfDataSets++;
208      this.volume= data;
209      this.legendLabels[ StockChartDataType.VOLUME.getInt() ]= legendLabel;
210      this.paints[ StockChartDataType.VOLUME.getInt() ]= paint;
211      }
212      */

213
214     /******************************************************************************************
215      *
216      * @param index
217      * @return double
218      *******************************************************************************************/

219     public double getHighValue( int index )
220     {
221         return this.high[ index ];
222     }
223
224
225     /******************************************************************************************
226      *
227      * @param index
228      * @return double
229      *******************************************************************************************/

230     public double getLowValue( int index )
231     {
232         return this.low[ index ];
233     }
234
235
236     /******************************************************************************************
237      *
238      * @param index
239      * @return double
240      *******************************************************************************************/

241     public double getCloseValue( int index )
242     {
243         return this.close[ index ];
244     }
245
246
247     /******************************************************************************************
248      *
249      * @return boolean
250      *******************************************************************************************/

251     public boolean hasCloseValues()
252     {
253         return ( this.close != null );
254     }
255
256
257     /******************************************************************************************
258      *
259      * @param index
260      * @return double
261      *******************************************************************************************/

262     public double getOpenValue( int index )
263     {
264         return this.open[ index ];
265     }
266
267
268     /******************************************************************************************
269      *
270      * @return boolean
271      *******************************************************************************************/

272     public boolean hasOpenValues()
273     {
274         return ( this.open != null );
275     }
276
277
278     /******************************************************************************************
279      *
280      * @param index
281      * @return double
282      *******************************************************************************************
283      public double getVolumeValue( int index )
284      {
285      return this.volume[ index ];
286      }
287      */

288
289     /******************************************************************************************
290      *
291      * @return boolean
292      *******************************************************************************************
293      public boolean hasVolumeValues()
294      {
295      return ( this.volume != null );
296      }
297      */

298
299     /******************************************************************************************
300      * Returns the type constant that this data set should be plotted as.
301      *
302      * @return ChartType
303      * @see ChartType
304      *******************************************************************************************/

305     public ChartType getChartType()
306     {
307         return this.chartType;
308     }
309
310
311     /******************************************************************************************
312      * Returns the chart specific properties
313      *
314      * @return ChartTypeProperties
315      *******************************************************************************************/

316     public ChartTypeProperties getChartTypeProperties()
317     {
318         return this.stockChartProperties;
319     }
320
321
322     /******************************************************************************************
323      * Returns the number of IAxisChartDataSet Objects in this series
324      *
325      * @return int
326      ******************************************************************************************/

327     public int getNumberOfDataSets()
328     {
329         return this.numberOfDataSets;
330     }
331
332
333     /******************************************************************************************
334      * Returns the number of IAxisChartDataSet Objects in this series
335      *
336      * @return int
337      ******************************************************************************************/

338     public Paint getPaint( int index )
339     {
340         return this.paints[ index ];
341     }
342
343
344     /*********************************************************************************************
345      * Enables the testing routines to display the contents of this Object.
346      *
347      * @param htmlGenerator
348      **********************************************************************************************/

349     public void toHTML( HTMLGenerator htmlGenerator )
350     {
351         //super.toHTML( htmlGenerator );
352

353 /*
354         //String name= this.getClass().getSuperclass().getName() + "->";
355
356         Field[] fields= this.getClass().getDeclaredFields();
357         for( int i=0; i< fields.length; i++ )
358         {
359             htmlGenerator.addTableRow( fields[ i ].getName(), fields[ i ].get( this ) );
360         }
361 */

362     }
363
364
365 }
366
Popular Tags