view flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java @ 1092:0eb585cd3882

Added limited themeing-support for MainValues. flys-artifacts/trunk@2595 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 26 Aug 2011 12:44:12 +0000
parents e298c4d28927
children 1ea7eb72aaa6
line wrap: on
line source
package de.intevation.flys.exports;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;

import java.awt.Color;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import de.intevation.artifacts.Artifact;

import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.NamedDouble;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WQKms;

import de.intevation.flys.jfree.StickyAxisAnnotation;

import de.intevation.flys.utils.ThemeUtil;


/**
 * An OutGenerator that generates discharge curves.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ComputedDischargeCurveGenerator
extends      DischargeCurveGenerator
implements   FacetTypes
{
    /** The logger used in this generator. */
    private static Logger logger =
        Logger.getLogger(ComputedDischargeCurveGenerator.class);


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

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

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

    public static final String I18N_CHART_TITLE_DEFAULT = "Abflusskurve";
    public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]";
    public static final String I18N_MAINVALUES_Q_LABEL = "Q (Haupt- und Extremwerte)";
    public static final String I18N_MAINVALUES_W_LABEL = "W (Haupt- und Extremwerte)";

    /** List of Annotations (specifically, Main Values). */
    protected List<XYAnnotation> annotations;

    /** Pseudo-Dataseries to have a legend for annotations. */
    protected XYSeriesCollection pseudoAnnotationData = null;

    // TODO Let theme pass through to annotations-facets.


    /** Trivial Constructor. */
    public ComputedDischargeCurveGenerator () {
        super();
        annotations= new ArrayList<XYAnnotation>();
    }


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


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


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

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

        if (name == null) {
            logger.warn("Broken facet in computed discharge out generation.");
            return;
        }

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

        if (name.equals(COMPUTED_DISCHARGE_Q)) {
            doQOut((WQKms) f.getData(artifact, context), attr);
        }
        else if (name.equals(COMPUTED_DISCHARGE_MAINVALUES_Q)) {
            doMainValueQAnnotations(f.getData(artifact, context), attr);
        }
        else if (name.equals(COMPUTED_DISCHARGE_MAINVALUES_W)) {
            doMainValueWAnnotations(f.getData(artifact, context), attr);
        }
        else {
            logger.warn("Unknown facet type for computed discharge: " + name);
            return;
        }
    }


    /**
     * Store W MainValues as annotations for later plotting.
     */
    protected void doMainValueWAnnotations(Object o, Document theme) {
        logger.debug("ComputedDischargeCurveGenerator set W MainValues.");
        if (pseudoAnnotationData == null) {
            pseudoAnnotationData = new XYSeriesCollection();
        }

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

        List<NamedDouble> mainValuesW = (List<NamedDouble>) o;
        for (NamedDouble mv: mainValuesW) {
            float pos = (float) mv.getValue();
            String text = mv.getName();
            StickyAxisAnnotation ta = new StickyAxisAnnotation(text, pos,
                    StickyAxisAnnotation.SimpleAxis.Y_AXIS);
            ta.setPaint(color);
            this.annotations.add(ta);
        }
        String label = msg(I18N_MAINVALUES_W_LABEL, I18N_MAINVALUES_W_LABEL, null);
        pseudoAnnotationData.addSeries(new StyledXYSeries(label, theme));
    }

    
    /**
     * Store Q MainValues as annotations for later plotting.
     */
    protected void doMainValueQAnnotations(Object o, Document theme) {
        logger.debug("ComputedDischargeCurveGenerator set Q MainValues.");

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

        if (pseudoAnnotationData == null) {
            pseudoAnnotationData = new XYSeriesCollection();
        }

        List<NamedDouble> mainValuesQ = (List<NamedDouble>) o;
        for (NamedDouble mv: mainValuesQ) {
            float pos = (float) mv.getValue();
            String text = mv.getName();
            StickyAxisAnnotation ta = new StickyAxisAnnotation(text, pos);
            ta.setPaint(color);
            this.annotations.add(ta);
        }
        String label = msg(I18N_MAINVALUES_Q_LABEL, I18N_MAINVALUES_Q_LABEL, null);
        pseudoAnnotationData.addSeries(new StyledXYSeries(label, theme));
    }


    /** Generate Chart with annotations. */
    @Override
    public JFreeChart generateChart() {
        JFreeChart c = super.generateChart();
        XYPlot p = (XYPlot) c.getPlot();
        redoAnnotations(p);
        return c;
    }


    /**
     * Recalculate some annotation positions and add them to plot.
     * Annotations represent MainValues.
     * @param plot      Plot to add annotations to.
     */
    protected void redoAnnotations(XYPlot plot) {
        plot.clearAnnotations();

        for (XYAnnotation a: annotations) {
            plot.addAnnotation(a, false);
        }
    }


    /**
     * Add Q-Series to plot.
     * @param wqkms actual data
     * @param theme theme to use.
     */
    protected void doQOut(WQKms wqkms, Document theme) {
        int size = wqkms.size();

        double[]   res  = new double[3];

        XYSeries series = new StyledXYSeries(getSeriesName(wqkms), theme);
        for (int i = 0; i < size; i++) {
            res = wqkms.get(i, res);
            series.add(res[1], res[0]);
        }

        addFirstAxisSeries(series);
    }

    /**
     * Add datasets to plot.
     * @param plot plot to add datasets to.
     * @todo merge with LongitudinalSectionGenerator/superclass.
     */
    @Override
    protected void addDatasets(XYPlot plot) {
        super.addDatasets(plot);
        if (pseudoAnnotationData != null) {
            plot.setDataset(2, pseudoAnnotationData);
        }
    }
        
    protected String getSeriesName(WQKms wqkms) {
        Object[] args = new Object[] {
            getRiverName(),
            wqkms.getName()
        };

        return msg(
            "chart.computed.discharge.curve.curve.label",
            "",
            args);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org