view flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 653:67c7020f4ed3

Refactored the chart creation in the ChartInfoGenerators. Now, every chart is created by the XYChartGenerator again. flys-artifacts/trunk@2048 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 03 Jun 2011 07:20:39 +0000
parents 8fa4c5c9cd1a
children bbc966c81809
line wrap: on
line source
package de.intevation.flys.exports;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

import java.io.IOException;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;

import de.intevation.flys.exports.ChartExportHelper;


/**
 * An abstract base class for creating XY charts.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public abstract class XYChartGenerator extends ChartGenerator {

    /** The logger that is used in this generator.*/
    private static Logger logger = Logger.getLogger(ChartGenerator.class);


    public static final Color DEFAULT_GRID_COLOR      = Color.GRAY;
    public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;


    /**
     * Returns the title of a chart.
     *
     * @return the title of a chart.
     */
    protected abstract String getChartTitle();

    /**
     * Returns the X-Axis label of a chart.
     *
     * @return the X-Axis label of a chart.
     */
    protected abstract String getXAxisLabel();

    /**
     * Returns the Y-Axis label of a chart.
     *
     * @return the Y-Axis label of a chart.
     */
    protected abstract String getYAxisLabel();

    /**
     * This method is called to add all datasets of a concrete XYChartGenerator
     * to the JFreeChart.
     *
     * @param chart The JFreeChart object.
     */
    protected abstract void addDatasets(JFreeChart chart);


    public void generate()
    throws IOException
    {
        logger.debug("XYChartGenerator.generate");

        JFreeChart chart = generateChart();

        int[] size = getSize();

        ChartExportHelper.exportImage(
            out,
            chart,
            "png",
            size[0], size[1]);
    }


    public JFreeChart generateChart() {
        logger.debug("XYChartGenerator.generateChart");

        JFreeChart chart = ChartFactory.createXYLineChart(
            getChartTitle(),
            getXAxisLabel(),
            getYAxisLabel(),
            null,
            PlotOrientation.VERTICAL,
            true,
            false,
            false);

        chart.setBackgroundPaint(Color.WHITE);
        chart.getPlot().setBackgroundPaint(Color.WHITE);

        XYPlot plot = (XYPlot) chart.getPlot();

        addDatasets(chart);
        addSubtitles(chart);
        adjustPlot(plot);
        adjustAxes(plot);
        zoom(plot);

        return chart;
    }


    /**
     * Zooms the chart to the ranges specified in the attribute document.
     *
     * @param plot The XYPlot.
     */
    protected void zoom(XYPlot plot) {
        logger.debug("Zoom to specified ranges.");
        zoomX(plot);
        zoomY(plot);
    }


    /**
     * Zooms the x axis to the range specified in the attribute document.
     *
     * @param plot The XYPlot.
     */
    protected void zoomX(XYPlot plot) {
        Range xrange = getDomainAxisRange();
        if (xrange != null) {
            ValueAxis xaxis = plot.getDomainAxis();
            xaxis.setRange(xrange);

            logger.debug("Zoom chart to X: " + xrange);
        }
    }


    /**
     * Zooms the y axis to the range specified in the attribute document.
     *
     * @param plot The XYPlot.
     */
    protected void zoomY(XYPlot plot) {
        Range yrange = getValueAxisRange();
        if (yrange != null) {
            ValueAxis yaxis = plot.getRangeAxis();
            yaxis.setRange(yrange);

            logger.debug("Zoom chart to Y: " + yrange);
        }
    }


    /**
     * Adjusts the axes of a plot.
     *
     * @param plot The XYPlot of the chart.
     */
    protected void adjustAxes(XYPlot plot) {
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();

        yAxis.setAutoRangeIncludesZero(false);
    }


    protected void adjustPlot(XYPlot plot) {
        Stroke gridStroke = new BasicStroke(
            DEFAULT_GRID_LINE_WIDTH,
            BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER,
            3.0f,
            new float[] { 3.0f },
            0.0f);

        plot.setDomainGridlineStroke(gridStroke);
        plot.setDomainGridlinePaint(DEFAULT_GRID_COLOR);
        plot.setDomainGridlinesVisible(true);

        plot.setRangeGridlineStroke(gridStroke);
        plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR);
        plot.setRangeGridlinesVisible(true);
    }


    protected void addSubtitles(JFreeChart chart) {
        // override this method in subclasses that need subtitles
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org