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.JRXyDataset;
036: import net.sf.jasperreports.charts.JRXySeries;
037: import net.sf.jasperreports.charts.util.XYDatasetLabelGenerator;
038: import net.sf.jasperreports.engine.JRChartDataset;
039: import net.sf.jasperreports.engine.JRExpressionCollector;
040: import net.sf.jasperreports.engine.design.JRVerifier;
041: import net.sf.jasperreports.engine.fill.JRCalculator;
042: import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
043: import net.sf.jasperreports.engine.fill.JRFillChartDataset;
044: import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
045: import net.sf.jasperreports.engine.util.Pair;
046:
047: import org.jfree.data.general.Dataset;
048: import org.jfree.data.xy.XYSeries;
049: import org.jfree.data.xy.XYSeriesCollection;
050:
051: /**
052: * @author Teodor Danciu (teodord@users.sourceforge.net)
053: * @version $Id: JRFillXyDataset.java 1364 2006-08-31 15:13:20Z lucianc $
054: */
055: public class JRFillXyDataset extends JRFillChartDataset implements
056: JRXyDataset {
057:
058: /**
059: *
060: */
061: protected JRFillXySeries[] xySeries = null;
062:
063: private List seriesNames = null;
064: private Map seriesMap = null;
065: private Map labelsMap = null;
066:
067: private Map itemHyperlinks;
068:
069: /**
070: *
071: */
072: public JRFillXyDataset(JRXyDataset xyDataset,
073: JRFillObjectFactory factory) {
074: super (xyDataset, factory);
075:
076: /* */
077: JRXySeries[] srcXySeries = xyDataset.getSeries();
078: if (srcXySeries != null && srcXySeries.length > 0) {
079: xySeries = new JRFillXySeries[srcXySeries.length];
080: for (int i = 0; i < xySeries.length; i++) {
081: xySeries[i] = (JRFillXySeries) factory
082: .getXySeries(srcXySeries[i]);
083: }
084: }
085: }
086:
087: /**
088: *
089: */
090: public JRXySeries[] getSeries() {
091: return xySeries;
092: }
093:
094: /**
095: *
096: */
097: protected void customInitialize() {
098: seriesNames = null;
099: seriesMap = null;
100: labelsMap = null;
101: itemHyperlinks = null;
102: }
103:
104: /**
105: *
106: */
107: protected void customEvaluate(JRCalculator calculator)
108: throws JRExpressionEvalException {
109: if (xySeries != null && xySeries.length > 0) {
110: for (int i = 0; i < xySeries.length; i++) {
111: xySeries[i].evaluate(calculator);
112: }
113: }
114: }
115:
116: /**
117: *
118: */
119: protected void customIncrement() {
120: if (xySeries != null && xySeries.length > 0) {
121: if (seriesNames == null) {
122: seriesNames = new ArrayList();
123: seriesMap = new HashMap();
124: labelsMap = new HashMap();
125: itemHyperlinks = new HashMap();
126: }
127:
128: for (int i = 0; i < xySeries.length; i++) {
129: JRFillXySeries crtXySeries = xySeries[i];
130:
131: Comparable seriesName = crtXySeries.getSeries();
132: XYSeries xySrs = (XYSeries) seriesMap.get(seriesName);
133: if (xySrs == null) {
134: xySrs = new XYSeries(seriesName);
135: seriesNames.add(seriesName);
136: seriesMap.put(seriesName, xySrs);
137: }
138:
139: xySrs.addOrUpdate(crtXySeries.getXValue(), crtXySeries
140: .getYValue());
141:
142: if (crtXySeries.getLabelExpression() != null) {
143: Map seriesLabels = (Map) labelsMap.get(seriesName);
144: if (seriesLabels == null) {
145: seriesLabels = new HashMap();
146: labelsMap.put(seriesName, seriesLabels);
147: }
148:
149: seriesLabels.put(crtXySeries.getXValue(),
150: crtXySeries.getLabel());
151: }
152:
153: if (crtXySeries.hasItemHyperlinks()) {
154: Map seriesLinks = (Map) itemHyperlinks
155: .get(seriesName);
156: if (seriesLinks == null) {
157: seriesLinks = new HashMap();
158: itemHyperlinks.put(seriesName, seriesLinks);
159: }
160: Pair xyKey = new Pair(crtXySeries.getXValue(),
161: crtXySeries.getYValue());
162: seriesLinks.put(xyKey, crtXySeries
163: .getPrintItemHyperlink());
164: }
165: }
166: }
167: }
168:
169: /**
170: *
171: */
172: public Dataset getCustomDataset() {
173: XYSeriesCollection dataset = new XYSeriesCollection();
174: if (seriesNames != null) {
175: for (int i = 0; i < seriesNames.size(); i++) {
176: Comparable seriesName = (Comparable) seriesNames.get(i);
177: dataset.addSeries((XYSeries) seriesMap.get(seriesName));
178: }
179: }
180: return dataset;
181: }
182:
183: /**
184: *
185: */
186: public byte getDatasetType() {
187: return JRChartDataset.XY_DATASET;
188: }
189:
190: /**
191: *
192: */
193: public XYDatasetLabelGenerator getLabelGenerator() {
194: return new XYDatasetLabelGenerator(labelsMap);
195: }
196:
197: /**
198: *
199: */
200: public void collectExpressions(JRExpressionCollector collector) {
201: collector.collect(this );
202: }
203:
204: public Map getItemHyperlinks() {
205: return itemHyperlinks;
206: }
207:
208: public boolean hasItemHyperlinks() {
209: boolean foundLinks = false;
210: if (xySeries != null && xySeries.length > 0) {
211: for (int i = 0; i < xySeries.length && !foundLinks; i++) {
212: JRFillXySeries serie = xySeries[i];
213: foundLinks = serie.hasItemHyperlinks();
214: }
215: }
216: return foundLinks;
217: }
218:
219: public void validate(JRVerifier verifier) {
220: verifier.verify(this);
221: }
222:
223: }
|