view flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java @ 3228:698d09930329

Fix issue695 (labels of waterlines). flys-artifacts/trunk@4856 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 03 Jul 2012 09:32:47 +0000
parents ed07dd55f487
children c27c4e06dd87
line wrap: on
line source
package de.intevation.flys.exports;

import java.text.NumberFormat;
import java.util.List;

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.artifacts.DataProvider;
import de.intevation.flys.artifacts.geom.Lines;
import de.intevation.flys.artifacts.model.CrossSectionFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.HYKFactory;
import de.intevation.flys.jfree.FLYSAnnotation;
import de.intevation.flys.jfree.StyledXYSeries;
import de.intevation.flys.model.FastCrossSectionLine;
import de.intevation.flys.utils.Formatter;
import de.intevation.flys.utils.ThemeUtil;


/**
 * An OutGenerator that generates cross section graphs.
 */
public class CrossSectionGenerator
extends      LongitudinalSectionGenerator
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();
    }


    @Override
    protected YAxisWalker getYAxisWalker() {
        return new YAxisWalker() {
            @Override
            public int length() {
                return 1;
            }

            /** Get identifier for this index. */
            @Override
            public String getId(int idx) {
                return "W";
            }
        };
    }


    /**
     * Get localized chart title.
     */
    @Override
    public String getDefaultChartTitle() {
        Object[] i18n_msg_args = new Object[] {
            getRiverName()
        };
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT, i18n_msg_args);
    }


    /** Always return default subtitle. */
    @Override
    protected String getChartSubtitle() {
        // XXX NOTE: overriding this method disables ChartSettings subtitle!
        // The default implementation of this method in ChartGenerator returns
        // the subtitle changed via the chart settings dialog. This method
        // always returns the subtitle containing river and km, NEVER the
        // ChartSettings subtitle!
        return getDefaultChartSubtitle();
    }


    /** Get Charts default subtitle. */
    @Override
    protected String getDefaultChartSubtitle() {
        List<DataProvider> providers =
            context.getDataProvider(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA);
        double km = 0d;
        if (providers.size() > 0) {
            FastCrossSectionLine csl = (FastCrossSectionLine) providers.get(0).
                provideData(CrossSectionFacet.BLACKBOARD_CS_MASTER_DATA,
                    null, context);
            km = csl.getKm();
        }

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

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


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


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


    @Override
    protected String getDefaultYAxisLabel(int pos) {
        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,
                attr,
                visible);
        }
        else if (name.equals(HYK)) {
            doHyk(artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else if (FacetTypes.IS.MANUALLINE(name)) {
            doCrossSectionWaterLineOut(
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints(artifactFacet.getData(context),
                artifactFacet,
                attr, visible, YAXIS.W.idx);
        }
        else {
            logger.warn("CrossSection.doOut: Unknown facet name: " + name);
            return;
        }
    }


    /** Look up the axis identifier for a given facet type. */
    public int axisIdxForFacet(String facetName) {
        // TODO Where to add thid axis too.
        return 0;
    }


    /**
     * 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");

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

        if (!ThemeUtil.parseShowLineLabel(theme)) {
            series.setLabel("");
        }
        if (ThemeUtil.parseShowWidth(theme)) {
            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
            String labelAdd = "b=" + nf.format(lines.width) + "m";
            if (series.getLabel().equals("")) {
               series.setLabel(labelAdd);
            }
            else {
                series.setLabel(series.getLabel() + ", " + labelAdd);
            }
        }
        if (ThemeUtil.parseShowLevel(theme) && lines.points.length >0
            && lines.points[1].length > 0) {
            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
            String labelAdd = "W=" + nf.format(lines.points[1][0]) + "NN+m";
            if (series.getLabel().equals("")) {
               series.setLabel(labelAdd);
            }
            else {
                series.setLabel(series.getLabel() + ", " + labelAdd);
            }
        }
        if (ThemeUtil.parseShowMiddleHeight(theme) && lines.width != 0) {
            NumberFormat nf = nf = Formatter.getMeterFormat(this.context);
            String labelAdd = "H=" + nf.format(lines.area / lines.width) + "m";
                // : " + lines.area + "/" + lines.width);
            if (series.getLabel().equals("")) {
               series.setLabel(labelAdd);
            }
            else {
                series.setLabel(series.getLabel() + ", " + labelAdd);
            }
        }

        StyledSeriesBuilder.addPoints(series, lines.points, false);

        addAxisSeries(series, 0, visible);
    }


    /** Add HYK-Annotations (colorize and label some areas, draw lines. */
    protected void doHyk(
        Object   o,
        String   seriesName,
        Document theme,
        boolean  visible
    ) {
        logger.debug("CrossSectionGenerator.doHyk");

        List<HYKFactory.Zone> zones = (List<HYKFactory.Zone>) o;

        if (zones == null || zones.size() == 0) {
            logger.warn("CrossSectionGenerator.doHYK: empty zone list received.");
            return;
        }

        // Actual Styling is done in XYChartGenerator.
        if (visible) {
            addAnnotations(new FLYSAnnotation(seriesName, null, zones, theme));
        }
    }


    /**
     * 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, false);

        addAxisSeries(series, 0, visible);
    }


    /**
     * Creates a new <i>ChartSection</i>.
     *
     * @return a new <i>ChartSection</i>.
     */
    @Override
    protected ChartSection buildChartSection() {
        ChartSection chartSection = new ChartSection();
        chartSection.setTitle(getChartTitle());
        chartSection.setDisplayGird(isGridVisible());
        return chartSection;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org