1 39 40 package org.jfree.data; 41 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.List ; 45 import java.util.Map ; 46 47 54 public class HistogramDataset extends AbstractDataset implements IntervalXYDataset { 55 56 57 public final static HistogramType FREQUENCY = new HistogramType(); 58 59 60 public static HistogramType RELATIVE_FREQUENCY = new HistogramType(); 61 62 63 public static HistogramType SCALE_AREA_TO_1 = new HistogramType(); 64 65 66 private List list = new ArrayList (); 67 68 69 private HistogramType type = FREQUENCY; 70 71 76 public void setType(HistogramType type) { 77 if (type == null) { 78 throw new IllegalArgumentException ( 79 "HistogramDataset.setType(...): null not permitted."); 80 } 81 this.type = type; 82 } 83 84 89 public HistogramType getType() { 90 return type; 91 } 92 93 100 public void addSeries(String name, double[] values, int numberOfBins) { 101 if (values == null) { 102 throw new IllegalArgumentException ( 103 "HistogramDataset.addSeries(...): 'values' argument must not be null." 104 ); 105 } 106 else if (numberOfBins < 1) { 107 throw new IllegalArgumentException ( 108 "HistogramDataset.addSeries(...): number of bins must be at least 1" 109 ); 110 } 111 double minimum = getMinimum(values); 113 double maximum = getMaximum(values); 114 double binWidth = (maximum - minimum) / numberOfBins; 115 double tmp = minimum; 117 HistogramBin[] bins = new HistogramBin[numberOfBins]; 118 for (int i = 0; i < bins.length; i++) { 119 HistogramBin bin = new HistogramBin(tmp, tmp + binWidth); 120 tmp = tmp + binWidth; 121 bins[i] = bin; 122 } 123 for (int i = 0; i < values.length; i++) { 125 for (int j = 0; j < bins.length; j++) { 126 if (values[i] >= bins[j].getStartBoundary() 127 && values[i] <= bins[j].getEndBoundary()) { 128 bins[j].incrementCount(); 130 break; } 132 } 133 } 134 Map map = new HashMap (); 136 map.put("name", name); 137 map.put("bins", bins); 138 map.put("values.length", new Integer (values.length)); 139 map.put("bin width", new Double (binWidth)); 140 list.add(map); 141 } 142 143 150 private double getMinimum(double[] values) { 151 if (values == null || values.length < 1) { 152 throw new IllegalArgumentException (); 153 } 154 155 double min = Double.MAX_VALUE; 156 for (int i = 0; i < values.length; i++) { 157 if (values[i] < min) { 158 min = values[i]; 159 } 160 } 161 return min; 162 } 163 164 171 private double getMaximum(double[] values) { 172 if (values == null || values.length < 1) { 173 throw new IllegalArgumentException (); 174 } 175 176 double max = -Double.MAX_VALUE; 177 for (int i = 0; i < values.length; i++) { 178 if (values[i] > max) { 179 max = values[i]; 180 } 181 } 182 return max; 183 } 184 185 192 HistogramBin[] getBins(int series) { 193 Map map = (Map ) list.get(series); 194 return (HistogramBin[]) map.get("bins"); 195 } 196 197 204 private int getTotal(int series) { 205 Map map = (Map ) list.get(series); 206 return ((Integer ) map.get("values.length")).intValue(); 207 } 208 209 216 private double getBinWidth(int series) { 217 Map map = (Map ) list.get(series); 218 return ((Double ) map.get("bin width")).doubleValue(); 219 } 220 221 226 public int getSeriesCount() { 227 return list.size(); 228 } 229 230 237 public String getSeriesName(int series) { 238 Map map = (Map ) list.get(series); 239 return (String ) map.get("name"); 240 } 241 242 249 public int getItemCount(int series) { 250 return getBins(series).length; 251 } 252 253 265 public Number getXValue(int series, int item) { 266 HistogramBin[] bins = getBins(series); 267 HistogramBin bin = bins[item]; 268 double x = (bin.getStartBoundary() + bin.getEndBoundary()) / 2.; 269 return new Double (x); 270 } 271 272 280 public Number getYValue(int series, int item) { 281 HistogramBin[] bins = getBins(series); 282 double total = getTotal(series); 283 double binWidth = getBinWidth(series); 284 285 if (type == FREQUENCY) { 286 return new Double (bins[item].getCount()); 287 } 288 else if (type == RELATIVE_FREQUENCY) { 289 return new Double (bins[item].getCount() / total); 290 } 291 else if (type == SCALE_AREA_TO_1) { 292 return new Double (bins[item].getCount() / (binWidth * total)); 293 } 294 else { throw new IllegalStateException (); 296 } 297 } 298 299 307 public Number getStartXValue(int series, int item) { 308 HistogramBin[] bins = getBins(series); 309 return new Double (bins[item].getStartBoundary()); 310 } 311 312 320 public Number getEndXValue(int series, int item) { 321 HistogramBin[] bins = getBins(series); 322 return new Double (bins[item].getEndBoundary()); 323 } 324 325 333 public Number getStartYValue(int series, int item) { 334 return getYValue(series, item); 336 } 337 338 346 public Number getEndYValue(int series, int item) { 347 return getYValue(series, item); 349 } 350 351 355 private static class HistogramType { 356 359 private HistogramType() { 360 } 361 } 362 363 } 364 | Popular Tags |