view flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesCurveGenerator.java @ 1937:f07d64d5cbe1

'W auf freier Strecke' calculation. Fetch corresponding Qs for given Ws from the WST model flys-artifacts/trunk@3318 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Nov 2011 15:50:31 +0000
parents 9e9cfc036a3f
children 06d8d371d244
line wrap: on
line source
package de.intevation.flys.exports;

import org.apache.log4j.Logger;

import org.jfree.chart.title.TextTitle;

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

import org.w3c.dom.Document;

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.WKms;

import de.intevation.flys.utils.DataUtil;

import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.utils.FLYSUtils;

import de.intevation.flys.jfree.FLYSAnnotation;

/**
 * An OutGenerator that generates w differences curves.
 */
public class WDifferencesCurveGenerator
extends      LongitudinalSectionGenerator
implements   FacetTypes
{
    public static enum YAXIS {
        D(0),
        W(1),
        Q(2);
        protected int idx;
        private YAXIS(int c) {
           idx = c;
        }
    }

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

    /** Key for internationalized title of WDiff charts. */
    public final static String I18N_WDIFF_TITLE = "chart.w_differences.title";

    /** Default for internationalized title (when no translation found). */
    public final static String I18N_WDIFF_TITLE_DEFAULT = "Differences";

    /** Key for internationalized title of WDiff charts. */
    public final static String I18N_WDIFF_2YAXIS_LABEL =
        "chart.w_differences.yaxis.second.label";

    /** Default for label for second Y-Axis when no translation found. */
    public final static String I18N_WDIFF_2YAXIS_LABEL_DEFAULT = "W [NN + m]";

    public final static String I18N_WDIFF_SUBTITLE =
        "chart.w_differences.subtitle";

    public final static String I18N_WDIFF_YAXIS_LABEL =
        "chart.w_differences.yaxis.label";

    public final static String I18N_WDIFF_YAXIS_LABEL_DEFAULT = "m";


    /**
     * Get internationalized title for chart.
     * @return internationalized Chart title.
     */
    @Override
    public String getChartTitle() {
        return msg(I18N_WDIFF_TITLE, I18N_WDIFF_TITLE_DEFAULT);
    }


    /**
     * Get default value for the second Y-Axis' label (if no translation was
     * found).
     * @return default value for second y-axis label.
     */
    @Override
    protected String get2YAxisDefaultLabel() {
        return I18N_WDIFF_2YAXIS_LABEL_DEFAULT;
    }


    /**
     * Gets key to look up internationalized String for the charts subtitle.
     * @return key to look up translated subtitle.
     */
    @Override
    protected String getChartSubtitleKey() {
        return I18N_WDIFF_SUBTITLE;
    }


    /**
     * Get key for internationalization of the second Y-Axis' label.
     * @return internationalized second y-axis.
     */
    @Override
    protected String get2YAxisLabelKey() {
        return I18N_WDIFF_2YAXIS_LABEL;
    }


    /**
     * Get internationalized label for the y axis.
     * @return internationalized label for y axos.
     */
    @Override
    protected String getYAxisLabel() {
        FLYSArtifact flys = (FLYSArtifact) master;

        String unit = FLYSUtils.getRiver(flys).getWstUnit().getName();

        return msg(
            I18N_WDIFF_YAXIS_LABEL,
            I18N_WDIFF_YAXIS_LABEL_DEFAULT,
            new Object[] { unit });
    }


    /**
     * Add (themed) data for chart generation.
     */
    @Override
    public void doOut(
        Artifact artifact,
        Facet    facet,
        Document attr,
        boolean  visible
    ) {
        String name = facet.getName();

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

        if (name == null) {
            logger.error("No facet name for doOut(). No output generated!");
            return;
        }

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

        if (f == null) {
            return;
        }

        if (name.equals(W_DIFFERENCES)) {
            doWDifferencesOut(
                (WKms) f.getData(artifact, context),
                facet,
                attr,
                visible);
        }
        else if (name.equals(LONGITUDINAL_W)) {
            doWOut((WQKms) f.getData(artifact, context), facet, attr, visible);
        }
        else if (name.equals(STATIC_WKMS) || name.equals(HEIGHTMARKS_POINTS)) {
            doWOut((WKms) f.getData(artifact, context), facet, attr, visible);
        }
        else if (name.equals(LONGITUDINAL_ANNOTATION)) {
            doAnnotations((FLYSAnnotation) f.getData(artifact, context),
                 facet, attr, visible);
        }
        else {
            logger.warn("Unknown facet name: " + name);
            return;
        }
    }


    /**
     * Add the waterlevel-curves (the "absolutes" from which
     * differences were calculated).
     *
     * @param wqkms The wqkms to add to the diagram.
     * @param theme The theme that contains styling information.
     */
    protected void doWOut(
        WKms     wkms,
        Facet    facet,
        Document theme,
        boolean  visible
    ) {
        logger.debug("WDifferencesCurveGenerator.doWOut");

        XYSeries series = new StyledXYSeries(facet.getDescription(), theme);

        StyledSeriesBuilder.addPoints(series, wkms);

        // Note: the only difference in the super-implementation
        //  (in LongitudinalSectionGenerator) is here (adds with
        //  addFirstAxisSeries() .
        addAxisSeries(series, YAXIS.W.idx, visible);

        if (wkms instanceof WQKms) {
            if (needInvertAxis((WQKms) wkms)) {
                setInverted(true);
            }
        }
    }

    /**
     * Add (internationalized) subtitle to chart.
     * Overridden to avoid trying to access the range of masterartifact.
     * @see getChartSubtitleKey
     */
    @Override
    protected void addSubtitles(JFreeChart chart) {

        // TODO i18n
        /*
        Object[] args = new Object[] {
            getRiverName()
        };
        */
        String subtitle = getRiverName();
        chart.addSubtitle(new TextTitle(subtitle));
    }


    /**
     * Add items to dataseries which describes the differences.
     */
    protected void doWDifferencesOut(
        WKms       wkms,
        Facet      facet,
        Document   theme,
        boolean    visible
    ) {
        logger.debug("WDifferencesCurveGenerator.doWDifferencesOut");
        if (wkms == null) {
            logger.warn("No data to add to WDifferencesChart.");
            return;
         }

        XYSeries series = new StyledXYSeries(facet.getDescription(), theme);

        if (logger.isDebugEnabled()) {
            if (wkms.size() > 0) {
                logger.debug("Generate series: " + series.getKey());
                logger.debug("Start km: " + wkms.getKm(0));
                logger.debug("End   km: " + wkms.getKm(wkms.size()-1));
                logger.debug("Values  : " + wkms.size());
            }
        }

        StyledSeriesBuilder.addPoints(series, wkms);

        addAxisSeries(series, YAXIS.D.idx, visible);
        if (DataUtil.guessWaterIncreasing(wkms.allWs())) {
            setInverted(true);
        }
    }

    @Override
    protected NumberAxis createYAxis(int index) {
        String s = "" + index;
        return new NumberAxis(s);
    }

    /**
     * Disable Longitudinals behaviour to include "0" in the Q axis.
     *
     * @param range range with which to look up upper bound.
     * @return range to be used for "auto-scaling" axis.
     */
    @Override
    protected Range createSecondAxisRange(Range range) {
        if (range == null) {
            return range;
        }
        return new Range(range.getLowerBound(), range.getUpperBound());
    }

 
    /**
     * 
     */
    @Override
    public JFreeChart generateChart() {
        JFreeChart chart = super.generateChart();
        if (chart != null && chart.getPlot() != null) {
            XYPlot plot = (XYPlot) chart.getPlot();
            plot.setRangeZeroBaselineVisible(true);
        }
        return chart;
    }

    /**
     * Get name of series (displayed in legend).
     * @return name of the series.
     */
    protected String getSeriesName(WKms wqkms, String mode) {
        String name   = wqkms.getName();
        String prefix = (name != null && name.indexOf(mode) >= 0)
                      ? null
                      : mode;

        return (prefix != null && prefix.length() > 0)
                ? prefix + "(" + name +")"
                : name;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org