SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CustomHistogramDataset.java
1
2package umontreal.ssj.charts;
3
4import java.util.*;
5import org.jfree.data.general.DatasetChangeEvent;
6import org.jfree.data.statistics.HistogramBin;
7import org.jfree.data.statistics.HistogramType;
8import org.jfree.data.xy.AbstractIntervalXYDataset;
9import org.jfree.data.xy.IntervalXYDataset;
10import org.jfree.util.ObjectUtilities;
11import org.jfree.util.PublicCloneable;
12
13/*
14 * Class: CustomHistogramDataset
15 * Description:
16 * Environment: Java
17 * Software: SSJ
18 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
19 * Organization: DIRO, Universite de Montreal
20 * @author
21 * @since
22 *
23 *
24 * Licensed under the Apache License, Version 2.0 (the "License");
25 * you may not use this file except in compliance with the License.
26 * You may obtain a copy of the License at
27 *
28 * http://www.apache.org/licenses/LICENSE-2.0
29 *
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
35 *
36 */
37
44public class CustomHistogramDataset extends AbstractIntervalXYDataset
45 implements IntervalXYDataset, Cloneable, PublicCloneable {
46
48 private List<Map> list;
49
51 public HistogramType type;
52
58 list = new ArrayList<Map>();
59 type = HistogramType.FREQUENCY;
60 }
61
65 public HistogramType getType() {
66 return type;
67 }
68
75 public void setType(HistogramType type) {
76 if (type == null) {
77 throw new IllegalArgumentException("Null 'type' argument");
78 } else {
79 this.type = type;
80 notifyListeners(new DatasetChangeEvent(this, this));
81 return;
82 }
83 }
84
92 public void addSeries(Comparable key, double values[], int bins) {
93 double minimum = getMinimum(values);
94 double maximum = getMaximum(values);
95 addSeries(key, values, bins, minimum, maximum);
96 }
97
106 public void addSeries(Comparable key, double values[], int numPoints, int bins) {
107 double minimum = getMinimum(values);
108 double maximum = getMaximum(values);
109 addSeries(key, values, numPoints, bins, minimum, maximum);
110 }
111
124 public void addSeries(Comparable key, double values[], int bins, double minimum, double maximum) {
125 addSeries(key, values, values.length, bins, minimum, maximum);
126 }
127
142 public void addSeries(Comparable key, double values[], int numPoints, int bins, double minimum, double maximum) {
143 if (key == null)
144 throw new IllegalArgumentException("Null 'key' argument.");
145 if (values == null)
146 throw new IllegalArgumentException("Null 'values' argument.");
147 if (bins < 1)
148 throw new IllegalArgumentException("The 'bins' value must be at least 1.");
149 double binWidth = (maximum - minimum) / (double) bins;
150 double lower = minimum;
151 final double EPS = 1.0e-15;
152 List<HistogramBin> binList = new ArrayList<HistogramBin>(bins);
153 for (int i = 0; i < bins; i++) {
154 HistogramBin bin;
155 if (i == bins - 1) {
156 bin = new HistogramBin(lower, maximum * (1.0 + EPS));
157 } else {
158 double upper = minimum + (double) (i + 1) * binWidth;
159 bin = new HistogramBin(lower, upper);
160 lower = upper;
161 }
162 binList.add(bin);
163 }
164
165 ArrayList<Double> valuesList = new ArrayList<Double>(numPoints);
166 for (int i = 0; i < numPoints; i++)
167 valuesList.add(Double.valueOf(values[i]));
168
169 synchronizeValuesAndBins(binList, valuesList);
170 Map map = new HashMap();
171 map.put("key", key);
172 map.put("values", valuesList);
173 map.put("bins", binList);
174 map.put("numPoints", Integer.valueOf(numPoints));
175 map.put("bin width", Double.valueOf(binWidth));
176 list.add(map);
177 }
178
187 public void addSeries(Comparable key, double values[], HistogramBin bins[]) {
188 addSeries(key, values, values.length, bins);
189 }
190
200 public void addSeries(Comparable key, double values[], int numPoints, HistogramBin bins[]) {
201 if (key == null)
202 throw new IllegalArgumentException("Null 'key' argument.");
203 if (values == null)
204 throw new IllegalArgumentException("Null 'values' argument.");
205 if (bins == null || bins.length < 2)
206 throw new IllegalArgumentException(
207 "The 'bins' table must contain at least 1 org.jfree.data.statistics.HistogramBin.");
208 List binList = new ArrayList(bins.length);
209 for (int i = 0; i < bins.length; i++)
210 binList.add(bins[i]);
211
212 ArrayList valuesList = new ArrayList(numPoints);
213 for (int i = 0; i < numPoints; i++)
214 valuesList.add(Double.valueOf(values[i]));
215
216 synchronizeValuesAndBins(binList, valuesList);
217 Map map = new HashMap();
218 map.put("key", key);
219 map.put("values", valuesList);
220 map.put("bins", binList);
221 map.put("numPoints", Integer.valueOf(numPoints));
222 map.put("bin width", Double.valueOf(-1D));
223 list.add(map);
224 }
225
234 private double getMinimum(double values[]) {
235 if (values == null || values.length < 1)
236 throw new IllegalArgumentException("Null or zero length 'values' argument.");
237 double min = 1.7E+308D;
238 for (int i = 0; i < values.length; i++)
239 if (values[i] < min)
240 min = values[i];
241
242 return min;
243 }
244
253 private double getMaximum(double values[]) {
254 if (values == null || values.length < 1)
255 throw new IllegalArgumentException("Null or zero length 'values' argument.");
256 double max = -1.7E+308D;
257 for (int i = 0; i < values.length; i++)
258 if (values[i] > max)
259 max = values[i];
260
261 return max;
262 }
263
275 public List getBins(int series) {
276 Map map = (Map) list.get(series);
277 return (List) map.get("bins");
278 }
279
290 public void setBins(int series, int bins) {
291 double minimum = getMinimum(getValues(series));
292 double maximum = getMaximum(getValues(series));
293 setBins(series, bins, minimum, maximum);
294 }
295
308 public void setBins(int series, int bins, double minimum, double maximum) {
309 Map map = (Map) list.get(series);
310 List currentValues = (List) map.get("values");
311 double binWidth = (maximum - minimum) / (double) bins;
312 double lower = minimum;
313 List binList = new ArrayList(bins);
314 double EPS = 1.0e-15;
315 for (int i = 0; i < bins; i++) {
316 HistogramBin bin;
317 if (i == bins - 1) {
318 bin = new HistogramBin(lower, maximum * (1.0 + EPS));
319 } else {
320 double upper = minimum + (double) (i + 1) * binWidth;
321 bin = new HistogramBin(lower, upper);
322 lower = upper;
323 }
324 binList.add(bin);
325 }
326
327 synchronizeValuesAndBins(binList, currentValues);
328 map.put("values", currentValues);
329 map.put("bins", binList);
330 }
331
342 public void setBins(int series, HistogramBin bins[]) {
343 Map map = (Map) list.get(series);
344 List currentValues = (List) map.get("values");
345 ArrayList binList = new ArrayList(bins.length);
346 for (int i = 0; i < bins.length; i++)
347 binList.add(bins[i]);
348
349 synchronizeValuesAndBins(binList, currentValues);
350 map.put("values", currentValues);
351 map.put("bins", binList);
352 }
353
365 public List getValuesList(int series) {
366 Map map = (Map) list.get(series);
367 return (List) map.get("values");
368 }
369
381 public double[] getValues(int series) {
382 List valuesList = (List) ((Map) list.get(series)).get("values");
383 ListIterator iter = valuesList.listIterator();
384 double retour[] = new double[valuesList.size()];
385 for (int i = 0; iter.hasNext(); i++)
386 retour[i] = ((Double) iter.next()).doubleValue();
387
388 return retour;
389 }
390
401 public void setValues(int series, List valuesList) {
402 Map map = (Map) list.get(series);
403 List currentBins = (List) map.get("bins");
404 synchronizeValuesAndBins(currentBins, valuesList);
405 map.put("values", valuesList);
406 map.put("bins", currentBins);
407 }
408
419 public void setValues(int series, double values[]) {
420 ArrayList valuesList = new ArrayList(values.length);
421 for (int i = 0; i < values.length; i++)
422 valuesList.add(new Double(values[i]));
423
424 setValues(series, ((List) (valuesList)));
425 }
426
433 private void synchronizeValuesAndBins(List bins, List values) {
434 ListIterator iterBins = bins.listIterator(0);
435 ListIterator iterValues = values.listIterator();
436 HistogramBin bin;
437 for (; iterBins.hasNext(); iterBins.set(new HistogramBin(bin.getStartBoundary(), bin.getEndBoundary())))
438 bin = (HistogramBin) iterBins.next();
439
440 iterBins = bins.listIterator(0);
441 while (iterValues.hasNext()) {
442 double currentValue = ((Double) iterValues.next()).doubleValue();
443 boolean continu = true;
444 iterBins = bins.listIterator(0);
445 while (continu && iterBins.hasNext()) {
446 HistogramBin tempBin = (HistogramBin) iterBins.next();
447 if (currentValue >= tempBin.getStartBoundary() && currentValue < tempBin.getEndBoundary()) {
448 tempBin.incrementCount();
449 continu = false;
450 }
451 }
452 }
453 }
454
462 public int getTotal(int series) {
463 Map map = (Map) list.get(series);
464 return ((Integer) map.get("numPoints")).intValue();
465 }
466
474 public double getBinWidth(int series) {
475 Map map = (Map) list.get(series);
476 return ((Double) map.get("bin width")).doubleValue();
477 }
478
484 public int getSeriesCount() {
485 return list.size();
486 }
487
499 public Comparable getSeriesKey(int series) {
500 Map map = (Map) list.get(series);
501 return (Comparable) map.get("key");
502 }
503
515 public int getItemCount(int series) {
516 return getBins(series).size();
517 }
518
533 public Number getX(int series, int item) {
534 List bins = getBins(series);
535 HistogramBin bin = (HistogramBin) bins.get(item);
536 double x = (bin.getStartBoundary() + bin.getEndBoundary()) / 2D;
537 return new Double(x);
538 }
539
553 public Number getY(int series, int item) {
554 List bins = getBins(series);
555 HistogramBin bin = (HistogramBin) bins.get(item);
556 double total = getTotal(series);
557 double binWidth = getBinWidth(series);
558 if (type == HistogramType.FREQUENCY)
559 return new Double(bin.getCount());
560 if (type == HistogramType.RELATIVE_FREQUENCY)
561 return new Double((double) bin.getCount() / total);
562 if (type == HistogramType.SCALE_AREA_TO_1)
563 return new Double((double) bin.getCount() / (binWidth * total));
564 else
565 throw new IllegalStateException();
566 }
567
580 public Number getStartX(int series, int item) {
581 List bins = getBins(series);
582 HistogramBin bin = (HistogramBin) bins.get(item);
583 return new Double(bin.getStartBoundary());
584 }
585
598 public Number getEndX(int series, int item) {
599 List bins = getBins(series);
600 HistogramBin bin = (HistogramBin) bins.get(item);
601 return new Double(bin.getEndBoundary());
602 }
603
618 public Number getStartY(int series, int item) {
619 return getY(series, item);
620 }
621
636 public Number getEndY(int series, int item) {
637 return getY(series, item);
638 }
639
647 public boolean equals(Object obj) {
648 if (obj == this)
649 return true;
650 if (!(obj instanceof CustomHistogramDataset))
651 return false;
653 if (!ObjectUtilities.equal(type, that.type))
654 return false;
655 return ObjectUtilities.equal(list, that.list);
656 }
657
665 public Object clone() throws CloneNotSupportedException {
666 return super.clone();
667 }
668}
void addSeries(Comparable key, double values[], int numPoints, int bins, double minimum, double maximum)
Adds a series to the dataset.
void addSeries(Comparable key, double values[], int bins)
Adds a series to the dataset, using the specified number of bins.
int getSeriesCount()
Returns the number of series in the dataset.
void addSeries(Comparable key, double values[], HistogramBin bins[])
Adds a series to the dataset.
void setBins(int series, int bins, double minimum, double maximum)
Sets the bins for a series.
double[] getValues(int series)
Returns the values for a series.
Number getY(int series, int item)
Returns the y-value for a bin (calculated to take into account the histogram type).
double getBinWidth(int series)
Returns the bin width for a series.
void setType(HistogramType type)
Sets the histogram type and sends a DatasetChangeEvent to all registered listeners.
Object clone()
Returns a clone of the dataset.
void addSeries(Comparable key, double values[], int numPoints, int bins)
Adds a series to the dataset, using the specified number of bins.
Number getStartY(int series, int item)
Returns the start y-value for a bin (which is the same as the y-value).
Number getEndY(int series, int item)
Returns the end y-value for a bin (which is the same as the y-value).
boolean equals(Object obj)
Tests this dataset for equality with an arbitrary object.
List getBins(int series)
Returns the bins for a series.
Comparable getSeriesKey(int series)
Returns the key for a series.
List getValuesList(int series)
Returns the values for a series.
void setBins(int series, int bins)
Sets the bins for a series.
HistogramType getType()
Returns the histogram type.
void addSeries(Comparable key, double values[], int bins, double minimum, double maximum)
Adds a series to the dataset.
int getItemCount(int series)
Returns the number of data items for a series.
Number getEndX(int series, int item)
Returns the end value for a bin.
Number getStartX(int series, int item)
Returns the start value for a bin.
int getTotal(int series)
Returns the total number of observations for a series.
void addSeries(Comparable key, double values[], int numPoints, HistogramBin bins[])
Adds a series to the dataset.
void setValues(int series, List valuesList)
Sets the values for a series.
Number getX(int series, int item)
Returns the X value for a bin.
CustomHistogramDataset()
Creates a new (empty) dataset with a default type of HistogramType.FREQUENCY.
void setValues(int series, double values[])
Sets the values for a series.
void setBins(int series, HistogramBin bins[])
Sets the bins for a series.