⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractchartdefinition.java

📁 cewolf是个一个自动报表程序
💻 JAVA
字号:
/* ================================================================
 * Cewolf : Chart enabling Web Objects Framework
 * ================================================================
 *
 * Project Info:  http://cewolf.sourceforge.net
 * Project Lead:  Guido Laures (guido@laures.de);
 *
 * (C) Copyright 2002, by Guido Laures
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package de.laures.cewolf.taglib;

import java.awt.Image;
import java.awt.Paint;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.Legend;

import de.laures.cewolf.ChartHolder;
import de.laures.cewolf.ChartPostProcessor;
import de.laures.cewolf.ChartValidationException;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.PostProcessingException;
import de.laures.cewolf.event.ChartImageRenderListener;
import de.laures.cewolf.util.ImageHelper;
import de.laures.cewolf.util.RenderedImage;

/**
 * Serializable implementaton of a ChartDefinition.
 * @author glaures
 * @see de.laures.cewolf.ChartHolder
 */
public abstract class AbstractChartDefinition implements ChartHolder, Serializable, TaglibConstants, ChartImageRenderListener {
    
    protected static Log log = LogFactory.getLog(AbstractChartDefinition.class);

    protected String title;
	protected String xAxisLabel;
	protected String yAxisLabel;
	protected String type;

    private boolean antialias = true;
    private String background;
    private float backgroundImageAlpha = 1.0f;
    private Paint paint;

    private int legendAnchor = ANCHOR_SOUTH;
    private boolean showLegend = true;

    private transient List postProcessors = new ArrayList();
    private List postProcessorsParams = new ArrayList();

    private transient JFreeChart chart;
	
	protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException;
	
    /**
     * This method triggers the dataset and chart production. It is only
     * from outside if there is no cached image available in the the
     * image cache.
     */
    public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
        if (chart == null) {
            chart = produceChart();
            chart.setAntiAlias(antialias);
            if (background != null) {
                Image image = ImageHelper.loadImage(background);
                chart.setBackgroundImage(image);
                chart.setBackgroundImageAlpha(backgroundImageAlpha);
            }
            if (paint != null) {
                chart.setBackgroundPaint(paint);
            }
            if (showLegend) {
                Legend legend = chart.getLegend();
                switch (legendAnchor) {
                    case ANCHOR_NORTH :
                        legend.setAnchor(Legend.NORTH);
                        break;
                    case ANCHOR_WEST :
                        legend.setAnchor(Legend.WEST);
                        break;
                    case ANCHOR_EAST :
                        legend.setAnchor(Legend.EAST);
                        break;
                    default :
                        legend.setAnchor(Legend.SOUTH);
                }
            } else {
                chart.setLegend(null);
            }
            // postProcessing
            for (int i = 0; i < postProcessors.size(); i++) {
                ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
                try {
                    pp.processChart(chart, (Map)postProcessorsParams.get(i));
                } catch (Throwable t) {
                	log.error(t);
                    throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" +
                    		pp + "'.\nPost processing of this post processor " + "has been ignored.");
                }
            }
        }
        return chart;
    }

    /**
     * Sets the antialias.
     * @param antialias The antialias to set
     */
    public void setAntialias(boolean antialias) {
        this.antialias = antialias;
    }

    /**
     * Sets the background.
     * @param background The background to set
     */
    public void setBackground(String background) {
        this.background = background;
    }

    /**
     * Sets the backgroundImageAlpha.
     * @param backgroundImageAlpha The backgroundImageAlpha to set
     */
    public void setBackgroundImageAlpha(float backgroundImageAlpha) {
        this.backgroundImageAlpha = backgroundImageAlpha;
    }

    /**
     * Sets the legendAnchor.
     * @param legendAnchor The legendAnchor to set
     */
    public void setLegendAnchor(int legendAnchor) {
        this.legendAnchor = legendAnchor;
    }

    /**
     * Sets the paint.
     * @param paint The paint to set
     */
    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    /**
     * Sets the showLegend.
     * @param showLegend The showLegend to set
     */
    public void setShowLegend(boolean showLegend) {
        this.showLegend = showLegend;
    }

    /**
     * Sets the title.
     * @param title The title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Sets the type.
     * @param type The type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * Sets the xAxisLabel.
     * @param xAxisLabel The xAxisLabel to set
     */
    public void setXAxisLabel(String xAxisLabel) {
        this.xAxisLabel = xAxisLabel;
    }

    /**
     * Sets the yAxisLabel.
     * @param yAxisLabel The yAxisLabel to set
     */
    public void setYAxisLabel(String yAxisLabel) {
        this.yAxisLabel = yAxisLabel;
    }

    public void addPostProcessor(ChartPostProcessor pp) {
        postProcessors.add(pp);
    }

    public void addPostProcessorParams(Map params) {
        postProcessorsParams.add(params);
    }
    
	/**
	 * Callback right after a new image gets rendered.
	 * Implemented, so if postprocessors implement the ImageRenderListener interface
	 * then they will be called back also
	 * 
	 * @param renderedImage The fresh image just got rendered
	 */
	public void onImageRendered (RenderedImage renderedImage) {
		// if the postprocessor implements ImageRenderListener interface call it!
        for (int i = 0; i < postProcessors.size(); i++) {
            ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
            if (pp instanceof ChartImageRenderListener) {
            	((ChartImageRenderListener) pp).onImageRendered(renderedImage);
            }
        }		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -