view gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/CompactXYItems.java @ 1087:92fce3b3d07f

Centered histograms in pdf exports. gnv-artifacts/trunk@1189 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 10 Jun 2010 09:23:33 +0000
parents 05bf8534a35a
children f953c9a559d8
line wrap: on
line source
package de.intevation.gnv.jfreechart;

import java.io.Serializable;

/**
 * This class is used to represent geometries (e.g. point, line, polygon). Each
 * geometry is made up of multiple xy points stored in a single array. A line
 * composed by start- and endpoint is stored in the following order in that
 * array: [x1, y1, x2, y2].
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha Teichmann</a>
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class CompactXYItems
implements   Serializable
{
    /**
     * Array storing the xy items.
     */
    protected double [] data;

    /**
     * Constructs a new CompactXYItems object with the given data.
     *
     * @param data An array with xy values.
     */
    public CompactXYItems(double [] data) {
        this.data = data;
    }

    /**
     * Retrieves the x coordinate of the point with the given index.
     *
     * @param index Index
     * @return X coordinate.
     */
    public double getX(int index) {
        return data[index << 1];
    }

    /**
     * Retrieves the y coordinate of the point with the given index.
     *
     * @param index Index
     * @return Y coordinate.
     */
    public double getY(int index) {
        return data[(index << 1)+1];
    }

    /**
     * Write the tupel of xy-values at a specific index into the given array.
     *
     * @param index Index used to specify the xy-value.
     * @param xy the xy coordinate is written into this array with the following
     * order: [x,y]
     */
    public void get(int index, double [] xy) {
        xy[0] = data[index = (index << 1) + 1];
        xy[1] = data[index + 1];
    }

    /**
     *
     * @return the data array.
     */
    public double [] getData() {
        return data;
    }

    /**
     *
     * @param data
     */
    public void setData(double [] data) {
        this.data = data;
    }

    /**
     *
     * @return the number of data points.
     */
    public int size() {
        return data.length >> 1;
    }

    /**
     * Retrieves the bounding box spaned by the coordinates in the data array.
     *
     * @param bbox
     * @return the calculated bounding box.
     */
    public double [] calculateBoundingBox(double [] bbox)  {
        for (int i = 0; i < data.length;) {
            double x = data[i++];
            double y = data[i++];
            if (x < bbox[0]) bbox[0] = x;
            if (y < bbox[1]) bbox[1] = y;
            if (x > bbox[2]) bbox[2] = x;
            if (y > bbox[3]) bbox[3] = y;
        }
        return bbox;
    }

    /**
     *
     * @return the coordinates as string.
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length;) {
            if (i > 0) sb.append("; ");
            sb.append('(');
            sb.append(data[i++]);
            sb.append(", ");
            sb.append(data[i++]);
            sb.append(')');
        }
        return sb.toString();
    }


    /**
     *
     * @return the lowest x value.
     */
    public double getMinX() {
        double lower = Double.POSITIVE_INFINITY;

        for (int i = 0; i < data.length; i += 2) {
            double x = data[i];

            if (!Double.isNaN(x)) {
                lower = Math.min(lower, x);
            }
        }

        return lower;
    }


    /**
     *
     * @return the highest x value.
     */
    public double getMaxX() {
        double upper = Double.NEGATIVE_INFINITY;

        for (int i = 0; i < data.length; i += 2) {
            double x = data[i];

            if (!Double.isNaN(x)) {
                upper = Math.max(upper, x);
            }
        }

        return upper;
    }


    /**
     *
     * @return the lowest y value.
     */
    public double getMinY() {
        double lower = Double.POSITIVE_INFINITY;

        for (int i = 1; i < data.length; i += 2) {
            double y = data[i];

            if (!Double.isNaN(y)) {
                lower = Math.min(lower, y);
            }
        }

        return lower;
    }


    /**
     *
     * @return the highest y value.
     */
    public double getMaxY() {
        double upper = Double.NEGATIVE_INFINITY;

        for (int i = 1; i < data.length; i += 2) {
            double y = data[i];

            if (!Double.isNaN(y)) {
                upper = Math.max(upper, y);
            }
        }

        return upper;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org