view flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents cd5eb8f5f6f1
children
line wrap: on
line source
package de.intevation.flys.exports;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WQDay;
import de.intevation.flys.jfree.Bounds;
import de.intevation.flys.jfree.FLYSAnnotation;
import de.intevation.flys.jfree.StyledXYSeries;

import java.awt.Font;
import java.awt.geom.Point2D;

import org.apache.log4j.Logger;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries;
import org.w3c.dom.Document;


/**
 * An OutGenerator that generates duration curves.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DurationCurveGenerator
extends      XYChartGenerator
implements   FacetTypes
{
    public static enum YAXIS {
        W(0),
        Q(1);
        public int idx;
        private YAXIS(int c) {
           idx = c;
        }
    }

    /** Local logger. */
    private static Logger logger =
        Logger.getLogger(DurationCurveGenerator.class);

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


    /**
     * Create Axis for given index.
     * @return axis with according internationalized label.
     */
    @Override
    protected NumberAxis createYAxis(int index) {
        Font labelFont = new Font("Tahoma", Font.BOLD, 14);
        String label   = getYAxisLabel(index);

        NumberAxis axis = createNumberAxis(index, label);
        if (index == YAXIS.W.idx) {
            axis.setAutoRangeIncludesZero(false);
        }
        axis.setLabelFont(labelFont);
        return axis;
    }


    @Override
    protected String getDefaultChartTitle() {
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
    }


    @Override
    protected String getDefaultChartSubtitle() {
        double[] dist  = getRange();

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

        return msg(I18N_CHART_SUBTITLE, "", args);
    }


    @Override
    protected String getDefaultXAxisLabel() {
        return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
    }


    @Override
    protected String getDefaultYAxisLabel(int index) {
        String label = "default";
        if (index == YAXIS.W.idx) {
            label = msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT);
        }
        else if (index == YAXIS.Q.idx) {
            // TODO i18n for this label
            label = "Q [m\u00b3/s]";
            //label = msg(get2YAxisLabelKey(), get2YAxisDefaultLabel());
        }

        return label;
    }


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

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

        axis.setUpperBound(364);

        return zoomin;
    }


    /**
     * This method overrides the method in the parent class to set the lower
     * bounds of the Q axis to 0. This axis should never display negative
     * values on its own.
     */
    @Override
    protected boolean zoomY(XYPlot plot, ValueAxis axis, Bounds bounds, Range x) {
        boolean zoomin = super.zoom(plot, axis, bounds, x);

        if (!zoomin && axis instanceof IdentifiableNumberAxis) {
            String id = ((IdentifiableNumberAxis) axis).getId();

            if (YAXIS.Q.toString().equals(id)) {
                axis.setLowerBound(0d);
            }
        }

        return zoomin;
    }


    @Override
    public void doOut(
        ArtifactAndFacet artifactFacet,
        Document         attr,
        boolean          visible
    ) {
        String name = artifactFacet.getFacetName();

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

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

        if (name.equals(DURATION_W)) {
            doWOut(
                (WQDay) artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (name.equals(DURATION_Q)) {
            doQOut(
                (WQDay) artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (name.equals(DURATION_MAINVALUES_Q)
                || name.equals(MAINVALUES_Q)
                || name.equals(COMPUTED_DISCHARGE_MAINVALUES_W)
                || name.equals(MAINVALUES_W)
        ) {
            doAnnotations(
                (FLYSAnnotation) artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (name.equals(RELATIVE_POINT)) {
            doPointOut((Point2D) artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints(
                artifactFacet.getData(context),
                artifactFacet,
                attr, visible, YAXIS.W.idx);
        }
        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,
        ArtifactAndFacet aaf,
        Document         theme,
        boolean          visible
    ) {
        logger.debug("DurationCurveGenerator.doWOut");

        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);

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

            series.add(day, w);
        }

        addAxisSeries(series, YAXIS.W.idx, visible);
    }

    protected void doPointOut(
        Point2D point,
        ArtifactAndFacet aandf,
        Document theme,
        boolean visible
    ){
        logger.debug("DurationCurveGenerator.doPointOut");

        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);

        series.add(point.getX(), point.getY());

        addAxisSeries(series, YAXIS.W.idx, 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,
        ArtifactAndFacet aaf,
        Document         theme,
        boolean          visible
    ) {
        logger.debug("DurationCurveGenerator.doQOut");

        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);

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

            series.add(day, q);
        }

        addAxisSeries(series, YAXIS.Q.idx, visible);
    }


    @Override
    protected YAxisWalker getYAxisWalker() {
        return new YAxisWalker() {
            @Override
            public int length() {
                return YAXIS.values().length;
            }

            @Override
            public String getId(int idx) {
                YAXIS[] yaxes = YAXIS.values();
                return yaxes[idx].toString();
            }
        };
    }

    // MainValue-Annotations should be visualized by a line that goes to the curve itself.
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org