Source for org.jfree.data.DataUtilities

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------
  28:  * DataUtilities.java
  29:  * ------------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: DataUtilities.java,v 1.2.2.1 2005/10/25 21:29:13 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 05-Mar-2003 : Version 1 (DG);
  40:  * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
  41:  *               from the DatasetUtilities class (DG);
  42:  * 17-May-2005 : Added calculateColumnTotal() and calculateRowTotal() 
  43:  *               methods (DG);
  44:  * 
  45:  */
  46: 
  47: package org.jfree.data;
  48: 
  49: import org.jfree.data.general.DatasetUtilities;
  50: 
  51: /**
  52:  * Utility methods for use with some of the data classes (but not the datasets, 
  53:  * see {@link DatasetUtilities}).
  54:  */
  55: public abstract class DataUtilities {
  56: 
  57:     /**
  58:      * Returns the total of the values in one column of the supplied data
  59:      * table.
  60:      * 
  61:      * @param data  the table of values (<code>null</code> not permitted).
  62:      * @param column  the column index (zero-based).
  63:      * 
  64:      * @return The total of the values in the specified column.
  65:      */
  66:     public static double calculateColumnTotal(Values2D data, int column) {
  67:         double total = 0.0;
  68:         int rowCount = data.getRowCount();
  69:         for (int r = 0; r < rowCount; r++) {
  70:             Number n = data.getValue(r, column);
  71:             if (n != null) {
  72:                 total += n.doubleValue();   
  73:             }
  74:         }
  75:         return total;
  76:     }
  77:     
  78:     /**
  79:      * Returns the total of the values in one row of the supplied data
  80:      * table.
  81:      * 
  82:      * @param data  the table of values (<code>null</code> not permitted).
  83:      * @param row  the row index (zero-based).
  84:      * 
  85:      * @return The total of the values in the specified row.
  86:      */
  87:     public static double calculateRowTotal(Values2D data, int row) {
  88:         double total = 0.0;
  89:         int columnCount = data.getColumnCount();
  90:         for (int c = 0; c < columnCount; c++) {
  91:             Number n = data.getValue(row, c);
  92:             if (n != null) {
  93:                 total += n.doubleValue();   
  94:             }
  95:         }
  96:         return total;
  97:     }
  98:     
  99:     /**
 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: }