view gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractHistogram.java @ 1129:ccfa07b88476

merged geo-backend
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:01 +0200
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.chart;

import java.util.Locale;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartTheme;
import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnitSource;

import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYBarRenderer;

/**
 * This abstract class defines some methods to adjust chart settings after its
 * creation.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public abstract class AbstractHistogram
implements            Chart
{
    /**
     * Logger used for logging with Apache log4j.
     */
    private Logger logger = Logger.getLogger(AbstractHistogram.class);

    /**
     * JFreeChart chart stored at this place after chart creation.
     */
    protected JFreeChart  chart;

    /**
     * Labels used for chart title, subtitle, axis description.
     */
    protected ChartLabels labels;

    /**
     * Theme which is used to adjust the styling of this chart.
     */
    protected ChartTheme  theme;

    /**
     * Raw data which should be displayed in the chart.
     */
    protected Object[]    data;

    /**
     * Locale object used for i18n support.
     */
    protected Locale     locale;


    /**
     * Constructor for creating histogram charts.
     *
     * @param labels See {@link #labels}
     * @param data See {@link #data}
     * @param theme See {@link #theme}
     */
    public AbstractHistogram(
        ChartLabels labels, Object[] data, ChartTheme theme
    ) {
        this.labels = labels;
        this.data   = data;
        this.theme  = theme;
    }


    /**
     * @see de.intevation.gnv.chart.Chart#generateChart()
     */
    public JFreeChart generateChart() {

        if (chart != null)
            return chart;

        chart = ChartFactory.createHistogram(
            labels.getTitle(),
            labels.getDomainAxisLabel(),
            labels.getRangeAxisLabel(),
            null,
            PlotOrientation.VERTICAL,
            true,
            false,
            false);

        applyDatasets();

        theme.apply(chart);
        adjustPlot();
        adjustDomainAxis(chart);
        adjustRangeAxis(chart);

        return chart;
    }


    /**
     * Method to do some changes in plot settings.
     */
    protected void adjustPlot() {
        XYPlot plot = (XYPlot) chart.getPlot();
        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();

        renderer.setShadowVisible(false);
        renderer.setSeriesVisibleInLegend(0, false);
    }


    protected void adjustDomainAxis(JFreeChart chart) {
        XYPlot plot           = (XYPlot) chart.getPlot();
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();

        TickUnitSource tus    = domainAxis.createStandardTickUnits(locale);
        domainAxis.setStandardTickUnits(tus);
    }


    protected void adjustRangeAxis(JFreeChart chart) {
        XYPlot plot           = (XYPlot) chart.getPlot();
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

        TickUnitSource tus    = rangeAxis.createStandardTickUnits(locale);
        rangeAxis.setStandardTickUnits(tus);
    }




    /**
     * This method needs to be implemented by subclasses and should add valid
     * <code>HistogramDataset</code> objects to the created chart. It is called
     * by {@link #generateChart} after chart creation.
     */
    protected abstract void applyDatasets();
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org