001: /***********************************************************************************************
002: * File Info: $Id: StockChartDataSet.java,v 1.1 2003/05/17 16:58:11 nathaniel_auvil Exp $
003: * Copyright (C) 2002
004: * Author: Nathaniel G. Auvil
005: * Contributor(s):
006: *
007: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
008: *
009: * Redistribution and use of this software and associated documentation ("Software"), with or
010: * without modification, are permitted provided that the following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and notices.
013: * Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
016: * conditions and the following disclaimer in the documentation and/or other materials
017: * provided with the distribution.
018: *
019: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
020: * products derived from this Software without prior written permission of Nathaniel G.
021: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
022: *
023: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
024: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
025: * registered trademark of Nathaniel G. Auvil.
026: *
027: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
028: *
029: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
030: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
031: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
033: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
034: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
037: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: ************************************************************************************************/package org.krysalis.jcharts.chartData;
039:
040: import org.krysalis.jcharts.chartData.interfaces.IStockChartDataSet;
041: import org.krysalis.jcharts.properties.*;
042: import org.krysalis.jcharts.test.HTMLGenerator;
043: import org.krysalis.jcharts.test.HTMLTestable;
044: import org.krysalis.jcharts.types.ChartType;
045: import org.krysalis.jcharts.types.StockChartDataType;
046:
047: import java.awt.*;
048:
049: public class StockChartDataSet implements IStockChartDataSet,
050: HTMLTestable {
051: private ChartType chartType = ChartType.STOCK;
052:
053: private double[] high;
054: private double[] low;
055: private double[] open;
056: private double[] close;
057:
058: //---keep this value for reference sake. Start with high and low
059: private int numberOfDataSets = 2;
060:
061: private String[] legendLabels;
062: private Paint[] paints;
063: private StockChartProperties stockChartProperties;
064:
065: /******************************************************************************************
066: * Constructor
067: *
068: * @param high
069: * @param highLegendLabel
070: * @param low
071: * @param lowLegendLabel
072: * @param stockChartProperties properties Object specific to the type of chart you are rendering.
073: * @throws ChartDataException performs a limited validation of the data
074: *******************************************************************************************/
075: public StockChartDataSet(double[] high, String highLegendLabel,
076: double[] low, String lowLegendLabel, Paint highLowPaint,
077: StockChartProperties stockChartProperties)
078: throws ChartDataException {
079: this .high = high;
080: this .low = low;
081:
082: this .legendLabels = new String[5];
083: this .legendLabels[StockChartDataType.HIGH.getInt()] = highLegendLabel;
084: this .legendLabels[StockChartDataType.LOW.getInt()] = lowLegendLabel;
085:
086: this .paints = new Paint[5];
087: this .paints[StockChartDataType.HIGH.getInt()] = highLowPaint;
088: this .paints[StockChartDataType.LOW.getInt()] = highLowPaint;
089:
090: this .stockChartProperties = stockChartProperties;
091: }
092:
093: /************************************************************************************************
094: * Performs a limited validation of data passed to Constructor.
095: *
096: * @throws ChartDataException
097: * @throws PropertyException
098: *************************************************************************************************/
099: public void validate() throws ChartDataException, PropertyException {
100: if (high == null || low == null) {
101: throw new ChartDataException(
102: "The Hi/Low values can not be NULL.");
103: }
104:
105: if (high.length != low.length) {
106: throw new ChartDataException(
107: "The Hi/Low Arrays must have equal length.");
108: }
109:
110: if (this .paints[StockChartDataType.HIGH.getInt()] == null) {
111: throw new ChartDataException(
112: "The Hi/Low Paint implementation can not be NULL.");
113: }
114:
115: this .stockChartProperties.validate(this );
116: }
117:
118: /******************************************************************************************
119: * Returns the legend label for the passed index. This index corresponds to the DataSet
120: * for which label you want.
121: *
122: * @param index
123: * @return String
124: *******************************************************************************************/
125: public String getLegendLabel(int index) {
126: return this .legendLabels[index];
127: }
128:
129: /*********************************************************************************************
130: * Returns the number of Legend Labels to display.
131: *
132: * @return int
133: **********************************************************************************************/
134: public int getNumberOfLegendLabels() {
135: return this .legendLabels.length;
136: }
137:
138: /******************************************************************************************
139: * Returns the number of elements in the data set. All data sets must be of the same length
140: * so just look at the first one.
141: *
142: * @return int
143: *******************************************************************************************/
144: public int getNumberOfDataItems() {
145: //---always have a high and a low
146: return this .high.length;
147: }
148:
149: /***************************************************************************************************
150: * Sets the 'Close' values
151: *
152: * @param data
153: * @param legendLabel
154: * @param paint
155: **************************************************************************************************/
156: public void setCloseValues(double[] data, String legendLabel,
157: Paint paint) {
158: this .numberOfDataSets++;
159: this .close = data;
160: this .legendLabels[StockChartDataType.CLOSE.getInt()] = legendLabel;
161: this .paints[StockChartDataType.CLOSE.getInt()] = paint;
162: }
163:
164: /***************************************************************************************************
165: * Sets the 'Open' values
166: *
167: * @param data
168: * @param legendLabel
169: * @param paint
170: **************************************************************************************************/
171: public void setOpenValues(double[] data, String legendLabel,
172: Paint paint) {
173: this .numberOfDataSets++;
174: this .open = data;
175: this .legendLabels[StockChartDataType.OPEN.getInt()] = legendLabel;
176: this .paints[StockChartDataType.OPEN.getInt()] = paint;
177: }
178:
179: /*********************************************************************************************
180: * Sets the 'Volume' values
181: *
182: * @param data[]
183: * @param legendLabel
184: * @param paint
185: *********************************************************************************************
186: public void setVolumeValues( double[] data, String legendLabel, Paint paint )
187: {
188: this.numberOfDataSets++;
189: this.volume= data;
190: this.legendLabels[ StockChartDataType.VOLUME.getInt() ]= legendLabel;
191: this.paints[ StockChartDataType.VOLUME.getInt() ]= paint;
192: }
193: */
194:
195: /******************************************************************************************
196: *
197: * @param index
198: * @return double
199: *******************************************************************************************/
200: public double getHighValue(int index) {
201: return this .high[index];
202: }
203:
204: /******************************************************************************************
205: *
206: * @param index
207: * @return double
208: *******************************************************************************************/
209: public double getLowValue(int index) {
210: return this .low[index];
211: }
212:
213: /******************************************************************************************
214: *
215: * @param index
216: * @return double
217: *******************************************************************************************/
218: public double getCloseValue(int index) {
219: return this .close[index];
220: }
221:
222: /******************************************************************************************
223: *
224: * @return boolean
225: *******************************************************************************************/
226: public boolean hasCloseValues() {
227: return (this .close != null);
228: }
229:
230: /******************************************************************************************
231: *
232: * @param index
233: * @return double
234: *******************************************************************************************/
235: public double getOpenValue(int index) {
236: return this .open[index];
237: }
238:
239: /******************************************************************************************
240: *
241: * @return boolean
242: *******************************************************************************************/
243: public boolean hasOpenValues() {
244: return (this .open != null);
245: }
246:
247: /******************************************************************************************
248: *
249: * @param index
250: * @return double
251: *******************************************************************************************
252: public double getVolumeValue( int index )
253: {
254: return this.volume[ index ];
255: }
256: */
257:
258: /******************************************************************************************
259: *
260: * @return boolean
261: *******************************************************************************************
262: public boolean hasVolumeValues()
263: {
264: return ( this.volume != null );
265: }
266: */
267:
268: /******************************************************************************************
269: * Returns the type constant that this data set should be plotted as.
270: *
271: * @return ChartType
272: * @see ChartType
273: *******************************************************************************************/
274: public ChartType getChartType() {
275: return this .chartType;
276: }
277:
278: /******************************************************************************************
279: * Returns the chart specific properties
280: *
281: * @return ChartTypeProperties
282: *******************************************************************************************/
283: public ChartTypeProperties getChartTypeProperties() {
284: return this .stockChartProperties;
285: }
286:
287: /******************************************************************************************
288: * Returns the number of IAxisChartDataSet Objects in this series
289: *
290: * @return int
291: ******************************************************************************************/
292: public int getNumberOfDataSets() {
293: return this .numberOfDataSets;
294: }
295:
296: /******************************************************************************************
297: * Returns the number of IAxisChartDataSet Objects in this series
298: *
299: * @return int
300: ******************************************************************************************/
301: public Paint getPaint(int index) {
302: return this .paints[index];
303: }
304:
305: /*********************************************************************************************
306: * Enables the testing routines to display the contents of this Object.
307: *
308: * @param htmlGenerator
309: **********************************************************************************************/
310: public void toHTML(HTMLGenerator htmlGenerator) {
311: //super.toHTML( htmlGenerator );
312:
313: /*
314: //String name= this.getClass().getSuperclass().getName() + "->";
315:
316: Field[] fields= this.getClass().getDeclaredFields();
317: for( int i=0; i< fields.length; i++ )
318: {
319: htmlGenerator.addTableRow( fields[ i ].getName(), fields[ i ].get( this ) );
320: }
321: */
322: }
323:
324: }
|