Source for org.jfree.data.DataUtilities

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 05-Mar-2003 : Version 1 (DG);
  38:  * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
  39:  *               from the DatasetUtilities class (DG);
  40:  * 17-May-2005 : Added calculateColumnTotal() and calculateRowTotal() 
  41:  *               methods (DG);
  42:  * 
  43:  */
  44: 
  45: package org.jfree.data;
  46: 
  47: import org.jfree.data.general.DatasetUtilities;
  48: 
  49: /**
  50:  * Utility methods for use with some of the data classes (but not the datasets, 
  51:  * see {@link DatasetUtilities}).
  52:  */
  53: public abstract class DataUtilities {
  54: 
  55:     /**
  56:      * Returns the total of the values in one column of the supplied data
  57:      * table.
  58:      * 
  59:      * @param data  the table of values (<code>null</code> not permitted).
  60:      * @param column  the column index (zero-based).
  61:      * 
  62:      * @return The total of the values in the specified column.
  63:      */
  64:     public static double calculateColumnTotal(Values2D data, int column) {
  65:         double total = 0.0;
  66:         int rowCount = data.getRowCount();
  67:         for (int r = 0; r < rowCount; r++) {
  68:             Number n = data.getValue(r, column);
  69:             if (n != null) {
  70:                 total += n.doubleValue();   
  71:             }
  72:         }
  73:         return total;
  74:     }
  75:     
  76:     /**
  77:      * Returns the total of the values in one row of the supplied data
  78:      * table.
  79:      * 
  80:      * @param data  the table of values (<code>null</code> not permitted).
  81:      * @param row  the row index (zero-based).
  82:      * 
  83:      * @return The total of the values in the specified row.
  84:      */
  85:     public static double calculateRowTotal(Values2D data, int row) {
  86:         double total = 0.0;
  87:         int columnCount = data.getColumnCount();
  88:         for (int c = 0; c < columnCount; c++) {
  89:             Number n = data.getValue(row, c);
  90:             if (n != null) {
  91:                 total += n.doubleValue();   
  92:             }
  93:         }
  94:         return total;
  95:     }
  96:     
  97:     /**
  98:      * Constructs an array of <code>Number</code> objects from an array of 
  99:      * <code>double</code> primitives.
 100:      *
 101:      * @param data  the data (<code>null</code> not permitted).
 102:      *
 103:      * @return An array of <code>Double</code>.
 104:      */
 105:     public static Number[] createNumberArray(double[] data) {
 106:         if (data == null) {
 107:             throw new IllegalArgumentException("Null 'data' argument.");   
 108:         }
 109:         Number[] result = new Number[data.length];
 110:         for (int i = 0; i < data.length; i++) {
 111:             result[i] = new Double(data[i]);
 112:         }
 113:         return result;
 114:     }
 115: 
 116:     /**
 117:      * Constructs an array of arrays of <code>Number</code> objects from a 
 118:      * corresponding structure containing <code>double</code> primitives.
 119:      *
 120:      * @param data  the data (<code>null</code> not permitted).
 121:      *
 122:      * @return An array of <code>Double</code>.
 123:      */
 124:     public static Number[][] createNumberArray2D(double[][] data) {
 125:         if (data == null) {
 126:             throw new IllegalArgumentException("Null 'data' argument.");   
 127:         }
 128:         int l1 = data.length;
 129:         Number[][] result = new Number[l1][];
 130:         for (int i = 0; i < l1; i++) {
 131:             result[i] = createNumberArray(data[i]);
 132:         }
 133:         return result;
 134:     }
 135: 
 136:     /**
 137:      * Returns a {@link KeyedValues} instance that contains the cumulative 
 138:      * percentage values for the data in another {@link KeyedValues} instance.
 139:      * <p>
 140:      * The percentages are values between 0.0 and 1.0 (where 1.0 = 100%).
 141:      *
 142:      * @param data  the data (<code>null</code> not permitted).
 143:      *
 144:      * @return The cumulative percentages.
 145:      */
 146:     public static KeyedValues getCumulativePercentages(KeyedValues data) {
 147:         if (data == null) {
 148:             throw new IllegalArgumentException("Null 'data' argument.");   
 149:         }
 150:         DefaultKeyedValues result = new DefaultKeyedValues();
 151:         double total = 0.0;
 152:         for (int i = 0; i < data.getItemCount(); i++) {
 153:             Number v = data.getValue(i);
 154:             if (v != null) {
 155:                 total = total + v.doubleValue();
 156:             }
 157:         }
 158:         double runningTotal = 0.0;
 159:         for (int i = 0; i < data.getItemCount(); i++) {
 160:             Number v = data.getValue(i);
 161:             if (v != null) {
 162:                 runningTotal = runningTotal + v.doubleValue();
 163:             }
 164:             result.addValue(data.getKey(i), new Double(runningTotal / total));
 165:         }
 166:         return result;
 167:     }
 168: 
 169: }