view artifacts/src/main/java/org/dive4elements/river/jfree/AxisDataset.java @ 9555:ef5754ba5573

Implemented legend aggregation based on type of themes. Added theme-editor style configuration for aggregated legend entries. Only configured themes get aggregated.
author gernotbelger
date Tue, 23 Oct 2018 16:26:48 +0200
parents d8e753d0fdb9
children
line wrap: on
line source
/* Copyright (C) 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.jfree;

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

import org.jfree.data.Range;
import org.jfree.data.RangeInfo;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 * Axis datasets.
 */
public class AxisDataset {

    /** List of assigned datasets (in order). */
    private final List<XYDataset> datasets = new ArrayList<>();

    /** Range to use to include all given datasets. */
    private Range range;

    private boolean rangeDirty;

    /** Add a dataset to internal list for this axis. */
    public void addDataset(final XYDataset dataset) {
        this.datasets.add(dataset);
        this.rangeDirty = true;
    }

    public void setRange(final Range val) {
        this.range = val;
    }

    /** Get Range for the range axis of this dataset. */
    public Range getRange() {
        if (this.range != null && !this.rangeDirty) {
            return this.range;
        }
        /* Calculate the min / max of all series */
        for (final XYDataset dataset : this.datasets) {
            Range newRange = null;
            if (dataset instanceof StyledAreaSeriesCollection) {
                final StyledAreaSeriesCollection areaSeries = (StyledAreaSeriesCollection) dataset;
                if (areaSeries.shouldCalculateRange())
                    newRange = areaSeries.getRangeBounds(false);
                else {
                    /*
                     * For most area themes, we do not include areas in the range calculation because
                     * they are used with very large / small values to draw areas
                     * with axis boundaries
                     */
                    continue;
                }
            } else if (dataset instanceof RangeInfo) {
                /* The usual case for most series */
                newRange = ((RangeInfo) dataset).getRangeBounds(false);
            } else if (dataset instanceof TimeSeriesCollection) {
                /* Lalala <3 Jfreechart's class hirarchy */
                newRange = ((TimeSeriesCollection) dataset).getRangeBounds(false);
            }

            /* Now we only expand as we also only add new data */
            if (this.range == null) {
                this.range = newRange;
            } else {
                this.range = Range.combine(this.range, newRange);
            }
        }
        this.rangeDirty = false;
        return this.range;
    }

    /** Get Array of Datasets. */
    public XYDataset[] getDatasets() {
        return this.datasets.toArray(new XYDataset[this.datasets.size()]);
    }

    /** True if no datasets given. */
    public boolean isEmpty() {
        return this.datasets.isEmpty();
    }
}

http://dive4elements.wald.intevation.org