view artifacts/src/main/java/org/dive4elements/river/exports/StyledSeriesBuilder.java @ 6140:60b94dec104b

Add handling of bound artifacts. When an artifact is bound to an out its facets will only be shown in that Out. They will be removed in the blackboard pass and marked as incompatible by the AttributeWriter
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 31 May 2013 15:29:30 +0200
parents af13ceeba52a
children 128686fa5add
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 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 org.apache.log4j.Logger;

import org.jfree.data.xy.XYSeries;

import org.dive4elements.river.artifacts.model.WKms;
import org.dive4elements.river.artifacts.model.WQKms;
import org.dive4elements.river.artifacts.model.WWQQ;

/**
 * Helper to create and modify StyledXYSeries.
 */
public class StyledSeriesBuilder {

    /**
     * JFreeChart and the area calculation will fail if we use Double.INFINITY
     * or Double.MAX_VALUE (probably because these are really used in
     * calculations). We define and use a more handy value instead.
     */
    final static double BIG_DOUBLE_VALUE = 1234567d;

    private static final Logger logger = Logger.getLogger
        (StyledSeriesBuilder.class);


    /**
     * Trivial, hidden constructor.
     */
    private StyledSeriesBuilder() {
    }


    /**
     * Add points to series.
     *
     * @param series Series to add points to.
     * @param points Points to add to series, points[0] to 1st dim, points[1]
     *               to 2nd dim.
     * @param skipNANs if true, skip NAN values in points parameter.
     */
    public static void addPoints(XYSeries series, double[][] points, boolean skipNANs) {
        if (points == null || points.length <= 1) {
            return;
        }
        double [] xPoints = points[0];
        double [] yPoints = points[1];
        for (int i = 0; i < xPoints.length; i++) {
            if (skipNANs &&
                (Double.isNaN(xPoints[i]) || Double.isNaN(yPoints[i]))) {
                logger.warn ("Skipping NaN in StyledSeriesBuilder.");
                continue;
            }
            series.add(xPoints[i], yPoints[i], false);
        }
    }


    /**
     * Add points to series (km to 1st dim, w to 2nd dim).
     *
     * @param series Series to add points to.
     * @param wkms WKms to add to series.
     */
    public static void addPoints(XYSeries series, WKms wkms) {
        if (wkms == null) {
            return;
        }

        int size = wkms.size();

        for (int i = 0; i < size; i++) {
            series.add(wkms.getKm(i), wkms.getW(i), false);
        }
    }


    /**
     * Add points to dataset with an offset (shift all points by given amount).
     * @param series series to add data to.
     * @param wkms WKms of which the Ws will be shifted.
     * @param off the offset.
     */
    public static void addUpperBand(XYSeries series, WKms wkms, double off) {
        if (wkms == null) {
            return;
        }

        int size = wkms.size();

        for (int i = 0; i < size; i++) {
            series.add(wkms.getKm(i), wkms.getW(i)+off, false);
        }
    }


    /**
     * Add points to dataset with an offset (shift all points 'down' by given
     * amount).
     * @param series series to add data to.
     * @param wkms WKms of which the Ws will be shifted.
     * @param off the offset.
     */
    public static void addLowerBand(XYSeries series, WKms wkms, double off) {
        addUpperBand(series, wkms, -off);
    }


    /**
     * Add points to series (km to 1st dim, q to 2nd dim).
     *
     * @param series Series to add points to.
     * @param wqkms WQKms to add to series.
     */
    public static void addPointsKmQ(XYSeries series, WQKms wqkms) {
        if (wqkms == null) {
            return;
        }

        int size = wqkms.size();

        for (int i = 0; i < size; i++) {
            series.add(wqkms.getKm(i), wqkms.getQ(i), false);
        }
    }


    /**
     * Add points to series (km to 1st dim, q to 2nd dim), adding points
     * to achieve a step-like curve.
     *
     * @param series Series to add points to.
     * @param wqkms WQKms to add to series.
     */
    public static void addStepPointsKmQ(XYSeries series, WQKms wqkms) {
        if (wqkms == null) {
            return;
        }

        int size = wqkms.size();

        for (int i = 0; i < size; i++) {
            if (i==0) {
                series.add(wqkms.getKm(i), wqkms.getQ(i), false);
            }
            else {
                //Add two points.
                double halveX = (wqkms.getKm(i-1) + wqkms.getKm(i)) / 2d;
                series.add(halveX, wqkms.getQ(i-1), false);
                series.add(halveX, wqkms.getQ(i), false);
            }
            if (i == size-1) {
                series.add(wqkms.getKm(i), wqkms.getQ(i), false);
            }
        }
    }


    /**
     * Add points to series (q to 1st dim, w to 2nd dim).
     *
     * @param series Series to add points to.
     * @param wqkms WQKms to add to series.
     */
    public static void addPointsQW(XYSeries series, WQKms wqkms) {
        if (wqkms == null) {
            return;
        }

        int size = wqkms.size();

        for (int i = 0; i < size; i++) {
            series.add(wqkms.getQ(i), wqkms.getW(i), false);
        }
    }


    /**
     * Add points to series (q to 1st dim, w to 2nd dim).
     *
     * @param series Series to add points to.
     * @param qs the Qs to add, assumed same length than ws.
     * @param ws the Ws to add, assumed same length than qs.
     */
    public static void addPointsQW(XYSeries series, double[] qs, double ws[]) {
        if (ws == null || qs == null) {
            return;
        }

        int size = qs.length;

        for (int i = 0; i < size; i++) {
            series.add(qs[i], ws[i], false);
        }
    }


    /**
     * Add points to series (q to 1st dim, w to 2nd dim).
     *
     * @param series Series to add points to.
     * @param wwqq WWQQ to add to series.
     */
    public static void addPoints(XYSeries series, WWQQ wwqq) {
        if (wwqq == null) {
            return;
        }

        int size = wwqq.size();

        for (int i = 0; i < size; i++) {
            series.add(wwqq.getW1(i), wwqq.getW2(i), false);
        }
    }


    /**
     * Create a Series such that an infinitely big area can be filled
     * between the newly created and the given series.
     */
    public static XYSeries createGroundAtInfinity(XYSeries series) {
        XYSeries ground = new XYSeries(series.getKey() + /** TODO rand + */ "INF");
        ground.add(series.getMinX(), -BIG_DOUBLE_VALUE);
        ground.add(series.getMaxX(), -BIG_DOUBLE_VALUE);
        return ground;
    }


    /**
     * Create a Series such that an infinitely big area can be filled
     * between the newly created and the given series.
     */
    public static XYSeries createCeilingAtInfinity(XYSeries series) {
        XYSeries ground = new XYSeries(series.getKey() + /** TODO rand + */ "INF");
        ground.add(series.getMinX(), BIG_DOUBLE_VALUE);
        ground.add(series.getMaxX(), BIG_DOUBLE_VALUE);
        return ground;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org