001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.charts.fill;
029:
030: import java.util.ArrayList;
031: import java.util.Date;
032: import java.util.List;
033:
034: import net.sf.jasperreports.charts.JRHighLowDataset;
035: import net.sf.jasperreports.engine.JRChartDataset;
036: import net.sf.jasperreports.engine.JRException;
037: import net.sf.jasperreports.engine.JRExpression;
038: import net.sf.jasperreports.engine.JRExpressionCollector;
039: import net.sf.jasperreports.engine.JRHyperlink;
040: import net.sf.jasperreports.engine.JRHyperlinkHelper;
041: import net.sf.jasperreports.engine.JRPrintHyperlink;
042: import net.sf.jasperreports.engine.JRRuntimeException;
043: import net.sf.jasperreports.engine.design.JRVerifier;
044: import net.sf.jasperreports.engine.fill.JRCalculator;
045: import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
046: import net.sf.jasperreports.engine.fill.JRFillChartDataset;
047: import net.sf.jasperreports.engine.fill.JRFillHyperlinkHelper;
048: import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
049:
050: import org.jfree.data.general.Dataset;
051: import org.jfree.data.xy.DefaultHighLowDataset;
052:
053: /**
054: * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
055: * @version $Id: JRFillHighLowDataset.java 1720 2007-05-07 10:02:56Z lucianc $
056: */
057: public class JRFillHighLowDataset extends JRFillChartDataset implements
058: JRHighLowDataset {
059:
060: /**
061: *
062: */
063: private String series = null;
064: private List elements = new ArrayList();
065: private Date date = null;
066: private Number high = null;
067: private Number low = null;
068: private Number open = null;
069: private Number close = null;
070: private Number volume = null;
071:
072: private JRPrintHyperlink itemHyperlink;
073: private List itemHyperlinks;
074:
075: /**
076: *
077: */
078: public JRFillHighLowDataset(JRHighLowDataset dataset,
079: JRFillObjectFactory factory) {
080: super (dataset, factory);
081: }
082:
083: protected void customInitialize() {
084: elements = new ArrayList();
085: itemHyperlinks = new ArrayList();
086: }
087:
088: protected void customEvaluate(JRCalculator calculator)
089: throws JRExpressionEvalException {
090: series = (String) calculator.evaluate(getSeriesExpression());
091: date = (Date) calculator.evaluate(getDateExpression());
092: high = (Number) calculator.evaluate(getHighExpression());
093: low = (Number) calculator.evaluate(getLowExpression());
094: open = (Number) calculator.evaluate(getOpenExpression());
095: close = (Number) calculator.evaluate(getCloseExpression());
096: volume = (Number) calculator.evaluate(getVolumeExpression());
097:
098: if (hasItemHyperlink()) {
099: evaluateSectionHyperlink(calculator);
100: }
101: }
102:
103: protected void evaluateSectionHyperlink(JRCalculator calculator)
104: throws JRExpressionEvalException {
105: try {
106: itemHyperlink = JRFillHyperlinkHelper.evaluateHyperlink(
107: getItemHyperlink(), calculator,
108: JRExpression.EVALUATION_DEFAULT);
109: } catch (JRExpressionEvalException e) {
110: throw e;
111: } catch (JRException e) {
112: throw new JRRuntimeException(e);
113: }
114: }
115:
116: protected void customIncrement() {
117: elements.add(new HighLowElement(date, high, low, open, close,
118: volume));
119:
120: if (hasItemHyperlink()) {
121: itemHyperlinks.add(itemHyperlink);
122: }
123: }
124:
125: public Dataset getCustomDataset() {
126: int size = elements.size();
127: if (size > 0) {
128: Date[] dateArray = new Date[size];
129: double[] highArray = new double[size];
130: double[] lowArray = new double[size];
131: double[] openArray = new double[size];
132: double[] closeArray = new double[size];
133: double[] volumeArray = new double[size];
134:
135: for (int i = 0; i < elements.size(); i++) {
136: HighLowElement bean = (HighLowElement) elements.get(i);
137: dateArray[i] = new Date(bean.getDate().getTime());
138: highArray[i] = bean.getHigh().doubleValue();
139: lowArray[i] = bean.getLow().doubleValue();
140: openArray[i] = bean.getOpen().doubleValue();
141: closeArray[i] = bean.getClose().doubleValue();
142: volumeArray[i] = bean.getVolume().doubleValue();
143: }
144:
145: return new DefaultHighLowDataset(series, dateArray,
146: highArray, lowArray, openArray, closeArray,
147: volumeArray);
148: }
149:
150: return null;
151: }
152:
153: public JRExpression getSeriesExpression() {
154: return ((JRHighLowDataset) parent).getSeriesExpression();
155: }
156:
157: public JRExpression getDateExpression() {
158: return ((JRHighLowDataset) parent).getDateExpression();
159: }
160:
161: public JRExpression getHighExpression() {
162: return ((JRHighLowDataset) parent).getHighExpression();
163: }
164:
165: public JRExpression getLowExpression() {
166: return ((JRHighLowDataset) parent).getLowExpression();
167: }
168:
169: public JRExpression getOpenExpression() {
170: return ((JRHighLowDataset) parent).getOpenExpression();
171: }
172:
173: public JRExpression getCloseExpression() {
174: return ((JRHighLowDataset) parent).getCloseExpression();
175: }
176:
177: public JRExpression getVolumeExpression() {
178: return ((JRHighLowDataset) parent).getVolumeExpression();
179: }
180:
181: /**
182: *
183: */
184: private static class HighLowElement {
185: Date date;
186: Number high;
187: Number low;
188: Number open;
189: Number close;
190: Number volume;
191:
192: public HighLowElement(Date date, Number high, Number low,
193: Number open, Number close, Number volume) {
194: if (date == null)
195: throw new JRRuntimeException(
196: "Date value is null in high-low series.");
197: this .date = date;
198:
199: if (high == null)
200: throw new JRRuntimeException(
201: "High value is null in high-low series.");
202: this .high = high;
203:
204: if (low == null)
205: throw new JRRuntimeException(
206: "Low value is null in high-low series.");
207: this .low = low;
208:
209: if (open == null)
210: throw new JRRuntimeException(
211: "Open value is null in high-low series.");
212: this .open = open;
213:
214: if (close == null)
215: throw new JRRuntimeException(
216: "Close value is null in high-low series.");
217: this .close = close;
218:
219: if (volume == null)
220: throw new JRRuntimeException(
221: "Volume value is null in high-low series.");
222: this .volume = volume;
223: }
224:
225: public Date getDate() {
226: return date;
227: }
228:
229: public void setDate(Date date) {
230: this .date = date;
231: }
232:
233: public Number getHigh() {
234: return high;
235: }
236:
237: public void setHigh(Number high) {
238: this .high = high;
239: }
240:
241: public Number getLow() {
242: return low;
243: }
244:
245: public void setLow(Number low) {
246: this .low = low;
247: }
248:
249: public Number getOpen() {
250: return open;
251: }
252:
253: public void setOpen(Number open) {
254: this .open = open;
255: }
256:
257: public Number getClose() {
258: return close;
259: }
260:
261: public void setClose(Number close) {
262: this .close = close;
263: }
264:
265: public Number getVolume() {
266: return volume;
267: }
268:
269: public void setVolume(Number volume) {
270: this .volume = volume;
271: }
272: }
273:
274: /**
275: *
276: */
277: public byte getDatasetType() {
278: return JRChartDataset.HIGHLOW_DATASET;
279: }
280:
281: /**
282: *
283: */
284: public void collectExpressions(JRExpressionCollector collector) {
285: collector.collect(this );
286: }
287:
288: public JRHyperlink getItemHyperlink() {
289: return ((JRHighLowDataset) parent).getItemHyperlink();
290: }
291:
292: public boolean hasItemHyperlink() {
293: return !JRHyperlinkHelper.isEmpty(getItemHyperlink());
294: }
295:
296: public List getItemHyperlinks() {
297: return itemHyperlinks;
298: }
299:
300: public void validate(JRVerifier verifier) {
301: verifier.verify(this);
302: }
303:
304: }
|