franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures and contributers franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of franta-hg@1: * the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: package de.laures.cewolf.taglib; franta-hg@1: franta-hg@1: import java.io.Serializable; franta-hg@1: import java.util.Map; franta-hg@1: franta-hg@1: import org.apache.commons.logging.Log; franta-hg@1: import org.apache.commons.logging.LogFactory; franta-hg@1: import org.jfree.chart.plot.CategoryPlot; franta-hg@1: import org.jfree.chart.plot.DrawingSupplier; franta-hg@1: import org.jfree.chart.plot.Plot; franta-hg@1: import org.jfree.chart.plot.XYPlot; franta-hg@1: import org.jfree.chart.renderer.AbstractRenderer; franta-hg@1: import org.jfree.chart.renderer.category.CategoryItemRenderer; franta-hg@1: import org.jfree.chart.renderer.xy.XYItemRenderer; franta-hg@1: import org.jfree.data.category.CategoryDataset; franta-hg@1: import org.jfree.data.general.Dataset; franta-hg@1: import org.jfree.data.xy.IntervalXYDataset; franta-hg@1: import org.jfree.data.xy.OHLCDataset; franta-hg@1: import org.jfree.data.xy.XYDataset; franta-hg@1: franta-hg@1: import de.laures.cewolf.ChartValidationException; franta-hg@1: import de.laures.cewolf.DatasetProduceException; franta-hg@1: import de.laures.cewolf.DatasetProducer; franta-hg@1: franta-hg@1: /** franta-hg@1: * (sub) Plot definition for combined/overlaid charts. franta-hg@1: * franta-hg@1: * @author Chris McCann franta-hg@1: * @author Guido Laures franta-hg@1: */ franta-hg@1: public class PlotDefinition implements DataAware, Serializable, TaglibConstants, PlotConstants { franta-hg@1: franta-hg@1: private transient Log log = LogFactory.getLog(getClass()); franta-hg@1: franta-hg@1: private String type; franta-hg@1: private String xAxisLabel; // [tb] franta-hg@1: private String yAxisLabel; // [tb] franta-hg@1: franta-hg@1: private DataContainer dataAware = new DataContainer(); franta-hg@1: private transient Plot plot; franta-hg@1: private transient DrawingSupplier drawingSupplier = null; franta-hg@1: franta-hg@1: public Plot getPlot(int chartType) throws DatasetProduceException, ChartValidationException { franta-hg@1: log.debug("Plot.getPlot: chartType: " + chartType); franta-hg@1: if (plot == null) { franta-hg@1: int rendererIndex = PlotTypes.getRendererIndex(type); franta-hg@1: franta-hg@1: Dataset data = (Dataset) getDataset(); franta-hg@1: log.debug("Plot.getPlot: data name: " +data.getClass().getName()); franta-hg@1: AbstractRenderer rend = PlotTypes.getRenderer(rendererIndex); franta-hg@1: log.debug("Plot.getPlot: rendererIndex: " + rendererIndex); franta-hg@1: if (chartType == ChartConstants.OVERLAY_XY || chartType == ChartConstants.COMBINED_XY) { franta-hg@1: switch (rendererIndex) { franta-hg@1: case XY_AREA : franta-hg@1: case XY_LINE : franta-hg@1: case XY_SHAPES_AND_LINES : franta-hg@1: case SCATTER : franta-hg@1: case STEP : franta-hg@1: check(data, XYDataset.class, rendererIndex); franta-hg@1: plot = new XYPlot((XYDataset) data, null, null, (XYItemRenderer) rend); franta-hg@1: break; franta-hg@1: case XY_VERTICAL_BAR : franta-hg@1: check(data, IntervalXYDataset.class, rendererIndex); franta-hg@1: plot = new XYPlot((IntervalXYDataset) data, null, null, (XYItemRenderer) rend); franta-hg@1: break; franta-hg@1: case CANDLESTICK : franta-hg@1: case HIGH_LOW : franta-hg@1: check(data, OHLCDataset.class, rendererIndex); franta-hg@1: plot = new XYPlot((OHLCDataset) data, null, null, (XYItemRenderer) rend); franta-hg@1: break; franta-hg@1: //case SIGNAL : franta-hg@1: // check(data, SignalsDataset.class, rendererIndex); franta-hg@1: // plot = new XYPlot((SignalsDataset) data, null, null, (XYItemRenderer) rend); franta-hg@1: default : franta-hg@1: throw new AttributeValidationException(chartType + ".type", type); franta-hg@1: } franta-hg@1: } else if (chartType == ChartConstants.OVERLAY_CATEGORY) { franta-hg@1: switch (rendererIndex) { franta-hg@1: case AREA : franta-hg@1: case VERTICAL_BAR : franta-hg@1: case LINE : franta-hg@1: case SHAPES_AND_LINES : franta-hg@1: check(data, CategoryDataset.class, rendererIndex); franta-hg@1: plot = franta-hg@1: new CategoryPlot( franta-hg@1: (CategoryDataset) data, franta-hg@1: null, franta-hg@1: null, franta-hg@1: (CategoryItemRenderer) rend); franta-hg@1: break; franta-hg@1: default : franta-hg@1: throw new AttributeValidationException(chartType + ".type", type); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: plot.setDrawingSupplier(drawingSupplier); franta-hg@1: return plot; franta-hg@1: } franta-hg@1: franta-hg@1: public Object getDataset() throws DatasetProduceException { franta-hg@1: return dataAware.getDataset(); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Gets the y-axis label. [tb] franta-hg@1: * franta-hg@1: * @return the y-axis label. franta-hg@1: */ franta-hg@1: public String getXaxislabel() { franta-hg@1: return xAxisLabel; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the x-axis label [tb] franta-hg@1: * franta-hg@1: * @return the x-axis label franta-hg@1: */ franta-hg@1: public String getYaxislabel() { franta-hg@1: return yAxisLabel; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the x-axis label [tb] franta-hg@1: * franta-hg@1: * @param xAxisLabel New value of property xAxisLabel. franta-hg@1: */ franta-hg@1: public void setXaxislabel(String xAxisLabel) { franta-hg@1: this.xAxisLabel = xAxisLabel; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the y-axis label [tb] franta-hg@1: * franta-hg@1: * @param yAxisLabel New value of property yAxisLabel. franta-hg@1: */ franta-hg@1: public void setYaxislabel(String yAxisLabel) { franta-hg@1: this.yAxisLabel = yAxisLabel; franta-hg@1: } franta-hg@1: /** franta-hg@1: * Sets the type. franta-hg@1: * @param type The type to set franta-hg@1: */ franta-hg@1: public void setType(String type) { franta-hg@1: this.type = type; franta-hg@1: } franta-hg@1: /** franta-hg@1: * Gets the type. franta-hg@1: * @return type of plot as a String franta-hg@1: */ franta-hg@1: public String getType() { franta-hg@1: return this.type; franta-hg@1: } franta-hg@1: franta-hg@1: public void check(Dataset data, Class clazz, int plotType) throws IncompatibleDatasetException { franta-hg@1: if (!clazz.isInstance(data)) { franta-hg@1: throw new IncompatibleDatasetException( franta-hg@1: "Plots of type " + PlotTypes.typeNames[plotType] + " need a dataset of type " + clazz.getName()); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) { franta-hg@1: log.debug("setDataProductionConfig(" + dsp + ", " + params); franta-hg@1: dataAware.setDataProductionConfig(dsp, params, useCache); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the drawingSupplier. franta-hg@1: * @param drawingSupplier The drawingSupplier to set franta-hg@1: */ franta-hg@1: public void setDrawingSupplier(DrawingSupplier drawingSupplier) { franta-hg@1: this.drawingSupplier = drawingSupplier; franta-hg@1: } franta-hg@1: franta-hg@1: }