001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures and contributers
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of
013: * the License, or (at your option) any later version.
014: *
015: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
016: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
017: * See the GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License along with this
020: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021: * Boston, MA 02111-1307, USA.
022: */
023: package de.laures.cewolf.taglib;
024:
025: import java.io.Serializable;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.jfree.chart.plot.CategoryPlot;
031: import org.jfree.chart.plot.DrawingSupplier;
032: import org.jfree.chart.plot.Plot;
033: import org.jfree.chart.plot.XYPlot;
034: import org.jfree.chart.renderer.AbstractRenderer;
035: import org.jfree.chart.renderer.category.CategoryItemRenderer;
036: import org.jfree.chart.renderer.xy.XYItemRenderer;
037: import org.jfree.data.category.CategoryDataset;
038: import org.jfree.data.general.Dataset;
039: import org.jfree.data.xy.IntervalXYDataset;
040: import org.jfree.data.xy.OHLCDataset;
041: import org.jfree.data.xy.XYDataset;
042:
043: import de.laures.cewolf.ChartValidationException;
044: import de.laures.cewolf.DatasetProduceException;
045: import de.laures.cewolf.DatasetProducer;
046:
047: /**
048: * (sub) Plot definition for combined/overlaid charts.
049: *
050: * @author Chris McCann
051: * @author Guido Laures
052: */
053: public class PlotDefinition implements DataAware, Serializable,
054: TaglibConstants, PlotConstants {
055:
056: private transient Log log = LogFactory.getLog(getClass());
057:
058: private String type;
059: private String xAxisLabel; // [tb]
060: private String yAxisLabel; // [tb]
061:
062: private DataContainer dataAware = new DataContainer();
063: private transient Plot plot;
064: private transient DrawingSupplier drawingSupplier = null;
065:
066: public Plot getPlot(int chartType) throws DatasetProduceException,
067: ChartValidationException {
068: log.debug("Plot.getPlot: chartType: " + chartType);
069: if (plot == null) {
070: int rendererIndex = PlotTypes.getRendererIndex(type);
071:
072: Dataset data = (Dataset) getDataset();
073: log.debug("Plot.getPlot: data name: "
074: + data.getClass().getName());
075: AbstractRenderer rend = PlotTypes
076: .getRenderer(rendererIndex);
077: log.debug("Plot.getPlot: rendererIndex: " + rendererIndex);
078: if (chartType == ChartConstants.OVERLAY_XY
079: || chartType == ChartConstants.COMBINED_XY) {
080: switch (rendererIndex) {
081: case XY_AREA:
082: case XY_LINE:
083: case XY_SHAPES_AND_LINES:
084: case SCATTER:
085: case STEP:
086: check(data, XYDataset.class, rendererIndex);
087: plot = new XYPlot((XYDataset) data, null, null,
088: (XYItemRenderer) rend);
089: break;
090: case XY_VERTICAL_BAR:
091: check(data, IntervalXYDataset.class, rendererIndex);
092: plot = new XYPlot((IntervalXYDataset) data, null,
093: null, (XYItemRenderer) rend);
094: break;
095: case CANDLESTICK:
096: case HIGH_LOW:
097: check(data, OHLCDataset.class, rendererIndex);
098: plot = new XYPlot((OHLCDataset) data, null, null,
099: (XYItemRenderer) rend);
100: break;
101: //case SIGNAL :
102: // check(data, SignalsDataset.class, rendererIndex);
103: // plot = new XYPlot((SignalsDataset) data, null, null, (XYItemRenderer) rend);
104: default:
105: throw new AttributeValidationException(chartType
106: + ".type", type);
107: }
108: } else if (chartType == ChartConstants.OVERLAY_CATEGORY) {
109: switch (rendererIndex) {
110: case AREA:
111: case VERTICAL_BAR:
112: case LINE:
113: case SHAPES_AND_LINES:
114: check(data, CategoryDataset.class, rendererIndex);
115: plot = new CategoryPlot((CategoryDataset) data,
116: null, null, (CategoryItemRenderer) rend);
117: break;
118: default:
119: throw new AttributeValidationException(chartType
120: + ".type", type);
121: }
122: }
123: }
124: plot.setDrawingSupplier(drawingSupplier);
125: return plot;
126: }
127:
128: public Object getDataset() throws DatasetProduceException {
129: return dataAware.getDataset();
130: }
131:
132: /**
133: * Gets the y-axis label. [tb]
134: *
135: * @return the y-axis label.
136: */
137: public String getXaxislabel() {
138: return xAxisLabel;
139: }
140:
141: /**
142: * Sets the x-axis label [tb]
143: *
144: * @return the x-axis label
145: */
146: public String getYaxislabel() {
147: return yAxisLabel;
148: }
149:
150: /**
151: * Sets the x-axis label [tb]
152: *
153: * @param xAxisLabel New value of property xAxisLabel.
154: */
155: public void setXaxislabel(String xAxisLabel) {
156: this .xAxisLabel = xAxisLabel;
157: }
158:
159: /**
160: * Sets the y-axis label [tb]
161: *
162: * @param yAxisLabel New value of property yAxisLabel.
163: */
164: public void setYaxislabel(String yAxisLabel) {
165: this .yAxisLabel = yAxisLabel;
166: }
167:
168: /**
169: * Sets the type.
170: * @param type The type to set
171: */
172: public void setType(String type) {
173: this .type = type;
174: }
175:
176: /**
177: * Gets the type.
178: * @return type of plot as a String
179: */
180: public String getType() {
181: return this .type;
182: }
183:
184: public void check(Dataset data, Class clazz, int plotType)
185: throws IncompatibleDatasetException {
186: if (!clazz.isInstance(data)) {
187: throw new IncompatibleDatasetException("Plots of type "
188: + PlotTypes.typeNames[plotType]
189: + " need a dataset of type " + clazz.getName());
190: }
191: }
192:
193: public void setDataProductionConfig(DatasetProducer dsp,
194: Map params, boolean useCache) {
195: log.debug("setDataProductionConfig(" + dsp + ", " + params);
196: dataAware.setDataProductionConfig(dsp, params, useCache);
197: }
198:
199: /**
200: * Sets the drawingSupplier.
201: * @param drawingSupplier The drawingSupplier to set
202: */
203: public void setDrawingSupplier(DrawingSupplier drawingSupplier) {
204: this.drawingSupplier = drawingSupplier;
205: }
206:
207: }
|