de.berlios.statcvs.xml.chart.AbstractChart.java Source code

Java tutorial

Introduction

Here is the source code for de.berlios.statcvs.xml.chart.AbstractChart.java

Source

/*
StatCvs - CVS statistics generation 
Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
http://statcvs.sf.net/
    
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.berlios.statcvs.xml.chart;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.Spacer;

import de.berlios.statcvs.xml.output.ReportSettings;
import de.berlios.statcvs.xml.util.StringHelper;

/**
 * AbstractChart
 * 
 * @author Tammo van Lessen
 */
public abstract class AbstractChart {

    private final static Logger logger = Logger.getLogger("net.sf.statcvs.output.xml.chart.Chart");

    private static int chartNumber = 0;

    private String filename;
    private String subtitle;
    private JFreeChart chart;

    private ReportSettings settings;

    public AbstractChart(ReportSettings settings, String defaultFilename, String defaultSubtitle) {
        this.settings = settings;

        setFilename(
                settings.getString("filename",
                        (defaultFilename == null) ? "chart" + ++chartNumber + ".png" : defaultFilename),
                settings.getFilenameId());
        setSubtitle(settings.getString("subtitle", defaultSubtitle), settings.getSubtitlePostfix());
    }

    /** 
     * get chart
     * @return chart the chart
     */
    public JFreeChart getChart() {
        return chart;
    }

    /** 
     * set chart
     * @param chart the chart
     */
    public void setChart(JFreeChart chart) {
        this.chart = chart;
    }

    /** 
     * get filename
     * @return filename file name
     */
    public String getFilename() {
        return filename;
    }

    /** 
     * set filename
     * @param filename file name
     */
    public void setFilename(String filename) {
        this.filename = StringHelper.escapeFilename(filename);
    }

    /**
     * @param string
     * @param string2
     */
    public void setFilename(String filename, String subst) {
        setFilename(filename.replaceAll("%1", subst));
    }

    public int getPreferredHeigth() {
        return 480;
    }

    public int getPreferredWidth() {
        return 640;
    }

    /**
     * create chart with titles and credit information
     */
    protected void setup(boolean defaultShowLegend) {
        if (chart == null) {
            throw new NullPointerException("Chart is not initialized");
        }

        if (this.subtitle != null && settings.getBoolean("showSubtitle", true)) {
            Font font = new Font("SansSerif", Font.PLAIN, 12);
            TextTitle title = new TextTitle(this.subtitle, font);
            title.setSpacer(new Spacer(Spacer.RELATIVE, 0.05, 0.05, 0.05, 0.0));
            chart.addSubtitle(title);
        }

        if (settings.getBoolean("showCredit", true)) {
            Font font = new Font("SansSerif", Font.PLAIN, 9);
            TextTitle copyright = new TextTitle("generated by statcvs-xml\u00B2", font);
            copyright.setPosition(RectangleEdge.BOTTOM);
            copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
            chart.addSubtitle(copyright);
        }

        if (!settings.getBoolean("showLegend", defaultShowLegend)) {
            chart.setLegend(null);
        }

        chart.setBackgroundPaint(Color.white);
    }

    public void save(File outputPath) throws IOException {
        int width = ("*".equals(settings.getString("width"))) ? getPreferredWidth()
                : settings.getInt("width", getPreferredWidth());
        int height = ("*".equals(settings.getString("heigth"))) ? getPreferredHeigth()
                : settings.getInt("height", getPreferredHeigth());

        save(new File(outputPath, filename), width, height);
    }

    public void save(File file, int width, int height) throws IOException {
        ChartUtilities.saveChartAsPNG(file, chart, width, height);
        logger.fine("saved chart as '" + filename + "'");
    }

    /**
     * @return
     */
    public String getSubtitle() {
        return subtitle;
    }

    /** 
     * set subtitle
     * @param subtitle file name
     */
    public void setSubtitle(String subtitle) {
        this.subtitle = subtitle;
    }

    /**
     * @param string
     * @param string2
     */
    public void setSubtitle(String subtitle, String subst) {
        setSubtitle(subtitle.replaceAll("%1", subst));
    }

    /**
     * @deprecated
     */
    public void setCategoryAxisLabel(String text) {
        CategoryPlot plot = getChart().getCategoryPlot();
        CategoryAxis axis = (CategoryAxis) plot.getDomainAxis();
        axis.setLabel(text);
    }

    /**
     * @deprecated
     */
    public void setValueAxisLabel(String text) {
        CategoryPlot plot = getChart().getCategoryPlot();
        ValueAxis axis = plot.getRangeAxis();
        axis.setLabel(text);
    }

    /**
     * @return Returns the settings.
     */
    public ReportSettings getSettings() {
        return settings;
    }

}