java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/AbstractChartDefinition.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/AbstractChartDefinition.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,274 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of
    1.16 + * the License, or (at your option) any later version.
    1.17 + *
    1.18 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.19 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.20 + * See the GNU Lesser General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.23 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.24 + * Boston, MA 02111-1307, USA.
    1.25 + */
    1.26 +
    1.27 +package de.laures.cewolf.taglib;
    1.28 +
    1.29 +import java.awt.Image;
    1.30 +import java.awt.Paint;
    1.31 +import java.io.Serializable;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Iterator;
    1.34 +import java.util.List;
    1.35 +import java.util.Map;
    1.36 +
    1.37 +import org.apache.commons.logging.Log;
    1.38 +import org.apache.commons.logging.LogFactory;
    1.39 +import org.jfree.chart.JFreeChart;
    1.40 +import org.jfree.chart.title.LegendTitle;
    1.41 +import org.jfree.ui.RectangleEdge;
    1.42 +
    1.43 +import de.laures.cewolf.ChartHolder;
    1.44 +import de.laures.cewolf.ChartPostProcessor;
    1.45 +import de.laures.cewolf.ChartValidationException;
    1.46 +import de.laures.cewolf.DatasetProduceException;
    1.47 +import de.laures.cewolf.PostProcessingException;
    1.48 +import de.laures.cewolf.event.ChartImageRenderListener;
    1.49 +import de.laures.cewolf.util.ImageHelper;
    1.50 +import de.laures.cewolf.util.RenderedImage;
    1.51 +
    1.52 +/**
    1.53 + * Serializable implementaton of a ChartDefinition.
    1.54 + * @author glaures
    1.55 + * @see de.laures.cewolf.ChartHolder
    1.56 + */
    1.57 +public abstract class AbstractChartDefinition implements ChartHolder, Serializable, TaglibConstants, ChartImageRenderListener {
    1.58 +    
    1.59 +    protected static Log log = LogFactory.getLog(AbstractChartDefinition.class);
    1.60 +
    1.61 +    protected String title;
    1.62 +	protected String xAxisLabel;
    1.63 +	protected String yAxisLabel;
    1.64 +	protected String type;
    1.65 +
    1.66 +    private boolean antialias = true;
    1.67 +    private String background;
    1.68 +    private float backgroundImageAlpha = 1.0f;
    1.69 +    private Paint paint;
    1.70 +
    1.71 +    private int legendAnchor = ANCHOR_SOUTH;
    1.72 +    private boolean showLegend = true;
    1.73 +
    1.74 +    private transient List postProcessors = new ArrayList();
    1.75 +    private List postProcessorsParams = new ArrayList();
    1.76 +
    1.77 +    private transient JFreeChart chart;
    1.78 +	
    1.79 +	protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException;
    1.80 +	
    1.81 +  
    1.82 +    //gets first legend in the list
    1.83 +    public LegendTitle getLegend()
    1.84 +    {
    1.85 +      //i need to find the legend now.
    1.86 +      LegendTitle legend = null;
    1.87 +      List subTitles = chart.getSubtitles();
    1.88 +      Iterator iter = subTitles.iterator();
    1.89 +      while (iter.hasNext())
    1.90 +      {
    1.91 +        Object o = iter.next();
    1.92 +        if (o instanceof LegendTitle)
    1.93 +        {
    1.94 +          legend = (LegendTitle) o;
    1.95 +          break;
    1.96 +        }
    1.97 +      }
    1.98 +      return legend;
    1.99 +    }
   1.100 +    
   1.101 +    //removes first legend in the list
   1.102 +    public void removeLegend()
   1.103 +    {
   1.104 +      List subTitles = chart.getSubtitles();
   1.105 +      Iterator iter = subTitles.iterator();
   1.106 +      while (iter.hasNext())
   1.107 +      {
   1.108 +        Object o = iter.next();
   1.109 +        if (o instanceof LegendTitle)
   1.110 +        {
   1.111 +          iter.remove();
   1.112 +          break;
   1.113 +        }
   1.114 +      }
   1.115 +    }
   1.116 +  
   1.117 +    /**
   1.118 +     * This method triggers the dataset and chart production. It is only
   1.119 +     * from outside if there is no cached image available in the the
   1.120 +     * image cache.
   1.121 +     */
   1.122 +    public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
   1.123 +        if (chart == null) {
   1.124 +            chart = produceChart();
   1.125 +            chart.setAntiAlias(antialias);
   1.126 +            if (background != null) {
   1.127 +                Image image = ImageHelper.loadImage(background);
   1.128 +                chart.setBackgroundImage(image);
   1.129 +                chart.setBackgroundImageAlpha(backgroundImageAlpha);
   1.130 +            }
   1.131 +            if (paint != null) {
   1.132 +                chart.setBackgroundPaint(paint);
   1.133 +            }
   1.134 +            if (showLegend) 
   1.135 +            {
   1.136 +
   1.137 +                LegendTitle legend = this.getLegend();
   1.138 +                switch (legendAnchor) 
   1.139 +                {
   1.140 +                    case ANCHOR_NORTH :
   1.141 +                        legend.setPosition(RectangleEdge.TOP);
   1.142 +                        break;
   1.143 +                    case ANCHOR_WEST :
   1.144 +                      legend.setPosition(RectangleEdge.RIGHT);
   1.145 +                        break;
   1.146 +                    case ANCHOR_EAST :
   1.147 +                      legend.setPosition(RectangleEdge.LEFT);
   1.148 +                        break;
   1.149 +                    default :
   1.150 +                      legend.setPosition(RectangleEdge.BOTTOM);
   1.151 +                }
   1.152 +            } 
   1.153 +            else 
   1.154 +            {
   1.155 +              this.removeLegend();
   1.156 +            }
   1.157 +            // postProcessing
   1.158 +            for (int i = 0; i < postProcessors.size(); i++) {
   1.159 +                ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
   1.160 +                try {
   1.161 +                    pp.processChart(chart, (Map)postProcessorsParams.get(i));
   1.162 +                } catch (Throwable t) {
   1.163 +                	log.error(t);
   1.164 +                    throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" +
   1.165 +                    		pp + "'.\nPost processing of this post processor " + "has been ignored.");
   1.166 +                }
   1.167 +            }
   1.168 +        }
   1.169 +        return chart;
   1.170 +    }
   1.171 +
   1.172 +    /**
   1.173 +     * Sets the antialias.
   1.174 +     * @param antialias The antialias to set
   1.175 +     */
   1.176 +    public void setAntialias(boolean antialias) {
   1.177 +        this.antialias = antialias;
   1.178 +    }
   1.179 +
   1.180 +    /**
   1.181 +     * Sets the background.
   1.182 +     * @param background The background to set
   1.183 +     */
   1.184 +    public void setBackground(String background) {
   1.185 +        this.background = background;
   1.186 +    }
   1.187 +
   1.188 +    /**
   1.189 +     * Sets the backgroundImageAlpha.
   1.190 +     * @param backgroundImageAlpha The backgroundImageAlpha to set
   1.191 +     */
   1.192 +    public void setBackgroundImageAlpha(float backgroundImageAlpha) {
   1.193 +        this.backgroundImageAlpha = backgroundImageAlpha;
   1.194 +    }
   1.195 +
   1.196 +    /**
   1.197 +     * Sets the legendAnchor.
   1.198 +     * @param legendAnchor The legendAnchor to set
   1.199 +     */
   1.200 +    public void setLegendAnchor(int legendAnchor) {
   1.201 +        this.legendAnchor = legendAnchor;
   1.202 +    }
   1.203 +
   1.204 +    /**
   1.205 +     * Sets the paint.
   1.206 +     * @param paint The paint to set
   1.207 +     */
   1.208 +    public void setPaint(Paint paint) {
   1.209 +        this.paint = paint;
   1.210 +    }
   1.211 +
   1.212 +    /**
   1.213 +     * Sets the showLegend.
   1.214 +     * @param showLegend The showLegend to set
   1.215 +     */
   1.216 +    public void setShowLegend(boolean showLegend) {
   1.217 +        this.showLegend = showLegend;
   1.218 +    }
   1.219 +
   1.220 +    /**
   1.221 +     * Sets the title.
   1.222 +     * @param title The title to set
   1.223 +     */
   1.224 +    public void setTitle(String title) {
   1.225 +        this.title = title;
   1.226 +    }
   1.227 +
   1.228 +    /**
   1.229 +     * Sets the type.
   1.230 +     * @param type The type to set
   1.231 +     */
   1.232 +    public void setType(String type) {
   1.233 +        this.type = type;
   1.234 +    }
   1.235 +
   1.236 +    /**
   1.237 +     * Sets the xAxisLabel.
   1.238 +     * @param xAxisLabel The xAxisLabel to set
   1.239 +     */
   1.240 +    public void setXAxisLabel(String xAxisLabel) {
   1.241 +        this.xAxisLabel = xAxisLabel;
   1.242 +    }
   1.243 +
   1.244 +    /**
   1.245 +     * Sets the yAxisLabel.
   1.246 +     * @param yAxisLabel The yAxisLabel to set
   1.247 +     */
   1.248 +    public void setYAxisLabel(String yAxisLabel) {
   1.249 +        this.yAxisLabel = yAxisLabel;
   1.250 +    }
   1.251 +
   1.252 +    public void addPostProcessor(ChartPostProcessor pp) {
   1.253 +        postProcessors.add(pp);
   1.254 +    }
   1.255 +
   1.256 +    public void addPostProcessorParams(Map params) {
   1.257 +        postProcessorsParams.add(params);
   1.258 +    }
   1.259 +    
   1.260 +	/**
   1.261 +	 * Callback right after a new image gets rendered.
   1.262 +	 * Implemented, so if postprocessors implement the ImageRenderListener interface
   1.263 +	 * then they will be called back also
   1.264 +	 * 
   1.265 +	 * @param renderedImage The fresh image just got rendered
   1.266 +	 */
   1.267 +	public void onImageRendered (RenderedImage renderedImage) {
   1.268 +		// if the postprocessor implements ImageRenderListener interface call it!
   1.269 +        for (int i = 0; i < postProcessors.size(); i++) {
   1.270 +            ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
   1.271 +            if (pp instanceof ChartImageRenderListener) {
   1.272 +            	((ChartImageRenderListener) pp).onImageRendered(renderedImage);
   1.273 +            }
   1.274 +        }		
   1.275 +	}
   1.276 +
   1.277 +}