KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------
27  * DataUtilities.java
28  * ------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DataUtilities.java,v 1.2 2005/05/17 12:24:00 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 05-Mar-2003 : Version 1 (DG);
39  * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
40  * from the DatasetUtilities class (DG);
41  * 17-May-2005 : Added calculateColumnTotal() and calculateRowTotal()
42  * methods (DG);
43  *
44  */

45
46 package org.jfree.data;
47
48 import org.jfree.data.general.DatasetUtilities;
49
50 /**
51  * Utility methods for use with some of the data classes (but not the datasets,
52  * see {@link DatasetUtilities}).
53  */

54 public abstract class DataUtilities {
55
56     /**
57      * Returns the total of the values in one column of the supplied data
58      * table.
59      *
60      * @param data the table of values (<code>null</code> not permitted).
61      * @param column the column index (zero-based).
62      *
63      * @return The total of the values in the specified column.
64      */

65     public static double calculateColumnTotal(Values2D data, int column) {
66         double total = 0.0;
67         int rowCount = data.getRowCount();
68         for (int r = 0; r < rowCount; r++) {
69             Number JavaDoc n = data.getValue(r, column);
70             if (n != null) {
71                 total += n.doubleValue();
72             }
73         }
74         return total;
75     }
76     
77     /**
78      * Returns the total of the values in one row of the supplied data
79      * table.
80      *
81      * @param data the table of values (<code>null</code> not permitted).
82      * @param row the row index (zero-based).
83      *
84      * @return The total of the values in the specified row.
85      */

86     public static double calculateRowTotal(Values2D data, int row) {
87         double total = 0.0;
88         int columnCount = data.getColumnCount();
89         for (int c = 0; c < columnCount; c++) {
90             Number JavaDoc n = data.getValue(row, c);
91             if (n != null) {
92                 total += n.doubleValue();
93             }
94         }
95         return total;
96     }
97     
98     /**
99      * Constructs an array of <code>Number</code> objects from an array of
100      * <code>double</code> primitives.
101      *
102      * @param data the data (<code>null</code> not permitted).
103      *
104      * @return An array of <code>Double</code>.
105      */

106     public static Number JavaDoc[] createNumberArray(double[] data) {
107         if (data == null) {
108             throw new IllegalArgumentException JavaDoc("Null 'data' argument.");
109         }
110         Number JavaDoc[] result = new Number JavaDoc[data.length];
111         for (int i = 0; i < data.length; i++) {
112             result[i] = new Double JavaDoc(data[i]);
113         }
114         return result;
115     }
116
117     /**
118      * Constructs an array of arrays of <code>Number</code> objects from a
119      * corresponding structure containing <code>double</code> primitives.
120      *
121      * @param data the data (<code>null</code> not permitted).
122      *
123      * @return An array of <code>Double</code>.
124      */

125     public static Number JavaDoc[][] createNumberArray2D(double[][] data) {
126         if (data == null) {
127             throw new IllegalArgumentException JavaDoc("Null 'data' argument.");
128         }
129         int l1 = data.length;
130         Number JavaDoc[][] result = new Number JavaDoc[l1][];
131         for (int i = 0; i < l1; i++) {
132             result[i] = createNumberArray(data[i]);
133         }
134         return result;
135     }
136
137     /**
138      * Returns a {@link KeyedValues} instance that contains the cumulative
139      * percentage values for the data in another {@link KeyedValues} instance.
140      * <p>
141      * The percentages are values between 0.0 and 1.0 (where 1.0 = 100%).
142      *
143      * @param data the data (<code>null</code> not permitted).
144      *
145      * @return The cumulative percentages.
146      */

147     public static KeyedValues getCumulativePercentages(KeyedValues data) {
148         if (data == null) {
149             throw new IllegalArgumentException JavaDoc("Null 'data' argument.");
150         }
151         DefaultKeyedValues result = new DefaultKeyedValues();
152         double total = 0.0;
153         for (int i = 0; i < data.getItemCount(); i++) {
154             Number JavaDoc v = data.getValue(i);
155             if (v != null) {
156                 total = total + v.doubleValue();
157             }
158         }
159         double runningTotal = 0.0;
160         for (int i = 0; i < data.getItemCount(); i++) {
161             Number JavaDoc v = data.getValue(i);
162             if (v != null) {
163                 runningTotal = runningTotal + v.doubleValue();
164             }
165             result.addValue(data.getKey(i), new Double JavaDoc(runningTotal / total));
166         }
167         return result;
168     }
169
170 }
171
Popular Tags