java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/PlotDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 01 Mar 2009 11:06:49 +0100
changeset 4 249560bcc466
parent 1 639991d0808a
permissions -rw-r--r--
Doplnění serializovatelnosti
     1 /* ================================================================
     2  * Cewolf : Chart enabling Web Objects Framework
     3  * ================================================================
     4  *
     5  * Project Info:  http://cewolf.sourceforge.net
     6  * Project Lead:  Guido Laures (guido@laures.de);
     7  *
     8  * (C) Copyright 2002, by Guido Laures and contributers
     9  *
    10  * This library is free software; you can redistribute it and/or modify it under the terms
    11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
    12  * either version 2.1 of
    13  * the License, or (at your option) any later version.
    14  *
    15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    16  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    17  * See the GNU Lesser General Public License for more details.
    18  *
    19  * You should have received a copy of the GNU Lesser General Public License along with this
    20  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    21  * Boston, MA 02111-1307, USA.
    22  */
    23 package de.laures.cewolf.taglib;
    24 
    25 import java.io.Serializable;
    26 import java.util.Map;
    27 
    28 import org.apache.commons.logging.Log;
    29 import org.apache.commons.logging.LogFactory;
    30 import org.jfree.chart.plot.CategoryPlot;
    31 import org.jfree.chart.plot.DrawingSupplier;
    32 import org.jfree.chart.plot.Plot;
    33 import org.jfree.chart.plot.XYPlot;
    34 import org.jfree.chart.renderer.AbstractRenderer;
    35 import org.jfree.chart.renderer.category.CategoryItemRenderer;
    36 import org.jfree.chart.renderer.xy.XYItemRenderer;
    37 import org.jfree.data.category.CategoryDataset;
    38 import org.jfree.data.general.Dataset;
    39 import org.jfree.data.xy.IntervalXYDataset;
    40 import org.jfree.data.xy.OHLCDataset;
    41 import org.jfree.data.xy.XYDataset;
    42 
    43 import de.laures.cewolf.ChartValidationException;
    44 import de.laures.cewolf.DatasetProduceException;
    45 import de.laures.cewolf.DatasetProducer;
    46 
    47 /**
    48  * (sub) Plot definition for combined/overlaid charts.
    49  * 
    50  * @author Chris McCann
    51  * @author Guido Laures
    52  */
    53 public class PlotDefinition implements DataAware, Serializable, TaglibConstants, PlotConstants {
    54 
    55 	private transient Log log = LogFactory.getLog(getClass());
    56 
    57 	private String type;
    58 	private String xAxisLabel; // [tb]
    59 	private String yAxisLabel; // [tb]
    60 
    61 	private DataContainer dataAware = new DataContainer();
    62 	private transient Plot plot;
    63 	private transient DrawingSupplier drawingSupplier = null;
    64 
    65 	public Plot getPlot(int chartType) throws DatasetProduceException, ChartValidationException {
    66     log.debug("Plot.getPlot: chartType: " + chartType);
    67 		if (plot == null) {
    68 			int rendererIndex = PlotTypes.getRendererIndex(type);
    69 			
    70 			Dataset data = (Dataset) getDataset();
    71 			log.debug("Plot.getPlot: data name: " +data.getClass().getName());
    72 			AbstractRenderer rend = PlotTypes.getRenderer(rendererIndex);
    73 			log.debug("Plot.getPlot: rendererIndex: " + rendererIndex);
    74 			if (chartType == ChartConstants.OVERLAY_XY || chartType == ChartConstants.COMBINED_XY) {
    75 				switch (rendererIndex) {
    76 					case XY_AREA :
    77 					case XY_LINE :
    78 					case XY_SHAPES_AND_LINES :
    79 					case SCATTER :
    80 					case STEP :
    81 						check(data, XYDataset.class, rendererIndex);
    82 						plot = new XYPlot((XYDataset) data, null, null, (XYItemRenderer) rend);
    83 						break;
    84 					case XY_VERTICAL_BAR :
    85 						check(data, IntervalXYDataset.class, rendererIndex);
    86 						plot = new XYPlot((IntervalXYDataset) data, null, null, (XYItemRenderer) rend);
    87 						break;
    88 					case CANDLESTICK :
    89 					case HIGH_LOW :
    90 						check(data, OHLCDataset.class, rendererIndex);
    91 						plot = new XYPlot((OHLCDataset) data, null, null, (XYItemRenderer) rend);
    92 						break;
    93 					//case SIGNAL :
    94 					//	check(data, SignalsDataset.class, rendererIndex);
    95 					//	plot = new XYPlot((SignalsDataset) data, null, null, (XYItemRenderer) rend);
    96 					default :
    97 						throw new AttributeValidationException(chartType + ".type", type);
    98 				}
    99 			} else if (chartType == ChartConstants.OVERLAY_CATEGORY) {
   100 				switch (rendererIndex) {
   101 					case AREA :
   102 					case VERTICAL_BAR :
   103 					case LINE :
   104 					case SHAPES_AND_LINES :
   105 						check(data, CategoryDataset.class, rendererIndex);
   106 						plot =
   107 							new CategoryPlot(
   108 								(CategoryDataset) data,
   109 								null,
   110 								null,
   111 								(CategoryItemRenderer) rend);
   112 						break;
   113 					default :
   114 						throw new AttributeValidationException(chartType + ".type", type);
   115 				}
   116 			}
   117 		}
   118 		plot.setDrawingSupplier(drawingSupplier);
   119 		return plot;
   120 	}
   121 
   122 	public Object getDataset() throws DatasetProduceException {
   123 		return dataAware.getDataset();
   124 	}
   125 
   126 	/**
   127 	 * Gets the y-axis label. [tb]
   128 	 *
   129 	 * @return the y-axis label.
   130 	 */
   131 	public String getXaxislabel() {
   132 		return xAxisLabel;
   133 	}
   134 
   135 	/**
   136 	 * Sets the x-axis label [tb]
   137 	 *
   138 	 * @return the x-axis label
   139 	 */
   140 	public String getYaxislabel() {
   141 		return yAxisLabel;
   142 	}
   143 
   144 	/**
   145 	 * Sets the x-axis label [tb]
   146 	 *
   147 	 * @param xAxisLabel New value of property xAxisLabel.
   148 	 */
   149 	public void setXaxislabel(String xAxisLabel) {
   150 		this.xAxisLabel = xAxisLabel;
   151 	}
   152 
   153 	/**
   154 	 * Sets the y-axis label [tb]
   155 	 *
   156 	 * @param yAxisLabel New value of property yAxisLabel.
   157 	 */
   158 	public void setYaxislabel(String yAxisLabel) {
   159 		this.yAxisLabel = yAxisLabel;
   160 	}
   161 	/**
   162 	 * Sets the type.
   163 	 * @param type The type to set
   164 	 */
   165 	public void setType(String type) {
   166 		this.type = type;
   167 	}
   168 	/**
   169 	 * Gets the type.
   170 	 * @return type of plot as a String
   171 	 */
   172 	public String getType() {
   173 		return this.type;
   174 	}
   175 
   176 	public void check(Dataset data, Class clazz, int plotType) throws IncompatibleDatasetException {
   177 		if (!clazz.isInstance(data)) {
   178 			throw new IncompatibleDatasetException(
   179 				"Plots of type " + PlotTypes.typeNames[plotType] + " need a dataset of type " + clazz.getName());
   180 		}
   181 	}
   182 
   183 	public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) {
   184 		log.debug("setDataProductionConfig(" + dsp + ", " + params);
   185 		dataAware.setDataProductionConfig(dsp, params, useCache);
   186 	}
   187 
   188 	/**
   189 	 * Sets the drawingSupplier.
   190 	 * @param drawingSupplier The drawingSupplier to set
   191 	 */
   192 	public void setDrawingSupplier(DrawingSupplier drawingSupplier) {
   193 		this.drawingSupplier = drawingSupplier;
   194 	}
   195 
   196 }