1 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 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; private String yAxisLabel; 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 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 131 public String getXaxislabel() { 132 return xAxisLabel; 133 } 134 135 140 public String getYaxislabel() { 141 return yAxisLabel; 142 } 143 144 149 public void setXaxislabel(String xAxisLabel) { 150 this.xAxisLabel = xAxisLabel; 151 } 152 153 158 public void setYaxislabel(String yAxisLabel) { 159 this.yAxisLabel = yAxisLabel; 160 } 161 165 public void setType(String type) { 166 this.type = type; 167 } 168 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 192 public void setDrawingSupplier(DrawingSupplier drawingSupplier) { 193 this.drawingSupplier = drawingSupplier; 194 } 195 196 } 197 | Popular Tags |