001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 05-Mar-2003 : Version 1 (DG);
038 * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
039 * from the DatasetUtilities class (DG);
040 * 17-May-2005 : Added calculateColumnTotal() and calculateRowTotal()
041 * methods (DG);
042 *
043 */
044
045 package org.jfree.data;
046
047 import org.jfree.data.general.DatasetUtilities;
048
049 /**
050 * Utility methods for use with some of the data classes (but not the datasets,
051 * see {@link DatasetUtilities}).
052 */
053 public abstract class DataUtilities {
054
055 /**
056 * Returns the total of the values in one column of the supplied data
057 * table.
058 *
059 * @param data the table of values (<code>null</code> not permitted).
060 * @param column the column index (zero-based).
061 *
062 * @return The total of the values in the specified column.
063 */
064 public static double calculateColumnTotal(Values2D data, int column) {
065 double total = 0.0;
066 int rowCount = data.getRowCount();
067 for (int r = 0; r < rowCount; r++) {
068 Number n = data.getValue(r, column);
069 if (n != null) {
070 total += n.doubleValue();
071 }
072 }
073 return total;
074 }
075
076 /**
077 * Returns the total of the values in one row of the supplied data
078 * table.
079 *
080 * @param data the table of values (<code>null</code> not permitted).
081 * @param row the row index (zero-based).
082 *
083 * @return The total of the values in the specified row.
084 */
085 public static double calculateRowTotal(Values2D data, int row) {
086 double total = 0.0;
087 int columnCount = data.getColumnCount();
088 for (int c = 0; c < columnCount; c++) {
089 Number n = data.getValue(row, c);
090 if (n != null) {
091 total += n.doubleValue();
092 }
093 }
094 return total;
095 }
096
097 /**
098 * Constructs an array of <code>Number</code> objects from an array of
099 * <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 }