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.HashMap;
032: import java.util.List;
033: import java.util.Map;
034:
035: import net.sf.jasperreports.charts.JRTimeSeries;
036: import net.sf.jasperreports.charts.JRTimeSeriesDataset;
037: import net.sf.jasperreports.charts.util.TimeSeriesLabelGenerator;
038: import net.sf.jasperreports.engine.JRChartDataset;
039: import net.sf.jasperreports.engine.JRExpressionCollector;
040: import net.sf.jasperreports.engine.JRRuntimeException;
041: import net.sf.jasperreports.engine.design.JRVerifier;
042: import net.sf.jasperreports.engine.fill.JRCalculator;
043: import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
044: import net.sf.jasperreports.engine.fill.JRFillChartDataset;
045: import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
046:
047: import org.jfree.data.general.Dataset;
048: import org.jfree.data.time.RegularTimePeriod;
049: import org.jfree.data.time.TimeSeries;
050: import org.jfree.data.time.TimeSeriesCollection;
051:
052: /**
053: * @author Flavius Sana (flavius_sana@users.sourceforge.net)
054: * @version $Id: JRFillTimeSeriesDataset.java 1531 2006-12-21 17:38:30Z teodord $
055: */
056: public class JRFillTimeSeriesDataset extends JRFillChartDataset
057: implements JRTimeSeriesDataset {
058:
059: /**
060: *
061: */
062: protected JRFillTimeSeries[] timeSeries = null;
063:
064: private List seriesNames = null;
065: private Map seriesMap = null;
066: private Map labelsMap = null;
067: private Map itemHyperlinks;
068:
069: public JRFillTimeSeriesDataset(
070: JRTimeSeriesDataset timeSeriesDataset,
071: JRFillObjectFactory factory) {
072: super (timeSeriesDataset, factory);
073:
074: JRTimeSeries[] srcTimeSeries = timeSeriesDataset.getSeries();
075: if (srcTimeSeries != null && srcTimeSeries.length > 0) {
076: timeSeries = new JRFillTimeSeries[srcTimeSeries.length];
077: for (int i = 0; i < timeSeries.length; i++) {
078: timeSeries[i] = (JRFillTimeSeries) factory
079: .getTimeSeries(srcTimeSeries[i]);
080: }
081: }
082: }
083:
084: public JRTimeSeries[] getSeries() {
085: return timeSeries;
086: }
087:
088: protected void customInitialize() {
089: seriesNames = null;
090: seriesMap = null;
091: labelsMap = null;
092: itemHyperlinks = null;
093: }
094:
095: protected void customEvaluate(JRCalculator calculator)
096: throws JRExpressionEvalException {
097: if (timeSeries != null && timeSeries.length > 0) {
098: for (int i = 0; i < timeSeries.length; i++) {
099: timeSeries[i].evaluate(calculator);
100: }
101: }
102: }
103:
104: protected void customIncrement() {
105: if (timeSeries != null && timeSeries.length > 0) {
106: if (seriesNames == null) {
107: seriesNames = new ArrayList();
108: seriesMap = new HashMap();
109: labelsMap = new HashMap();
110: itemHyperlinks = new HashMap();
111: }
112:
113: for (int i = 0; i < timeSeries.length; i++) {
114: JRFillTimeSeries crtTimeSeries = timeSeries[i];
115:
116: Comparable seriesName = crtTimeSeries.getSeries();
117: if (seriesName == null) {
118: throw new JRRuntimeException(
119: "Time series name is null.");
120: }
121:
122: TimeSeries series = (TimeSeries) seriesMap
123: .get(seriesName);
124: if (series == null) {
125: series = new TimeSeries(seriesName.toString(),
126: getTimePeriod());
127: seriesNames.add(seriesName);
128: seriesMap.put(seriesName, series);
129: }
130:
131: RegularTimePeriod tp = RegularTimePeriod
132: .createInstance(getTimePeriod(), crtTimeSeries
133: .getTimePeriod(), getTimeZone());
134:
135: series.addOrUpdate(tp, crtTimeSeries.getValue());
136:
137: if (crtTimeSeries.getLabelExpression() != null) {
138: Map seriesLabels = (Map) labelsMap.get(seriesName);
139: if (seriesLabels == null) {
140: seriesLabels = new HashMap();
141: labelsMap.put(seriesName, seriesLabels);
142: }
143:
144: seriesLabels.put(tp, crtTimeSeries.getLabel());
145: }
146:
147: if (crtTimeSeries.hasItemHyperlink()) {
148: Map seriesLinks = (Map) itemHyperlinks
149: .get(seriesName);
150: if (seriesLinks == null) {
151: seriesLinks = new HashMap();
152: itemHyperlinks.put(seriesName, seriesLinks);
153: }
154: seriesLinks.put(tp, crtTimeSeries
155: .getPrintItemHyperlink());
156: }
157: }
158: }
159: }
160:
161: public Dataset getCustomDataset() {
162: TimeSeriesCollection dataset = new TimeSeriesCollection();
163: if (seriesNames != null) {
164: for (int i = 0; i < seriesNames.size(); i++) {
165: Comparable seriesName = (Comparable) seriesNames.get(i);
166: dataset.addSeries((TimeSeries) seriesMap
167: .get(seriesName));
168: }
169: }
170: return dataset;
171: }
172:
173: public Class getTimePeriod() {
174: return ((JRTimeSeriesDataset) parent).getTimePeriod();
175: }
176:
177: public void setTimePeriod(Class timePeriod) {
178: }
179:
180: /**
181: *
182: */
183: public byte getDatasetType() {
184: return JRChartDataset.TIMESERIES_DATASET;
185: }
186:
187: public TimeSeriesLabelGenerator getLabelGenerator() {
188: return new TimeSeriesLabelGenerator(labelsMap);
189: }
190:
191: /**
192: *
193: */
194: public void collectExpressions(JRExpressionCollector collector) {
195: collector.collect(this );
196: }
197:
198: public Map getItemHyperlinks() {
199: return itemHyperlinks;
200: }
201:
202: public boolean hasItemHyperlinks() {
203: boolean foundLinks = false;
204: if (timeSeries != null && timeSeries.length > 0) {
205: for (int i = 0; i < timeSeries.length && !foundLinks; i++) {
206: foundLinks = timeSeries[i].hasItemHyperlink();
207: }
208: }
209: return foundLinks;
210: }
211:
212: public void validate(JRVerifier verifier) {
213: verifier.verify(this);
214: }
215:
216: }
|