    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ------------------
    028     * DataUtilities.java
    029     * ------------------
    030     * (C) Copyright 2003-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: DataUtilities.java,v 1.2.2.1 2005/10/25 21:29:13 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 05-Mar-2003 : Version 1 (DG);
    040     * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
    041     *               from the DatasetUtilities class (DG);
    042     * 17-May-2005 : Added calculateColumnTotal() and calculateRowTotal()
    043     *               methods (DG);
    044     *
    045     */
    046
    047    package org.jfree.data;
    048
    049    import org.jfree.data.general.DatasetUtilities;
    050
    051    /**
    052     * Utility methods for use with some of the data classes (but not the datasets,
    053     * see {@link DatasetUtilities}).
    054     */
    055    public abstract class DataUtilities {
    056
    057        /**
    058         * Returns the total of the values in one column of the supplied data
    059         * table.
    060         *
    061         * @param data  the table of values (<code>null</code> not permitted).
    062         * @param column  the column index (zero-based).
    063         *
    064         * @return The total of the values in the specified column.
    065         */
    066        public static double calculateColumnTotal(Values2D data, int column) {
    067            double total = 0.0;
    068            int rowCount = data.getRowCount();
    069            for (int r = 0; r < rowCount; r++) {
    070                Number n = data.getValue(r, column);
    071                if (n != null) {
    072                    total += n.doubleValue();
    073                }
    074            }
    075            return total;
    076        }
    077
    078        /**
    079         * Returns the total of the values in one row of the supplied data
    080         * table.
    081         *
    082         * @param data  the table of values (<code>null</code> not permitted).
    083         * @param row  the row index (zero-based).
    084         *
    085         * @return The total of the values in the specified row.
    086         */
    087        public static double calculateRowTotal(Values2D data, int row) {
    088            double total = 0.0;
    089            int columnCount = data.getColumnCount();
    090            for (int c = 0; c < columnCount; c++) {
    091                Number n = data.getValue(row, c);
    092                if (n != null) {
    093                    total += n.doubleValue();
    094                }
    095            }
    096            return total;
    097        }
    098
    099        /**
    100         * Constructs an array of <code>Number</code> objects from an array of
    101         * <code>double</code> primitives.
    102         *
    103         * @param data  the data (<code>null</code> not permitted).
    104         *
    105         * @return An array of <code>Double</code>.
    106         */
    107        public static Number[] createNumberArray(double[] data) {
    108            if (data == null) {
    109                throw new IllegalArgumentException("Null 'data' argument.");
    110            }
    111            Number[] result = new Number[data.length];
    112            for (int i = 0; i < data.length; i++) {
    113                result[i] = new Double(data[i]);
    114            }
    115            return result;
    116        }
    117
    118        /**
    119         * Constructs an array of arrays of <code>Number</code> objects from a
    120         * corresponding structure containing <code>double</code> primitives.
    121         *
    122         * @param data  the data (<code>null</code> not permitted).
    123         *
    124         * @return An array of <code>Double</code>.
    125         */
    126        public static Number[][] createNumberArray2D(double[][] data) {
    127            if (data == null) {
    128                throw new IllegalArgumentException("Null 'data' argument.");
    129            }
    130            int l1 = data.length;
    131            Number[][] result = new Number[l1][];
    132            for (int i = 0; i < l1; i++) {
    133                result[i] = createNumberArray(data[i]);
    134            }
    135            return result;
    136        }
    137
    138        /**
    139         * Returns a {@link KeyedValues} instance that contains the cumulative
    140         * percentage values for the data in another {@link KeyedValues} instance.
    141         * <p>
    142         * The percentages are values between 0.0 and 1.0 (where 1.0 = 100%).
    143         *
    144         * @param data  the data (<code>null</code> not permitted).
    145         *
    146         * @return The cumulative percentages.
    147         */
    148        public static KeyedValues getCumulativePercentages(KeyedValues data) {
    149            if (data == null) {
    150                throw new IllegalArgumentException("Null 'data' argument.");
    151            }
    152            DefaultKeyedValues result = new DefaultKeyedValues();
    153            double total = 0.0;
    154            for (int i = 0; i < data.getItemCount(); i++) {
    155                Number v = data.getValue(i);
    156                if (v != null) {
    157                    total = total + v.doubleValue();
    158                }
    159            }
    160            double runningTotal = 0.0;
    161            for (int i = 0; i < data.getItemCount(); i++) {
    162                Number v = data.getValue(i);
    163                if (v != null) {
    164                    runningTotal = runningTotal + v.doubleValue();
    165                }
    166                result.addValue(data.getKey(i), new Double(runningTotal / total));
    167            }
    168            return result;
    169        }
    170
    171    }