view flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java @ 672:bc1e4878d7e3

Corrected axes/datasets mappings. flys-artifacts/trunk@2094 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 09 Jun 2011 10:31:22 +0000
parents d73b02526a6e
children 591249171c32
line wrap: on
line source
package de.intevation.flys.exports;

import java.awt.Color;

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.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import de.intevation.artifacts.Artifact;

import de.intevation.flys.artifacts.WINFOArtifact;
import de.intevation.flys.artifacts.model.WQDay;


/**
 * An OutGenerator that generates duration curves.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DurationCurveGenerator extends XYChartGenerator {

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

    /** The storage for the W series to be drawn in this chart.*/
    protected XYSeriesCollection w;

    /** The storage for the Q series to be drawn in this chart.*/
    protected XYSeriesCollection q;


    public static final String DURATION_CURVE_W =
        "duration_curve.w";

    public static final String DURATION_CURVE_Q =
        "duration_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();

        this.w = new XYSeriesCollection();
        this.q = new XYSeriesCollection();
    }


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


    public void addDatasets(JFreeChart chart) {
        XYPlot plot = (XYPlot) chart.getPlot();

        plot.setDataset(0, w);
        plot.setDataset(1, q);
    }


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

        // TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES!
        XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer)
            plot.getRendererForDataset(w);

        XYLineAndShapeRenderer rq = null;
        try {
            rq = (XYLineAndShapeRenderer) rw.clone();
        }
        catch (Exception e) {
            logger.error(e, e);
        }

        int wNum = w.getSeriesCount();
        int qNum = q.getSeriesCount();

        for (int i = 0; i < wNum; i++) {
            rw.setSeriesPaint(i, Color.BLUE);
        }

        for (int i = 0; i < qNum; i++) {
            rq.setSeriesPaint(i, Color.GREEN);
        }

        plot.setRenderer(0, rw);
        plot.setRenderer(1, rq);
    }


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

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

        plot.setRangeAxis(1, qAxis);
        plot.mapDatasetToRangeAxis(1, 1);
    }


    public void doOut(Artifact artifact, String facet, Document attr) {
        logger.debug("DurationCurveGenerator.doOut: " + facet);

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

        if (facet.equals(DURATION_CURVE_W)) {
            doWOut(getDurationCurveData(artifact));
        }
        else if (facet.equals(DURATION_CURVE_Q)) {
            doQOut(getDurationCurveData(artifact));
        }
        else {
            logger.warn("Unknown facet name: " + facet);
            return;
        }
    }


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

        // TODO find the correct series name
        XYSeries series = new XYSeries("W-1");

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

        this.w.addSeries(series);
    }


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

        // TODO find the correct series name
        XYSeries series = new XYSeries("Q-1");

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

        this.q.addSeries(series);
    }


    /**
     * Returns the computed data for a duration curve based on the artifact's
     * computation method.
     *
     * @param artifact The WINFO artifact.
     *
     * @return the computed data for a duration curve's W and Q facet.
     */
    protected WQDay getDurationCurveData(Artifact artifact) {
        WINFOArtifact winfoArtifact = (WINFOArtifact) artifact;
        return winfoArtifact.getDurationCurveData();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org