view artifacts/src/main/java/org/dive4elements/river/exports/DischargeGenerator.java @ 8722:a83d519155ab

(issue1754) Do not base smoothing radius on calculation range Using the calculation parameters for startkm and endkm in the case that the domain axis had the default extend caused weird behavior when zooming and led to too large radius values for most data that only had valid values on a subset of the caluclation range.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 28 Apr 2015 14:22:47 +0200
parents bc803f4b6784
children e6d0fc817e20
line wrap: on
line source
/* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.exports;

import java.awt.Font;

import org.apache.log4j.Logger;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.exports.injector.InjectorConstants;
import org.dive4elements.river.jfree.AxisDataset;
import org.dive4elements.river.jfree.DoubleBounds;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;

public class DischargeGenerator
extends      DiagramGenerator
implements   InjectorConstants
{
    private static Logger log = Logger.getLogger(DischargeGenerator.class);

    private String I18N_AXIS_LABEL = "chart.discharge.curve.yaxis.cm.label";

    private int wAxisIndex;
    private int wInCmAxisIndex;
    double pnpValue;

    public DischargeGenerator() {
    }

    @Override
    public void addDatasets(XYPlot plot) {
        super.addDatasets(plot);

        Object pnp = context.getContextValue(PNP);
        if (!(pnp instanceof Number)) {
            return;
        }

        pnpValue = ((Number)pnp).doubleValue();

        wAxisIndex = diagramAttributes.getAxisIndex("W");
        if (wAxisIndex == -1) {
            log.warn("No W axis found.");
            return;
        }

        AxisDataset data = datasets.get(wAxisIndex);
        if (data == null) {
            // No W axis
            return;
        }

        if (data.getRange() == null) {
            // No active datasets
            return;
        }

        Range axisRange = inCm(
            plot.getRangeAxis(wAxisIndex).getRange(),
            pnpValue
        );
        Range dataRange = inCm(data.getRange(), pnpValue);

        // Do we have an index for W in cm?
        NumberAxis wInCmAxis = createWinCMAxis(wAxisIndex);
        wInCmAxis.setRange(axisRange);

        wInCmAxisIndex = plot.getRangeAxisCount();
        plot.setRangeAxis(wInCmAxisIndex, wInCmAxis);
        combineYBounds(new DoubleBounds(dataRange), wInCmAxisIndex);
    }

    private static Range inCm(Range r, double pnpValue) {
        double l = r.getLowerBound();
        double u = r.getUpperBound();
        l = (l - pnpValue)*100d;
        u = (u - pnpValue)*100d;
        return new Range(l, u);
    }

    private NumberAxis createWinCMAxis(int wAxisIndex) {

        Font labelFont = new Font(
            DEFAULT_FONT_NAME,
            Font.BOLD,
            getYAxisFontSize(wAxisIndex));

        String axisName = "W.in.cm";
        String axisLabel = Resources.getMsg(context.getMeta(),
            I18N_AXIS_LABEL, "W [cm]");

        IdentifiableNumberAxis axis = new IdentifiableNumberAxis(
            axisName, axisLabel);

        axis.setAutoRangeIncludesZero(false);
        axis.setLabelFont(labelFont);
        axis.setTickLabelFont(labelFont);

        return axis;
    }

     /** We need to override this to keep both axis synced. */
    @Override
    protected void autoZoom(XYPlot plot) {
        super.autoZoom(plot);
        IdentifiableNumberAxis idA = (IdentifiableNumberAxis) plot.getRangeAxis(wAxisIndex);
        Range fixedRange = getRangeForAxisFromSettings(idA.getId());
        if (fixedRange == null) {
            return;
        }
        log.debug("Adjusting helper centimeter axis to fixed range.");
        Range adjustedRange = inCm(
            fixedRange,
            pnpValue
        );
        IdentifiableNumberAxis wInCmAxis=
            (IdentifiableNumberAxis) plot.getRangeAxis(wInCmAxisIndex);
        wInCmAxis.setRange(adjustedRange);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org