java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/AbstractChartDefinition.java
1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
8 * (C) Copyright 2002, by Guido Laures
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.
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.
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.
24 package de.laures.cewolf.taglib;
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;
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;
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;
50 * Serializable implementaton of a ChartDefinition.
52 * @see de.laures.cewolf.ChartHolder
54 public abstract class AbstractChartDefinition implements ChartHolder, Serializable, TaglibConstants, ChartImageRenderListener {
56 protected static Log log = LogFactory.getLog(AbstractChartDefinition.class);
58 protected String title;
59 protected String xAxisLabel;
60 protected String yAxisLabel;
61 protected String type;
63 private boolean antialias = true;
64 private String background;
65 private float backgroundImageAlpha = 1.0f;
68 private int legendAnchor = ANCHOR_SOUTH;
69 private boolean showLegend = true;
71 private transient List postProcessors = new ArrayList();
72 private List postProcessorsParams = new ArrayList();
74 private transient JFreeChart chart;
76 protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException;
79 //gets first legend in the list
80 public LegendTitle getLegend()
82 //i need to find the legend now.
83 LegendTitle legend = null;
84 List subTitles = chart.getSubtitles();
85 Iterator iter = subTitles.iterator();
86 while (iter.hasNext())
88 Object o = iter.next();
89 if (o instanceof LegendTitle)
91 legend = (LegendTitle) o;
98 //removes first legend in the list
99 public void removeLegend()
101 List subTitles = chart.getSubtitles();
102 Iterator iter = subTitles.iterator();
103 while (iter.hasNext())
105 Object o = iter.next();
106 if (o instanceof LegendTitle)
115 * This method triggers the dataset and chart production. It is only
116 * from outside if there is no cached image available in the the
119 public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
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);
129 chart.setBackgroundPaint(paint);
134 LegendTitle legend = this.getLegend();
135 switch (legendAnchor)
138 legend.setPosition(RectangleEdge.TOP);
141 legend.setPosition(RectangleEdge.RIGHT);
144 legend.setPosition(RectangleEdge.LEFT);
147 legend.setPosition(RectangleEdge.BOTTOM);
155 for (int i = 0; i < postProcessors.size(); i++) {
156 ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
158 pp.processChart(chart, (Map)postProcessorsParams.get(i));
159 } catch (Throwable t) {
161 throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" +
162 pp + "'.\nPost processing of this post processor " + "has been ignored.");
170 * Sets the antialias.
171 * @param antialias The antialias to set
173 public void setAntialias(boolean antialias) {
174 this.antialias = antialias;
178 * Sets the background.
179 * @param background The background to set
181 public void setBackground(String background) {
182 this.background = background;
186 * Sets the backgroundImageAlpha.
187 * @param backgroundImageAlpha The backgroundImageAlpha to set
189 public void setBackgroundImageAlpha(float backgroundImageAlpha) {
190 this.backgroundImageAlpha = backgroundImageAlpha;
194 * Sets the legendAnchor.
195 * @param legendAnchor The legendAnchor to set
197 public void setLegendAnchor(int legendAnchor) {
198 this.legendAnchor = legendAnchor;
203 * @param paint The paint to set
205 public void setPaint(Paint paint) {
210 * Sets the showLegend.
211 * @param showLegend The showLegend to set
213 public void setShowLegend(boolean showLegend) {
214 this.showLegend = showLegend;
219 * @param title The title to set
221 public void setTitle(String title) {
227 * @param type The type to set
229 public void setType(String type) {
234 * Sets the xAxisLabel.
235 * @param xAxisLabel The xAxisLabel to set
237 public void setXAxisLabel(String xAxisLabel) {
238 this.xAxisLabel = xAxisLabel;
242 * Sets the yAxisLabel.
243 * @param yAxisLabel The yAxisLabel to set
245 public void setYAxisLabel(String yAxisLabel) {
246 this.yAxisLabel = yAxisLabel;
249 public void addPostProcessor(ChartPostProcessor pp) {
250 postProcessors.add(pp);
253 public void addPostProcessorParams(Map params) {
254 postProcessorsParams.add(params);
258 * Callback right after a new image gets rendered.
259 * Implemented, so if postprocessors implement the ImageRenderListener interface
260 * then they will be called back also
262 * @param renderedImage The fresh image just got rendered
264 public void onImageRendered (RenderedImage renderedImage) {
265 // if the postprocessor implements ImageRenderListener interface call it!
266 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);