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