1 23 24 package de.laures.cewolf.taglib; 25 26 import java.awt.Image ; 27 import java.awt.Paint ; 28 import java.io.Serializable ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.jfree.chart.JFreeChart; 37 import org.jfree.chart.title.LegendTitle; 38 import org.jfree.ui.RectangleEdge; 39 40 import de.laures.cewolf.ChartHolder; 41 import de.laures.cewolf.ChartPostProcessor; 42 import de.laures.cewolf.ChartValidationException; 43 import de.laures.cewolf.DatasetProduceException; 44 import de.laures.cewolf.PostProcessingException; 45 import de.laures.cewolf.event.ChartImageRenderListener; 46 import de.laures.cewolf.util.ImageHelper; 47 import de.laures.cewolf.util.RenderedImage; 48 49 54 public abstract class AbstractChartDefinition implements ChartHolder, Serializable , TaglibConstants, ChartImageRenderListener { 55 56 protected static Log log = LogFactory.getLog(AbstractChartDefinition.class); 57 58 protected String title; 59 protected String xAxisLabel; 60 protected String yAxisLabel; 61 protected String type; 62 63 private boolean antialias = true; 64 private String background; 65 private float backgroundImageAlpha = 1.0f; 66 private Paint paint; 67 68 private int legendAnchor = ANCHOR_SOUTH; 69 private boolean showLegend = true; 70 71 private transient List postProcessors = new ArrayList (); 72 private List postProcessorsParams = new ArrayList (); 73 74 private transient JFreeChart chart; 75 76 protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException; 77 78 79 public LegendTitle getLegend() 81 { 82 LegendTitle legend = null; 84 List subTitles = chart.getSubtitles(); 85 Iterator iter = subTitles.iterator(); 86 while (iter.hasNext()) 87 { 88 Object o = iter.next(); 89 if (o instanceof LegendTitle) 90 { 91 legend = (LegendTitle) o; 92 break; 93 } 94 } 95 return legend; 96 } 97 98 public void removeLegend() 100 { 101 List subTitles = chart.getSubtitles(); 102 Iterator iter = subTitles.iterator(); 103 while (iter.hasNext()) 104 { 105 Object o = iter.next(); 106 if (o instanceof LegendTitle) 107 { 108 iter.remove(); 109 break; 110 } 111 } 112 } 113 114 119 public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException { 120 if (chart == null) { 121 chart = produceChart(); 122 chart.setAntiAlias(antialias); 123 if (background != null) { 124 Image image = ImageHelper.loadImage(background); 125 chart.setBackgroundImage(image); 126 chart.setBackgroundImageAlpha(backgroundImageAlpha); 127 } 128 if (paint != null) { 129 chart.setBackgroundPaint(paint); 130 } 131 if (showLegend) 132 { 133 134 LegendTitle legend = this.getLegend(); 135 switch (legendAnchor) 136 { 137 case ANCHOR_NORTH : 138 legend.setPosition(RectangleEdge.TOP); 139 break; 140 case ANCHOR_WEST : 141 legend.setPosition(RectangleEdge.RIGHT); 142 break; 143 case ANCHOR_EAST : 144 legend.setPosition(RectangleEdge.LEFT); 145 break; 146 default : 147 legend.setPosition(RectangleEdge.BOTTOM); 148 } 149 } 150 else 151 { 152 this.removeLegend(); 153 } 154 for (int i = 0; i < postProcessors.size(); i++) { 156 ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i); 157 try { 158 pp.processChart(chart, (Map )postProcessorsParams.get(i)); 159 } catch (Throwable t) { 160 log.error(t); 161 throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" + 162 pp + "'.\nPost processing of this post processor " + "has been ignored."); 163 } 164 } 165 } 166 return chart; 167 } 168 169 173 public void setAntialias(boolean antialias) { 174 this.antialias = antialias; 175 } 176 177 181 public void setBackground(String background) { 182 this.background = background; 183 } 184 185 189 public void setBackgroundImageAlpha(float backgroundImageAlpha) { 190 this.backgroundImageAlpha = backgroundImageAlpha; 191 } 192 193 197 public void setLegendAnchor(int legendAnchor) { 198 this.legendAnchor = legendAnchor; 199 } 200 201 205 public void setPaint(Paint paint) { 206 this.paint = paint; 207 } 208 209 213 public void setShowLegend(boolean showLegend) { 214 this.showLegend = showLegend; 215 } 216 217 221 public void setTitle(String title) { 222 this.title = title; 223 } 224 225 229 public void setType(String type) { 230 this.type = type; 231 } 232 233 237 public void setXAxisLabel(String xAxisLabel) { 238 this.xAxisLabel = xAxisLabel; 239 } 240 241 245 public void setYAxisLabel(String yAxisLabel) { 246 this.yAxisLabel = yAxisLabel; 247 } 248 249 public void addPostProcessor(ChartPostProcessor pp) { 250 postProcessors.add(pp); 251 } 252 253 public void addPostProcessorParams(Map params) { 254 postProcessorsParams.add(params); 255 } 256 257 264 public void onImageRendered (RenderedImage renderedImage) { 265 for (int i = 0; i < postProcessors.size(); i++) { 267 ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i); 268 if (pp instanceof ChartImageRenderListener) { 269 ((ChartImageRenderListener) pp).onImageRendered(renderedImage); 270 } 271 } 272 } 273 274 } 275 | Popular Tags |