1 45 46 package org.jfree.data; 47 48 import org.jfree.data.general.DatasetUtilities; 49 50 54 public abstract class DataUtilities { 55 56 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 n = data.getValue(r, column); 70 if (n != null) { 71 total += n.doubleValue(); 72 } 73 } 74 return total; 75 } 76 77 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 n = data.getValue(row, c); 91 if (n != null) { 92 total += n.doubleValue(); 93 } 94 } 95 return total; 96 } 97 98 106 public static Number [] createNumberArray(double[] data) { 107 if (data == null) { 108 throw new IllegalArgumentException ("Null 'data' argument."); 109 } 110 Number [] result = new Number [data.length]; 111 for (int i = 0; i < data.length; i++) { 112 result[i] = new Double (data[i]); 113 } 114 return result; 115 } 116 117 125 public static Number [][] createNumberArray2D(double[][] data) { 126 if (data == null) { 127 throw new IllegalArgumentException ("Null 'data' argument."); 128 } 129 int l1 = data.length; 130 Number [][] result = new Number [l1][]; 131 for (int i = 0; i < l1; i++) { 132 result[i] = createNumberArray(data[i]); 133 } 134 return result; 135 } 136 137 147 public static KeyedValues getCumulativePercentages(KeyedValues data) { 148 if (data == null) { 149 throw new IllegalArgumentException ("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 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 v = data.getValue(i); 162 if (v != null) { 163 runningTotal = runningTotal + v.doubleValue(); 164 } 165 result.addValue(data.getKey(i), new Double (runningTotal / total)); 166 } 167 return result; 168 } 169 170 } 171 | Popular Tags |