java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/PlotDefinition.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/PlotDefinition.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,196 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures and contributers
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of
    1.16 + * the License, or (at your option) any later version.
    1.17 + *
    1.18 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.19 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.20 + * See the GNU Lesser General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.23 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.24 + * Boston, MA 02111-1307, USA.
    1.25 + */
    1.26 +package de.laures.cewolf.taglib;
    1.27 +
    1.28 +import java.io.Serializable;
    1.29 +import java.util.Map;
    1.30 +
    1.31 +import org.apache.commons.logging.Log;
    1.32 +import org.apache.commons.logging.LogFactory;
    1.33 +import org.jfree.chart.plot.CategoryPlot;
    1.34 +import org.jfree.chart.plot.DrawingSupplier;
    1.35 +import org.jfree.chart.plot.Plot;
    1.36 +import org.jfree.chart.plot.XYPlot;
    1.37 +import org.jfree.chart.renderer.AbstractRenderer;
    1.38 +import org.jfree.chart.renderer.category.CategoryItemRenderer;
    1.39 +import org.jfree.chart.renderer.xy.XYItemRenderer;
    1.40 +import org.jfree.data.category.CategoryDataset;
    1.41 +import org.jfree.data.general.Dataset;
    1.42 +import org.jfree.data.xy.IntervalXYDataset;
    1.43 +import org.jfree.data.xy.OHLCDataset;
    1.44 +import org.jfree.data.xy.XYDataset;
    1.45 +
    1.46 +import de.laures.cewolf.ChartValidationException;
    1.47 +import de.laures.cewolf.DatasetProduceException;
    1.48 +import de.laures.cewolf.DatasetProducer;
    1.49 +
    1.50 +/**
    1.51 + * (sub) Plot definition for combined/overlaid charts.
    1.52 + * 
    1.53 + * @author Chris McCann
    1.54 + * @author Guido Laures
    1.55 + */
    1.56 +public class PlotDefinition implements DataAware, Serializable, TaglibConstants, PlotConstants {
    1.57 +
    1.58 +	private transient Log log = LogFactory.getLog(getClass());
    1.59 +
    1.60 +	private String type;
    1.61 +	private String xAxisLabel; // [tb]
    1.62 +	private String yAxisLabel; // [tb]
    1.63 +
    1.64 +	private DataContainer dataAware = new DataContainer();
    1.65 +	private transient Plot plot;
    1.66 +	private transient DrawingSupplier drawingSupplier = null;
    1.67 +
    1.68 +	public Plot getPlot(int chartType) throws DatasetProduceException, ChartValidationException {
    1.69 +    log.debug("Plot.getPlot: chartType: " + chartType);
    1.70 +		if (plot == null) {
    1.71 +			int rendererIndex = PlotTypes.getRendererIndex(type);
    1.72 +			
    1.73 +			Dataset data = (Dataset) getDataset();
    1.74 +			log.debug("Plot.getPlot: data name: " +data.getClass().getName());
    1.75 +			AbstractRenderer rend = PlotTypes.getRenderer(rendererIndex);
    1.76 +			log.debug("Plot.getPlot: rendererIndex: " + rendererIndex);
    1.77 +			if (chartType == ChartConstants.OVERLAY_XY || chartType == ChartConstants.COMBINED_XY) {
    1.78 +				switch (rendererIndex) {
    1.79 +					case XY_AREA :
    1.80 +					case XY_LINE :
    1.81 +					case XY_SHAPES_AND_LINES :
    1.82 +					case SCATTER :
    1.83 +					case STEP :
    1.84 +						check(data, XYDataset.class, rendererIndex);
    1.85 +						plot = new XYPlot((XYDataset) data, null, null, (XYItemRenderer) rend);
    1.86 +						break;
    1.87 +					case XY_VERTICAL_BAR :
    1.88 +						check(data, IntervalXYDataset.class, rendererIndex);
    1.89 +						plot = new XYPlot((IntervalXYDataset) data, null, null, (XYItemRenderer) rend);
    1.90 +						break;
    1.91 +					case CANDLESTICK :
    1.92 +					case HIGH_LOW :
    1.93 +						check(data, OHLCDataset.class, rendererIndex);
    1.94 +						plot = new XYPlot((OHLCDataset) data, null, null, (XYItemRenderer) rend);
    1.95 +						break;
    1.96 +					//case SIGNAL :
    1.97 +					//	check(data, SignalsDataset.class, rendererIndex);
    1.98 +					//	plot = new XYPlot((SignalsDataset) data, null, null, (XYItemRenderer) rend);
    1.99 +					default :
   1.100 +						throw new AttributeValidationException(chartType + ".type", type);
   1.101 +				}
   1.102 +			} else if (chartType == ChartConstants.OVERLAY_CATEGORY) {
   1.103 +				switch (rendererIndex) {
   1.104 +					case AREA :
   1.105 +					case VERTICAL_BAR :
   1.106 +					case LINE :
   1.107 +					case SHAPES_AND_LINES :
   1.108 +						check(data, CategoryDataset.class, rendererIndex);
   1.109 +						plot =
   1.110 +							new CategoryPlot(
   1.111 +								(CategoryDataset) data,
   1.112 +								null,
   1.113 +								null,
   1.114 +								(CategoryItemRenderer) rend);
   1.115 +						break;
   1.116 +					default :
   1.117 +						throw new AttributeValidationException(chartType + ".type", type);
   1.118 +				}
   1.119 +			}
   1.120 +		}
   1.121 +		plot.setDrawingSupplier(drawingSupplier);
   1.122 +		return plot;
   1.123 +	}
   1.124 +
   1.125 +	public Object getDataset() throws DatasetProduceException {
   1.126 +		return dataAware.getDataset();
   1.127 +	}
   1.128 +
   1.129 +	/**
   1.130 +	 * Gets the y-axis label. [tb]
   1.131 +	 *
   1.132 +	 * @return the y-axis label.
   1.133 +	 */
   1.134 +	public String getXaxislabel() {
   1.135 +		return xAxisLabel;
   1.136 +	}
   1.137 +
   1.138 +	/**
   1.139 +	 * Sets the x-axis label [tb]
   1.140 +	 *
   1.141 +	 * @return the x-axis label
   1.142 +	 */
   1.143 +	public String getYaxislabel() {
   1.144 +		return yAxisLabel;
   1.145 +	}
   1.146 +
   1.147 +	/**
   1.148 +	 * Sets the x-axis label [tb]
   1.149 +	 *
   1.150 +	 * @param xAxisLabel New value of property xAxisLabel.
   1.151 +	 */
   1.152 +	public void setXaxislabel(String xAxisLabel) {
   1.153 +		this.xAxisLabel = xAxisLabel;
   1.154 +	}
   1.155 +
   1.156 +	/**
   1.157 +	 * Sets the y-axis label [tb]
   1.158 +	 *
   1.159 +	 * @param yAxisLabel New value of property yAxisLabel.
   1.160 +	 */
   1.161 +	public void setYaxislabel(String yAxisLabel) {
   1.162 +		this.yAxisLabel = yAxisLabel;
   1.163 +	}
   1.164 +	/**
   1.165 +	 * Sets the type.
   1.166 +	 * @param type The type to set
   1.167 +	 */
   1.168 +	public void setType(String type) {
   1.169 +		this.type = type;
   1.170 +	}
   1.171 +	/**
   1.172 +	 * Gets the type.
   1.173 +	 * @return type of plot as a String
   1.174 +	 */
   1.175 +	public String getType() {
   1.176 +		return this.type;
   1.177 +	}
   1.178 +
   1.179 +	public void check(Dataset data, Class clazz, int plotType) throws IncompatibleDatasetException {
   1.180 +		if (!clazz.isInstance(data)) {
   1.181 +			throw new IncompatibleDatasetException(
   1.182 +				"Plots of type " + PlotTypes.typeNames[plotType] + " need a dataset of type " + clazz.getName());
   1.183 +		}
   1.184 +	}
   1.185 +
   1.186 +	public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) {
   1.187 +		log.debug("setDataProductionConfig(" + dsp + ", " + params);
   1.188 +		dataAware.setDataProductionConfig(dsp, params, useCache);
   1.189 +	}
   1.190 +
   1.191 +	/**
   1.192 +	 * Sets the drawingSupplier.
   1.193 +	 * @param drawingSupplier The drawingSupplier to set
   1.194 +	 */
   1.195 +	public void setDrawingSupplier(DrawingSupplier drawingSupplier) {
   1.196 +		this.drawingSupplier = drawingSupplier;
   1.197 +	}
   1.198 +
   1.199 +}