franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of franta-hg@1: * the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.taglib; franta-hg@1: franta-hg@1: import java.awt.Image; franta-hg@1: import java.awt.Paint; franta-hg@1: import java.io.Serializable; franta-hg@1: import java.util.ArrayList; franta-hg@1: import java.util.Iterator; franta-hg@1: import java.util.List; franta-hg@1: import java.util.Map; franta-hg@1: franta-hg@1: import org.apache.commons.logging.Log; franta-hg@1: import org.apache.commons.logging.LogFactory; franta-hg@1: import org.jfree.chart.JFreeChart; franta-hg@1: import org.jfree.chart.title.LegendTitle; franta-hg@1: import org.jfree.ui.RectangleEdge; franta-hg@1: franta-hg@1: import de.laures.cewolf.ChartHolder; franta-hg@1: import de.laures.cewolf.ChartPostProcessor; franta-hg@1: import de.laures.cewolf.ChartValidationException; franta-hg@1: import de.laures.cewolf.DatasetProduceException; franta-hg@1: import de.laures.cewolf.PostProcessingException; franta-hg@1: import de.laures.cewolf.event.ChartImageRenderListener; franta-hg@1: import de.laures.cewolf.util.ImageHelper; franta-hg@1: import de.laures.cewolf.util.RenderedImage; franta-hg@1: franta-hg@1: /** franta-hg@1: * Serializable implementaton of a ChartDefinition. franta-hg@1: * @author glaures franta-hg@1: * @see de.laures.cewolf.ChartHolder franta-hg@1: */ franta-hg@1: public abstract class AbstractChartDefinition implements ChartHolder, Serializable, TaglibConstants, ChartImageRenderListener { franta-hg@1: franta-hg@1: protected static Log log = LogFactory.getLog(AbstractChartDefinition.class); franta-hg@1: franta-hg@1: protected String title; franta-hg@1: protected String xAxisLabel; franta-hg@1: protected String yAxisLabel; franta-hg@1: protected String type; franta-hg@1: franta-hg@1: private boolean antialias = true; franta-hg@1: private String background; franta-hg@1: private float backgroundImageAlpha = 1.0f; franta-hg@1: private Paint paint; franta-hg@1: franta-hg@1: private int legendAnchor = ANCHOR_SOUTH; franta-hg@1: private boolean showLegend = true; franta-hg@1: franta-hg@1: private transient List postProcessors = new ArrayList(); franta-hg@1: private List postProcessorsParams = new ArrayList(); franta-hg@1: franta-hg@1: private transient JFreeChart chart; franta-hg@1: franta-hg@1: protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException; franta-hg@1: franta-hg@1: franta-hg@1: //gets first legend in the list franta-hg@1: public LegendTitle getLegend() franta-hg@1: { franta-hg@1: //i need to find the legend now. franta-hg@1: LegendTitle legend = null; franta-hg@1: List subTitles = chart.getSubtitles(); franta-hg@1: Iterator iter = subTitles.iterator(); franta-hg@1: while (iter.hasNext()) franta-hg@1: { franta-hg@1: Object o = iter.next(); franta-hg@1: if (o instanceof LegendTitle) franta-hg@1: { franta-hg@1: legend = (LegendTitle) o; franta-hg@1: break; franta-hg@1: } franta-hg@1: } franta-hg@1: return legend; franta-hg@1: } franta-hg@1: franta-hg@1: //removes first legend in the list franta-hg@1: public void removeLegend() franta-hg@1: { franta-hg@1: List subTitles = chart.getSubtitles(); franta-hg@1: Iterator iter = subTitles.iterator(); franta-hg@1: while (iter.hasNext()) franta-hg@1: { franta-hg@1: Object o = iter.next(); franta-hg@1: if (o instanceof LegendTitle) franta-hg@1: { franta-hg@1: iter.remove(); franta-hg@1: break; franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * This method triggers the dataset and chart production. It is only franta-hg@1: * from outside if there is no cached image available in the the franta-hg@1: * image cache. franta-hg@1: */ franta-hg@1: public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException { franta-hg@1: if (chart == null) { franta-hg@1: chart = produceChart(); franta-hg@1: chart.setAntiAlias(antialias); franta-hg@1: if (background != null) { franta-hg@1: Image image = ImageHelper.loadImage(background); franta-hg@1: chart.setBackgroundImage(image); franta-hg@1: chart.setBackgroundImageAlpha(backgroundImageAlpha); franta-hg@1: } franta-hg@1: if (paint != null) { franta-hg@1: chart.setBackgroundPaint(paint); franta-hg@1: } franta-hg@1: if (showLegend) franta-hg@1: { franta-hg@1: franta-hg@1: LegendTitle legend = this.getLegend(); franta-hg@1: switch (legendAnchor) franta-hg@1: { franta-hg@1: case ANCHOR_NORTH : franta-hg@1: legend.setPosition(RectangleEdge.TOP); franta-hg@1: break; franta-hg@1: case ANCHOR_WEST : franta-hg@1: legend.setPosition(RectangleEdge.RIGHT); franta-hg@1: break; franta-hg@1: case ANCHOR_EAST : franta-hg@1: legend.setPosition(RectangleEdge.LEFT); franta-hg@1: break; franta-hg@1: default : franta-hg@1: legend.setPosition(RectangleEdge.BOTTOM); franta-hg@1: } franta-hg@1: } franta-hg@1: else franta-hg@1: { franta-hg@1: this.removeLegend(); franta-hg@1: } franta-hg@1: // postProcessing franta-hg@1: for (int i = 0; i < postProcessors.size(); i++) { franta-hg@1: ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i); franta-hg@1: try { franta-hg@1: pp.processChart(chart, (Map)postProcessorsParams.get(i)); franta-hg@1: } catch (Throwable t) { franta-hg@1: log.error(t); franta-hg@1: throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" + franta-hg@1: pp + "'.\nPost processing of this post processor " + "has been ignored."); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: return chart; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the antialias. franta-hg@1: * @param antialias The antialias to set franta-hg@1: */ franta-hg@1: public void setAntialias(boolean antialias) { franta-hg@1: this.antialias = antialias; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the background. franta-hg@1: * @param background The background to set franta-hg@1: */ franta-hg@1: public void setBackground(String background) { franta-hg@1: this.background = background; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the backgroundImageAlpha. franta-hg@1: * @param backgroundImageAlpha The backgroundImageAlpha to set franta-hg@1: */ franta-hg@1: public void setBackgroundImageAlpha(float backgroundImageAlpha) { franta-hg@1: this.backgroundImageAlpha = backgroundImageAlpha; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the legendAnchor. franta-hg@1: * @param legendAnchor The legendAnchor to set franta-hg@1: */ franta-hg@1: public void setLegendAnchor(int legendAnchor) { franta-hg@1: this.legendAnchor = legendAnchor; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the paint. franta-hg@1: * @param paint The paint to set franta-hg@1: */ franta-hg@1: public void setPaint(Paint paint) { franta-hg@1: this.paint = paint; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the showLegend. franta-hg@1: * @param showLegend The showLegend to set franta-hg@1: */ franta-hg@1: public void setShowLegend(boolean showLegend) { franta-hg@1: this.showLegend = showLegend; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the title. franta-hg@1: * @param title The title to set franta-hg@1: */ franta-hg@1: public void setTitle(String title) { franta-hg@1: this.title = title; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the type. franta-hg@1: * @param type The type to set franta-hg@1: */ franta-hg@1: public void setType(String type) { franta-hg@1: this.type = type; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the xAxisLabel. franta-hg@1: * @param xAxisLabel The xAxisLabel to set franta-hg@1: */ franta-hg@1: public void setXAxisLabel(String xAxisLabel) { franta-hg@1: this.xAxisLabel = xAxisLabel; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the yAxisLabel. franta-hg@1: * @param yAxisLabel The yAxisLabel to set franta-hg@1: */ franta-hg@1: public void setYAxisLabel(String yAxisLabel) { franta-hg@1: this.yAxisLabel = yAxisLabel; franta-hg@1: } franta-hg@1: franta-hg@1: public void addPostProcessor(ChartPostProcessor pp) { franta-hg@1: postProcessors.add(pp); franta-hg@1: } franta-hg@1: franta-hg@1: public void addPostProcessorParams(Map params) { franta-hg@1: postProcessorsParams.add(params); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Callback right after a new image gets rendered. franta-hg@1: * Implemented, so if postprocessors implement the ImageRenderListener interface franta-hg@1: * then they will be called back also franta-hg@1: * franta-hg@1: * @param renderedImage The fresh image just got rendered franta-hg@1: */ franta-hg@1: public void onImageRendered (RenderedImage renderedImage) { franta-hg@1: // if the postprocessor implements ImageRenderListener interface call it! franta-hg@1: for (int i = 0; i < postProcessors.size(); i++) { franta-hg@1: ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i); franta-hg@1: if (pp instanceof ChartImageRenderListener) { franta-hg@1: ((ChartImageRenderListener) pp).onImageRendered(renderedImage); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: }