view flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java @ 2020:4f7f781e4481

Improved area rendering workflow. flys-artifacts/trunk@3475 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 20 Dec 2011 06:47:08 +0000
parents 5aecebcc4698
children 76cedac30d35
line wrap: on
line source
package de.intevation.flys.exports;

import org.apache.log4j.Logger;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;

import org.w3c.dom.Document;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;

import de.intevation.flys.artifacts.WINFOArtifact;

import de.intevation.flys.artifacts.model.FacetTypes;


/**
 * An OutGenerator that generates cross section graphs.
 */
public class CrossSectionGenerator
extends      XYChartGenerator
implements   FacetTypes
{
    /** The logger that is used in this generator. */
    private static Logger logger =
        Logger.getLogger(CrossSectionGenerator.class);

    public static final String I18N_CHART_TITLE =
        "chart.cross_section.title";

    public static final String I18N_CHART_SUBTITLE =
        "chart.cross_section.subtitle";

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

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

    public static final String I18N_CHART_TITLE_DEFAULT = "Querprofildiagramm";
    public static final String I18N_XAXIS_LABEL_DEFAULT = "Abstand [m]";
    public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]";


    /** Trivial Constructor. */
    public CrossSectionGenerator() {
        super();
    }


    /**
     * Get localized chart title.
     */
    protected String getChartTitle() {
        // TODO get river etc for localized heading
        Object[] i18n_msg_args = new Object[] {
            getRiverName()
        };
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT, i18n_msg_args);
    }


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

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

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


    /**
     * Add localized subtitle to chart.
     */
    @Override
    protected void addSubtitles(JFreeChart chart) {
        String subtitle = getChartSubtitle();
        chart.addSubtitle(new TextTitle(subtitle));
    }


    /**
     * Get localized X Axis label.
     */
    protected String getXAxisLabel() {
        return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
    }


    /**
     * Get cross_section.km data from artifact.
     */
    protected Double getKm() {
        try {
            WINFOArtifact winfo = (WINFOArtifact) master;
            // TODO get km of "master cross section."
            //return winfo.getCrossSectionSnapKm(0);
            return 0.0d;
        }
        catch (Exception e) {
            logger.error("Cannot convert cross_section.km to double");
            return 0.0d;
        }
    }


    /**
     * Get localized Y Axis label.
     */
    protected String getYAxisLabel() {
        return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT);
    }


    /**
     * Let one facet do its job.
     */
    public void doOut(
        ArtifactAndFacet artifactFacet,
        Document         attr,
        boolean          visible
    ) {
        String name = artifactFacet.getFacetName();

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

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

        if (name.equals(CROSS_SECTION)) {
            doCrossSectionOut(
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else if (name.equals(CROSS_SECTION_WATER_LINE)) {
            doCrossSectionWaterLineOut(
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else if (FacetTypes.IS.AREA(name)) {
            doArea(artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else {
            logger.warn("CrossSection.doOut: Unknown facet name: " + name);
            return;
        }
    }


    /**
     * Do Area out.
     */
    protected void doArea(
        Object     o,
        String     seriesName,
        Document   theme,
        boolean    visible
    ) {
        logger.debug("CrossSectionGenerator.doArea");
        StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(theme);

        // TODO make this more stable.
        Object[] doubles = (Object[]) o;
        XYSeries up   = null;
        XYSeries down = null;

        if (doubles[1] != null) {
            up = new StyledXYSeries(seriesName, false, theme);
            StyledSeriesBuilder.addPoints(up, (double [][]) doubles[1]);
        }

        if (doubles[0] != null) {
            // TODO: Sort this out: when the two series have the same name,
            // the renderer (or anything in between) will not work correctly.
            down = new StyledXYSeries(seriesName + " ", false, theme);
            StyledSeriesBuilder.addPoints(down, (double [][]) doubles[0]);
        }

        if (up == null && down != null) {
            area.setMode(StyledAreaSeriesCollection.FILL_MODE.ABOVE);
            down.setKey(seriesName);
            area.addSeries(down);
        }
        else if (up != null && down == null) {
            area.setMode(StyledAreaSeriesCollection.FILL_MODE.UNDER);
            area.addSeries(up);
        }
        else if (up != null && down != null) {
            area.setMode(StyledAreaSeriesCollection.FILL_MODE.BETWEEN);
            area.addSeries(up);
            area.addSeries(down);
        }
        addAreaSeries(area, 0, visible);
    }


    /**
     * Do cross sections waterline out.
     *
     * @param seriesName name of the data (line) to display in legend.
     * @param theme Theme for the data series.
     */
    protected void doCrossSectionWaterLineOut(
        Object     o,
        String     seriesName,
        Document   theme,
        boolean    visible
    ) {
        logger.debug("CrossSectionGenerator.doCrossSectionWaterLineOut");

        // DO NOT SORT DATA! This destroys the gaps indicated by NaNs
        XYSeries series = new StyledXYSeries(seriesName, false, theme);

        StyledSeriesBuilder.addPoints(series, (double [][]) o);

        addAxisSeries(series, 0, visible);
    }


    /**
     * Do cross sections out.
     *
     * @param seriesName name of the data (line) to display in legend.
     * @param theme Theme for the data series.
     */
    protected void doCrossSectionOut(
        Object     o,
        String     seriesName,
        Document   theme,
        boolean    visible
    ) {
        logger.debug("CrossSectionGenerator.doCrossSectionOut");

        XYSeries series = new StyledXYSeries(seriesName, theme);

        StyledSeriesBuilder.addPoints(series, (double [][]) o);

        addAxisSeries(series, 0, visible);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org