view flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java @ 1684:bdb05dc9b763

Bugfix: #353 Enabled chart's to be drawn with proper axes set even if no data is contained. flys-artifacts/trunk@2902 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 07 Oct 2011 10:51:09 +0000
parents f7761914f745
children 6d9184c745dd
line wrap: on
line source
package de.intevation.flys.exports;

import org.w3c.dom.Document;

import org.apache.log4j.Logger;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries;

import de.intevation.artifacts.Artifact;

import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WQDay;
import de.intevation.flys.artifacts.resources.Resources;


/**
 * An OutGenerator that generates duration curves.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DurationCurveGenerator
extends      XYChartGenerator
implements   FacetTypes
{
    private static Logger logger =
        Logger.getLogger(DurationCurveGenerator.class);


    public static final String I18N_DURATION_W =
        "chart.duration.curve.curve.w";

    public static final String I18N_DURATION_Q =
        "chart.duration.curve.curve.q";

    public static final String I18N_CHART_TITLE =
        "chart.duration.curve.title";

    public static final String I18N_CHART_SUBTITLE =
        "chart.duration.curve.subtitle";

    public static final String I18N_XAXIS_LABEL =
        "chart.duration.curve.xaxis.label";

    public static final String I18N_YAXIS_LABEL =
        "chart.duration.curve.yaxis.label";

    public static final String I18N_CHART_TITLE_DEFAULT  =
        "Dauerlinie";

    public static final String I18N_XAXIS_LABEL_DEFAULT  =
        "Unterschreitungsdauer [Tage]";

    public static final String I18N_YAXIS_LABEL_DEFAULT  =
        "W [NN + m]";


    public DurationCurveGenerator() {
        super();
    }


    protected String getChartTitle() {
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
    }


    @Override
    protected void addSubtitles(JFreeChart chart) {
        double[] dist  = getRange();

        Object[] args = new Object[] {
            getRiverName(),
            dist[0]
        };

        String subtitle = msg(I18N_CHART_SUBTITLE, "", args);
        chart.addSubtitle(new TextTitle(subtitle));
    }


    protected String getXAxisLabel() {
        return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
    }


    protected String getYAxisLabel() {
        return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT);
    }


    @Override
    protected boolean zoomX(XYPlot plot, ValueAxis axis, Range range, Range x) {
        boolean zoomin = super.zoom(plot, axis, range, x);

        if (!zoomin) {
            axis.setLowerBound(0d);
        }

        return zoomin;
    }


    protected void adjustAxes(XYPlot plot) {
        super.adjustAxes(plot);

        NumberAxis qAxis = new NumberAxis("Q [m\u00b3/s]");

        plot.setRangeAxis(1, qAxis);
    }


    @Override
    public void doOut(
        Artifact artifact,
        Facet    facet,
        Document attr,
        boolean  visible
    ) {
        String name = facet != null ? facet.getName() : null;

        logger.debug("DurationCurveGenerator.doOut: " + name);

        if (name == null || name.length() == 0) {
            logger.error("No facet given. Cannot create dataset.");
            return;
        }

        FLYSArtifact flys = (FLYSArtifact) artifact;
        Facet        f    = flys.getNativeFacet(facet);

        if (name.equals(DURATION_W)) {
            doWOut((WQDay) f.getData(artifact, context), attr, visible);
        }
        else if (name.equals(DURATION_Q)) {
            doQOut((WQDay) f.getData(artifact, context), attr, visible);
        }
        else {
            logger.warn("Unknown facet name: " + name);
            return;
        }
    }


    /**
     * Creates the series for a duration curve's W facet.
     *
     * @param wqdays The WQDay store that contains the Ws.
     * @param theme
     */
    protected void doWOut(WQDay wqdays, Document theme, boolean visible) {
        logger.debug("DurationCurveGenerator.doWOut");

        // TODO find the correct series name
        XYSeries series = new StyledXYSeries(
            getSeriesName(getRiverName(), DURATION_W), theme);

        int size = wqdays.size();
        for (int i = 0; i < size; i++) {
            int  day = wqdays.getDay(i);
            double w = wqdays.getW(i);

            series.add((double) day, w);
        }

        addFirstAxisSeries(series, visible);
    }


    /**
     * Creates the series for a duration curve's Q facet.
     *
     * @param wqdays The WQDay store that contains the Qs.
     * @param theme
     */
    protected void doQOut(WQDay wqdays, Document theme, boolean visible) {
        logger.debug("DurationCurveGenerator.doQOut");

        // TODO find the correct series name
        XYSeries series = new StyledXYSeries(
            getSeriesName(getRiverName(), DURATION_Q), theme);

        int size = wqdays.size();
        for (int i = 0; i < size; i++) {
            int  day = wqdays.getDay(i);
            double q = wqdays.getQ(i);

            series.add((double) day, q);
        }

        addSecondAxisSeries(series, visible);
    }


    protected String getSeriesName(String river, String type) {
        Object[] args = new Object[] { river };

        if (type == null || type.length() == 0) {
            logger.warn("No duration curve type given.");
            return "n/a";
        }
        else if (type.equals(DURATION_W)) {
            return Resources.getMsg(
                context.getMeta(),
                I18N_DURATION_W,
                "W",
                args);
        }
        else if (type.equals(DURATION_Q)) {
            return Resources.getMsg(
                context.getMeta(),
                I18N_DURATION_Q,
                "W",
                args);
        }

        logger.warn("Could not determine chart curve type: " + type);
        return type;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org