view flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 1688:7eb9015489b0

Worked on flys/issue150 flys-artifacts/trunk@2908 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 07 Oct 2011 15:55:38 +0000
parents 19c4cf5163e8
children 6e935087f6a1
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 java.text.NumberFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.annotations.XYTextAnnotation;
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.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import org.jfree.ui.RectangleInsets;

import de.intevation.flys.exports.ChartExportHelper;
import de.intevation.flys.jfree.FLYSAnnotation;
import de.intevation.flys.utils.ThemeUtil;


/**
 * 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(XYChartGenerator.class);


    /** SeriesCollection used for the first axis. */
    protected XYSeriesCollection first;

    /** SeriesCollection used for the second axis. */
    protected XYSeriesCollection second;

    /** List of annotations to insert in plot. */
    protected List<FLYSAnnotation> annotations;

    /** The max X range to include all X values of all series for each axis. */
    protected Map<Integer, Range> xRanges;

    /** The max Y range to include all Y values of all series for each axis. */
    protected Map<Integer, Range> yRanges;

    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();


    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(plot);
        addAnnotations(plot);
        addSubtitles(chart);
        adjustPlot(plot);
        localizeAxes(plot);

        removeEmptyRangeAxes(plot);

        preparePointRanges(plot);
        autoZoom(plot);

        applyThemes(plot);
        adjustAxes(plot);

        return chart;
    }


    /**
     * Add first and second dataset to plot.
     * @param plot plot to add datasets to.
     */
    protected void addDatasets(XYPlot plot) {
        if (first != null) {
            logger.debug("Set the first axis dataset.");
            plot.setDataset(0, first);
        }
        if (second != null) {
            logger.debug("Set the second axis dataset.");
            plot.setDataset(1, second);
        }
    }


    public void addFirstAxisSeries(XYSeries series, boolean visible) {
        if (first == null) {
            first = new XYSeriesCollection();
        }

        if (series != null) {
            if (visible) {
                first.addSeries(series);
            }

            combineYRanges(new Range(series.getMinY(), series.getMaxY()), 0);
            combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
        }
    }


    public void addSecondAxisSeries(XYSeries series, boolean visible) {
        if (second == null) {
            second = new XYSeriesCollection();
        }

        if (series != null) {
            if (visible) {
                second.addSeries(series);
            }

            combineYRanges(new Range(series.getMinY(), series.getMaxY()), 1);
            combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
        }
    }


    private void combineXRanges(Range range, int index) {
        Integer key = new Integer(index);

        if (xRanges == null) {
            xRanges = new HashMap<Integer, Range>();
            xRanges.put(key, range);
            return;
        }

        Range newX = null;
        Range oldX = xRanges.get(key);

        if (oldX != null) {
            newX = Range.combine(oldX, range);
        }
        else {
            newX = range;
        }

        xRanges.put(key, newX);
    }


    private void combineYRanges(Range range, int index) {
        Integer key = new Integer(index);

        if (yRanges == null) {
            yRanges = new HashMap<Integer, Range>();
            yRanges.put(key, range);
            return;
        }

        Range newY = null;
        Range oldY = yRanges.get(key);

        if (oldY != null) {
            newY = Range.combine(oldY, range);
        }
        else {
            newY = range;
        }

        yRanges.put(key, newY);
    }


    public void addAnnotations(FLYSAnnotation annotation, boolean visible) {
        if (!visible) {
            return;
        }

        if (annotations == null) {
            annotations = new ArrayList<FLYSAnnotation>();
        }

        annotations.add(annotation);
    }


    private void removeEmptyRangeAxes(XYPlot plot) {
        if (first == null) {
            plot.setRangeAxis(0, null);
        }

        if (second == null) {
            plot.setRangeAxis(1, null);
        }
    }


    private void preparePointRanges(XYPlot plot) {
        for (int i = 0, num = plot.getDomainAxisCount(); i < num; i++) {
            Integer key = new Integer(i);

            Range r = xRanges.get(key);
            if (r != null && r.getLowerBound() == r.getUpperBound()) {
                xRanges.put(key, expandRange(r, 5));
            }
        }

        for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
            Integer key = new Integer(i);

            Range r = yRanges.get(key);
            if (r != null && r.getLowerBound() == r.getUpperBound()) {
                yRanges.put(key, expandRange(r, 5));
            }
        }
    }


    public static Range expandRange(Range range, double percent) {
        if (range == null) {
            return null;
        }

        double value  = range.getLowerBound();
        double expand = value / 100 * percent;

        return expand != 0
            ? new Range(value-expand, value+expand)
            : new Range(-0.01 * percent, 0.01 * percent);
    }


    /**
     * This method zooms the plot to the specified ranges in the attribute
     * document or to the ranges specified by the min/max values in the
     * datasets. <b>Note:</b> We determine the range manually if no zoom ranges
     * are given, because JFreeCharts auto-zoom adds a margin to the left and
     * right of the data area.
     *
     * @param plot The XYPlot.
     */
    protected void autoZoom(XYPlot plot) {
        logger.debug("Zoom to specified ranges.");

        Range xrange = getDomainAxisRange();
        Range yrange = getValueAxisRange();

        for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
            Range[] ranges = new Range[] {
                xRanges.get(0),
                yRanges.get(new Integer(i)) };

            if (i == 0) {
                ValueAxis xaxis = plot.getDomainAxis();
                zoomX(plot, xaxis, ranges[0], xrange);
            }

            ValueAxis yaxis = plot.getRangeAxis(i);

            if (yaxis == null) {
                continue;
            }

            zoomY(plot, yaxis, ranges[1], yrange);
        }
    }


    protected boolean zoomX(XYPlot plot, ValueAxis axis, Range range, Range x) {
        return zoom(plot, axis, range, x);
    }


    protected boolean zoomY(XYPlot plot, ValueAxis axis, Range range, Range x) {
        return zoom(plot, axis, range, x);
    }


    /**
     * Zooms the x axis to the range specified in the attribute document.
     *
     * @param plot The XYPlot.
     * @param axis The axis the shoud be modified.
     * @param range The whole range specified by a dataset.
     * @param x A user defined range (null permitted).
     *
     * @return true, if a zoom range was specified, otherwise false.
     */
    protected boolean zoom(XYPlot plot, ValueAxis axis, Range range, Range x) {
        if (x != null) {
            double min  = range.getLowerBound();
            double max  = range.getUpperBound();
            double diff = max > min ? max - min : min - max;

            Range computed = new Range(
                min + x.getLowerBound() * diff,
                min + x.getUpperBound() * diff);

            axis.setRangeWithMargins(computed);

            logger.debug("Zoom axis to: " + computed);

            return true;
        }

        axis.setRangeWithMargins(range);
        return false;
    }


    /**
     * This method extracts the minimum and maximum values for x and y axes
     * which are stored in <i>xRanges</i> and <i>yRanges</i>.
     *
     * @param index The index of the y-Axis.
     *
     * @return a Range[] as follows: [x-Range, y-Range].
     */
    public Range[] getRangesForDataset(int index) {
        return new Range[] {
            xRanges.get(new Integer(0)),
            yRanges.get(new Integer(index))
        };
    }


    protected void addAnnotations(XYPlot plot) {
        plot.clearAnnotations();

        if (annotations == null) {
            logger.debug("No Annotations given.");
            return;
        }

        LegendItemCollection lic = new LegendItemCollection();

        int idx = 0;
        if (plot.getRangeAxis(idx) == null && plot.getRangeAxisCount() >= 2) {
            idx = 1;
        }

        XYItemRenderer renderer = plot.getRenderer(idx);

        for (FLYSAnnotation fa: annotations) {
            Document theme = fa.getTheme();

            Color color = theme != null
                ? ThemeUtil.parseLineColorField(theme)
                : null;

            if (color == null) {
                color = Color.black;
            }

            lic.add(new LegendItem(fa.getLabel(), color));

            for (XYTextAnnotation ta: fa.getAnnotations()) {
                ta.setPaint(color);
                renderer.addAnnotation(ta);
            }

            plot.setFixedLegendItems(lic);
        }
    }


    /**
     * 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);

        plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d));

        if (plot.getDataset(0) != null) {
            plot.mapDatasetToRangeAxis(0, 0);
        }

        if (plot.getDataset(1) != null) {
            plot.mapDatasetToRangeAxis(1, 1);
        }
    }


    protected void addSubtitles(JFreeChart chart) {
        // override this method in subclasses that need subtitles
    }


    /**
     * This method walks over all axes (domain and range) of <i>plot</i> and
     * calls localizeDomainAxis() for domain axes or localizeRangeAxis() for
     * range axes.
     *
     * @param plot The XYPlot.
     */
    private void localizeAxes(XYPlot plot) {
        for (int i = 0, num = plot.getDomainAxisCount(); i < num; i++) {
            ValueAxis axis = plot.getDomainAxis(i);

            if (axis != null) {
                localizeDomainAxis(axis);
            }
            else {
                logger.warn("Domain axis at " + i + " is null.");
            }
        }

        for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
            ValueAxis axis = plot.getRangeAxis(i);

            if (axis != null) {
                localizeRangeAxis(axis);
            }
            else {
                logger.warn("Range axis at " + i + " is null.");
            }
        }
    }


    /**
     * Overrides the NumberFormat with the NumberFormat for the current locale
     * that is provided by getLocale().
     *
     * @param domainAxis The domain axis that needs localization.
     */
    protected void localizeDomainAxis(ValueAxis domainAxis) {
        NumberFormat nf = NumberFormat.getInstance(getLocale());
        ((NumberAxis) domainAxis).setNumberFormatOverride(nf);
    }


    /**
     * Overrides the NumberFormat with the NumberFormat for the current locale
     * that is provided by getLocale().
     *
     * @param domainAxis The domain axis that needs localization.
     */
    protected void localizeRangeAxis(ValueAxis rangeAxis) {
        NumberFormat nf = NumberFormat.getInstance(getLocale());
        ((NumberAxis) rangeAxis).setNumberFormatOverride(nf);
    }


    protected void applyThemes(XYPlot plot) {
        if (first != null) {
            applyThemes(plot, first, 0);
        }

        if (second != null) {
            applyThemes(plot, second, 1);
        }
    }


    protected void applyThemes(XYPlot plot, XYSeriesCollection dataset, int i) {
        LegendItemCollection lic  = new LegendItemCollection();
        LegendItemCollection anno = plot.getFixedLegendItems();

        XYLineAndShapeRenderer r = getRenderer(plot, i);

        for (int s = 0, num = dataset.getSeriesCount(); s < num; s++) {
            XYSeries series = dataset.getSeries(s);

            if (series instanceof StyledXYSeries) {
                ((StyledXYSeries) series).applyTheme(r, s);
            }

            // special case: if there is just one single item, we need to enable
            // points for this series, otherwise we would not see anything in
            // the chart area.
            if (series.getItemCount() == 1) {
                r.setSeriesShapesVisible(s, true);
            }

            lic.add(r.getLegendItem(i, s));
        }

        if (anno != null) {
            lic.addAll(anno);
        }

        plot.setFixedLegendItems(lic);

        plot.setRenderer(i, r);
    }


    protected XYLineAndShapeRenderer getRenderer(XYPlot plot, int idx) {
        XYLineAndShapeRenderer r =
            (XYLineAndShapeRenderer) plot.getRenderer(idx);

        if (r != null) {
            return r;
        }

        if (idx == 0) {
            logger.warn("No default renderer set!");
            return new XYLineAndShapeRenderer();
        }

        r = (XYLineAndShapeRenderer) plot.getRenderer(0);

        try {
            return (XYLineAndShapeRenderer) r.clone();
        }
        catch (CloneNotSupportedException cnse) {
            logger.warn(cnse, cnse);
        }

        logger.warn("No applicalable renderer found!");

        return new XYLineAndShapeRenderer();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org