KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > general > DatasetUtilities


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------------
28  * DatasetUtilities.java
29  * ---------------------
30  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Andrzej Porebski (bug fix);
34  * Jonathan Nash (bug fix);
35  * Richard Atkinson;
36  * Andreas Schroeder (beatification)
37  *
38  * $Id: DatasetUtilities.java,v 1.18.2.4 2006/08/02 15:27:14 mungady Exp $
39  *
40  * Changes (from 18-Sep-2001)
41  * --------------------------
42  * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
43  * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
44  * 15-Nov-2001 : Moved to package com.jrefinery.data.* in the JCommon class
45  * library (DG);
46  * Changed to handle null values from datasets (DG);
47  * Bug fix (thanks to Andrzej Porebski) - initial value now set
48  * to positive or negative infinity when iterating (DG);
49  * 22-Nov-2001 : Datasets with containing no data now return null for min and
50  * max calculations (DG);
51  * 13-Dec-2001 : Extended to handle HighLowDataset and IntervalXYDataset (DG);
52  * 15-Feb-2002 : Added getMinimumStackedRangeValue() and
53  * getMaximumStackedRangeValue() (DG);
54  * 28-Feb-2002 : Renamed Datasets.java --> DatasetUtilities.java (DG);
55  * 18-Mar-2002 : Fixed bug in min/max domain calculation for datasets that
56  * implement the CategoryDataset interface AND the XYDataset
57  * interface at the same time. Thanks to Jonathan Nash for the
58  * fix (DG);
59  * 23-Apr-2002 : Added getDomainExtent() and getRangeExtent() methods (DG);
60  * 13-Jun-2002 : Modified range measurements to handle
61  * IntervalCategoryDataset (DG);
62  * 12-Jul-2002 : Method name change in DomainInfo interface (DG);
63  * 30-Jul-2002 : Added pie dataset summation method (DG);
64  * 01-Oct-2002 : Added a method for constructing an XYDataset from a Function2D
65  * instance (DG);
66  * 24-Oct-2002 : Amendments required following changes to the CategoryDataset
67  * interface (DG);
68  * 18-Nov-2002 : Changed CategoryDataset to TableDataset (DG);
69  * 04-Mar-2003 : Added isEmpty(XYDataset) method (DG);
70  * 05-Mar-2003 : Added a method for creating a CategoryDataset from a
71  * KeyedValues instance (DG);
72  * 15-May-2003 : Renamed isEmpty --> isEmptyOrNull (DG);
73  * 25-Jun-2003 : Added limitPieDataset methods (RA);
74  * 26-Jun-2003 : Modified getDomainExtent() method to accept null datasets (DG);
75  * 27-Jul-2003 : Added getStackedRangeExtent(TableXYDataset data) (RA);
76  * 18-Aug-2003 : getStackedRangeExtent(TableXYDataset data) now handles null
77  * values (RA);
78  * 02-Sep-2003 : Added method to check for null or empty PieDataset (DG);
79  * 18-Sep-2003 : Fix for bug 803660 (getMaximumRangeValue for
80  * CategoryDataset) (DG);
81  * 20-Oct-2003 : Added getCumulativeRangeExtent() method (DG);
82  * 09-Jan-2003 : Added argument checking code to the createCategoryDataset()
83  * method (DG);
84  * 23-Mar-2004 : Fixed bug in getMaximumStackedRangeValue() method (DG);
85  * 31-Mar-2004 : Exposed the extent iteration algorithms to use one of them and
86  * applied noninstantiation pattern (AS);
87  * 11-May-2004 : Renamed getPieDatasetTotal --> calculatePieDatasetTotal (DG);
88  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue();
89  * 24-Aug-2004 : Added argument checks to createCategoryDataset() method (DG);
90  * 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
91  * 06-Oct-2004 : Renamed findDomainExtent() --> findDomainBounds(),
92  * findRangeExtent() --> findRangeBounds() (DG);
93  * 07-Jan-2005 : Renamed findStackedRangeExtent() --> findStackedRangeBounds(),
94  * findCumulativeRangeExtent() --> findCumulativeRangeBounds(),
95  * iterateXYRangeExtent() --> iterateXYRangeBounds(),
96  * removed deprecated methods (DG);
97  * 03-Feb-2005 : The findStackedRangeBounds() methods now return null for
98  * empty datasets (DG);
99  * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods
100  * from DatasetUtilities --> DataUtilities (DG);
101  * 22-Sep-2005 : Added new findStackedRangeBounds() method that takes base
102  * argument (DG);
103  *
104  */

105
106 package org.jfree.data.general;
107
108 import java.util.ArrayList JavaDoc;
109 import java.util.Iterator JavaDoc;
110 import java.util.List JavaDoc;
111
112 import org.jfree.data.DomainInfo;
113 import org.jfree.data.KeyToGroupMap;
114 import org.jfree.data.KeyedValues;
115 import org.jfree.data.Range;
116 import org.jfree.data.RangeInfo;
117 import org.jfree.data.category.CategoryDataset;
118 import org.jfree.data.category.DefaultCategoryDataset;
119 import org.jfree.data.category.IntervalCategoryDataset;
120 import org.jfree.data.function.Function2D;
121 import org.jfree.data.xy.OHLCDataset;
122 import org.jfree.data.xy.IntervalXYDataset;
123 import org.jfree.data.xy.TableXYDataset;
124 import org.jfree.data.xy.XYDataset;
125 import org.jfree.data.xy.XYSeries;
126 import org.jfree.data.xy.XYSeriesCollection;
127 import org.jfree.util.ArrayUtilities;
128
129 /**
130  * A collection of useful static methods relating to datasets.
131  */

132 public final class DatasetUtilities {
133     
134     /**
135      * Private constructor for non-instanceability.
136      */

137     private DatasetUtilities() {
138         // now try to instantiate this ;-)
139
}
140
141     /**
142      * Calculates the total of all the values in a {@link PieDataset}. If
143      * the dataset contains negative or <code>null</code> values, they are
144      * ignored.
145      *
146      * @param dataset the dataset (<code>null</code> not permitted).
147      *
148      * @return The total.
149      */

150     public static double calculatePieDatasetTotal(PieDataset dataset) {
151         if (dataset == null) {
152             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
153         }
154         List JavaDoc keys = dataset.getKeys();
155         double totalValue = 0;
156         Iterator JavaDoc iterator = keys.iterator();
157         while (iterator.hasNext()) {
158             Comparable JavaDoc current = (Comparable JavaDoc) iterator.next();
159             if (current != null) {
160                 Number JavaDoc value = dataset.getValue(current);
161                 double v = 0.0;
162                 if (value != null) {
163                     v = value.doubleValue();
164                 }
165                 if (v > 0) {
166                     totalValue = totalValue + v;
167                 }
168             }
169         }
170         return totalValue;
171     }
172
173     /**
174      * Creates a pie dataset from a table dataset by taking all the values
175      * for a single row.
176      *
177      * @param dataset the dataset (<code>null</code> not permitted).
178      * @param rowKey the row key.
179      *
180      * @return A pie dataset.
181      */

182     public static PieDataset createPieDatasetForRow(CategoryDataset dataset,
183                                                     Comparable JavaDoc rowKey) {
184         int row = dataset.getRowIndex(rowKey);
185         return createPieDatasetForRow(dataset, row);
186     }
187
188     /**
189      * Creates a pie dataset from a table dataset by taking all the values
190      * for a single row.
191      *
192      * @param dataset the dataset (<code>null</code> not permitted).
193      * @param row the row (zero-based index).
194      *
195      * @return A pie dataset.
196      */

197     public static PieDataset createPieDatasetForRow(CategoryDataset dataset,
198                                                     int row) {
199         DefaultPieDataset result = new DefaultPieDataset();
200         int columnCount = dataset.getColumnCount();
201         for (int current = 0; current < columnCount; current++) {
202             Comparable JavaDoc columnKey = dataset.getColumnKey(current);
203             result.setValue(columnKey, dataset.getValue(row, current));
204         }
205         return result;
206     }
207
208     /**
209      * Creates a pie dataset from a table dataset by taking all the values
210      * for a single column.
211      *
212      * @param dataset the dataset (<code>null</code> not permitted).
213      * @param columnKey the column key.
214      *
215      * @return A pie dataset.
216      */

217     public static PieDataset createPieDatasetForColumn(CategoryDataset dataset,
218                                                        Comparable JavaDoc columnKey) {
219         int column = dataset.getColumnIndex(columnKey);
220         return createPieDatasetForColumn(dataset, column);
221     }
222
223     /**
224      * Creates a pie dataset from a {@link CategoryDataset} by taking all the
225      * values for a single column.
226      *
227      * @param dataset the dataset (<code>null</code> not permitted).
228      * @param column the column (zero-based index).
229      *
230      * @return A pie dataset.
231      */

232     public static PieDataset createPieDatasetForColumn(CategoryDataset dataset,
233                                                        int column) {
234         DefaultPieDataset result = new DefaultPieDataset();
235         int rowCount = dataset.getRowCount();
236         for (int i = 0; i < rowCount; i++) {
237             Comparable JavaDoc rowKey = dataset.getRowKey(i);
238             result.setValue(rowKey, dataset.getValue(i, column));
239         }
240         return result;
241     }
242
243     /**
244      * Creates a new pie dataset based on the supplied dataset, but modified
245      * by aggregating all the low value items (those whose value is lower
246      * than the <code>percentThreshold</code>) into a single item with the
247      * key "Other".
248      *
249      * @param source the source dataset (<code>null</code> not permitted).
250      * @param key a new key for the aggregated items (<code>null</code> not
251      * permitted).
252      * @param minimumPercent the percent threshold.
253      *
254      * @return The pie dataset with (possibly) aggregated items.
255      */

256     public static PieDataset createConsolidatedPieDataset(PieDataset source,
257                                                           Comparable JavaDoc key,
258                                                           double minimumPercent)
259     {
260         return DatasetUtilities.createConsolidatedPieDataset(
261             source, key, minimumPercent, 2
262         );
263     }
264
265     /**
266      * Creates a new pie dataset based on the supplied dataset, but modified
267      * by aggregating all the low value items (those whose value is lower
268      * than the <code>percentThreshold</code>) into a single item. The
269      * aggregated items are assigned the specified key. Aggregation only
270      * occurs if there are at least <code>minItems</code> items to aggregate.
271      *
272      * @param source the source dataset (<code>null</code> not permitted).
273      * @param key the key to represent the aggregated items.
274      * @param minimumPercent the percent threshold (ten percent is 0.10).
275      * @param minItems only aggregate low values if there are at least this
276      * many.
277      *
278      * @return The pie dataset with (possibly) aggregated items.
279      */

280     public static PieDataset createConsolidatedPieDataset(PieDataset source,
281                                                           Comparable JavaDoc key,
282                                                           double minimumPercent,
283                                                           int minItems) {
284         
285         DefaultPieDataset result = new DefaultPieDataset();
286         double total = DatasetUtilities.calculatePieDatasetTotal(source);
287
288         // Iterate and find all keys below threshold percentThreshold
289
List JavaDoc keys = source.getKeys();
290         ArrayList JavaDoc otherKeys = new ArrayList JavaDoc();
291         Iterator JavaDoc iterator = keys.iterator();
292         while (iterator.hasNext()) {
293             Comparable JavaDoc currentKey = (Comparable JavaDoc) iterator.next();
294             Number JavaDoc dataValue = source.getValue(currentKey);
295             if (dataValue != null) {
296                 double value = dataValue.doubleValue();
297                 if (value / total < minimumPercent) {
298                     otherKeys.add(currentKey);
299                 }
300             }
301         }
302
303         // Create new dataset with keys above threshold percentThreshold
304
iterator = keys.iterator();
305         double otherValue = 0;
306         while (iterator.hasNext()) {
307             Comparable JavaDoc currentKey = (Comparable JavaDoc) iterator.next();
308             Number JavaDoc dataValue = source.getValue(currentKey);
309             if (dataValue != null) {
310                 if (otherKeys.contains(currentKey)
311                     && otherKeys.size() >= minItems) {
312                     // Do not add key to dataset
313
otherValue += dataValue.doubleValue();
314                 }
315                 else {
316                     // Add key to dataset
317
result.setValue(currentKey, dataValue);
318                 }
319             }
320         }
321         // Add other category if applicable
322
if (otherKeys.size() >= minItems) {
323             result.setValue(key, otherValue);
324         }
325         return result;
326     }
327
328     /**
329      * Creates a {@link CategoryDataset} that contains a copy of the data in an
330      * array (instances of <code>Double</code> are created to represent the
331      * data items).
332      * <p>
333      * Row and column keys are created by appending 0, 1, 2, ... to the
334      * supplied prefixes.
335      *
336      * @param rowKeyPrefix the row key prefix.
337      * @param columnKeyPrefix the column key prefix.
338      * @param data the data.
339      *
340      * @return The dataset.
341      */

342     public static CategoryDataset createCategoryDataset(String JavaDoc rowKeyPrefix,
343                                                         String JavaDoc columnKeyPrefix,
344                                                         double[][] data) {
345
346         DefaultCategoryDataset result = new DefaultCategoryDataset();
347         for (int r = 0; r < data.length; r++) {
348             String JavaDoc rowKey = rowKeyPrefix + (r + 1);
349             for (int c = 0; c < data[r].length; c++) {
350                 String JavaDoc columnKey = columnKeyPrefix + (c + 1);
351                 result.addValue(new Double JavaDoc(data[r][c]), rowKey, columnKey);
352             }
353         }
354         return result;
355
356     }
357
358     /**
359      * Creates a {@link CategoryDataset} that contains a copy of the data in
360      * an array.
361      * <p>
362      * Row and column keys are created by appending 0, 1, 2, ... to the
363      * supplied prefixes.
364      *
365      * @param rowKeyPrefix the row key prefix.
366      * @param columnKeyPrefix the column key prefix.
367      * @param data the data.
368      *
369      * @return The dataset.
370      */

371     public static CategoryDataset createCategoryDataset(String JavaDoc rowKeyPrefix,
372                                                         String JavaDoc columnKeyPrefix,
373                                                         Number JavaDoc[][] data) {
374
375         DefaultCategoryDataset result = new DefaultCategoryDataset();
376         for (int r = 0; r < data.length; r++) {
377             String JavaDoc rowKey = rowKeyPrefix + (r + 1);
378             for (int c = 0; c < data[r].length; c++) {
379                 String JavaDoc columnKey = columnKeyPrefix + (c + 1);
380                 result.addValue(data[r][c], rowKey, columnKey);
381             }
382         }
383         return result;
384
385     }
386
387     /**
388      * Creates a {@link CategoryDataset} that contains a copy of the data in
389      * an array (instances of <code>Double</code> are created to represent the
390      * data items).
391      * <p>
392      * Row and column keys are taken from the supplied arrays.
393      *
394      * @param rowKeys the row keys (<code>null</code> not permitted).
395      * @param columnKeys the column keys (<code>null</code> not permitted).
396      * @param data the data.
397      *
398      * @return The dataset.
399      */

400     public static CategoryDataset createCategoryDataset(Comparable JavaDoc[] rowKeys,
401                                                         Comparable JavaDoc[] columnKeys,
402                                                         double[][] data) {
403
404         // check arguments...
405
if (rowKeys == null) {
406             throw new IllegalArgumentException JavaDoc("Null 'rowKeys' argument.");
407         }
408         if (columnKeys == null) {
409             throw new IllegalArgumentException JavaDoc("Null 'columnKeys' argument.");
410         }
411         if (ArrayUtilities.hasDuplicateItems(rowKeys)) {
412             throw new IllegalArgumentException JavaDoc("Duplicate items in 'rowKeys'.");
413         }
414         if (ArrayUtilities.hasDuplicateItems(columnKeys)) {
415             throw new IllegalArgumentException JavaDoc(
416                 "Duplicate items in 'columnKeys'."
417             );
418         }
419         if (rowKeys.length != data.length) {
420             throw new IllegalArgumentException JavaDoc(
421                 "The number of row keys does not match the number of rows in "
422                 + "the data array."
423             );
424         }
425         int columnCount = 0;
426         for (int r = 0; r < data.length; r++) {
427             columnCount = Math.max(columnCount, data[r].length);
428         }
429         if (columnKeys.length != columnCount) {
430             throw new IllegalArgumentException JavaDoc(
431                 "The number of column keys does not match the number of "
432                 + "columns in the data array."
433             );
434         }
435         
436         // now do the work...
437
DefaultCategoryDataset result = new DefaultCategoryDataset();
438         for (int r = 0; r < data.length; r++) {
439             Comparable JavaDoc rowKey = rowKeys[r];
440             for (int c = 0; c < data[r].length; c++) {
441                 Comparable JavaDoc columnKey = columnKeys[c];
442                 result.addValue(new Double JavaDoc(data[r][c]), rowKey, columnKey);
443             }
444         }
445         return result;
446
447     }
448
449     /**
450      * Creates a {@link CategoryDataset} by copying the data from the supplied
451      * {@link KeyedValues} instance.
452      *
453      * @param rowKey the row key (<code>null</code> not permitted).
454      * @param rowData the row data (<code>null</code> not permitted).
455      *
456      * @return A dataset.
457      */

458     public static CategoryDataset createCategoryDataset(Comparable JavaDoc rowKey,
459                                                         KeyedValues rowData) {
460
461         if (rowKey == null) {
462             throw new IllegalArgumentException JavaDoc("Null 'rowKey' argument.");
463         }
464         if (rowData == null) {
465             throw new IllegalArgumentException JavaDoc("Null 'rowData' argument.");
466         }
467         DefaultCategoryDataset result = new DefaultCategoryDataset();
468         for (int i = 0; i < rowData.getItemCount(); i++) {
469             result.addValue(rowData.getValue(i), rowKey, rowData.getKey(i));
470         }
471         return result;
472
473     }
474
475     /**
476      * Creates an {@link XYDataset} by sampling the specified function over a
477      * fixed range.
478      *
479      * @param f the function (<code>null</code> not permitted).
480      * @param start the start value for the range.
481      * @param end the end value for the range.
482      * @param samples the number of sample points (must be > 1).
483      * @param seriesKey the key to give the resulting series
484      * (<code>null</code> not permitted).
485      *
486      * @return A dataset.
487      */

488     public static XYDataset sampleFunction2D(Function2D f,
489                                              double start,
490                                              double end,
491                                              int samples,
492                                              Comparable JavaDoc seriesKey) {
493
494         if (f == null) {
495             throw new IllegalArgumentException JavaDoc("Null 'f' argument.");
496         }
497         if (seriesKey == null) {
498             throw new IllegalArgumentException JavaDoc("Null 'seriesKey' argument.");
499         }
500         if (start >= end) {
501             throw new IllegalArgumentException JavaDoc("Requires 'start' < 'end'.");
502         }
503         if (samples < 2) {
504             throw new IllegalArgumentException JavaDoc("Requires 'samples' > 1");
505         }
506
507         XYSeries series = new XYSeries(seriesKey);
508         double step = (end - start) / samples;
509         for (int i = 0; i <= samples; i++) {
510             double x = start + (step * i);
511             series.add(x, f.getValue(x));
512         }
513         XYSeriesCollection collection = new XYSeriesCollection(series);
514         return collection;
515
516     }
517
518     /**
519      * Returns <code>true</code> if the dataset is empty (or <code>null</code>),
520      * and <code>false</code> otherwise.
521      *
522      * @param dataset the dataset (<code>null</code> permitted).
523      *
524      * @return A boolean.
525      */

526     public static boolean isEmptyOrNull(PieDataset dataset) {
527
528         if (dataset == null) {
529             return true;
530         }
531
532         int itemCount = dataset.getItemCount();
533         if (itemCount == 0) {
534             return true;
535         }
536
537         for (int item = 0; item < itemCount; item++) {
538             Number JavaDoc y = dataset.getValue(item);
539             if (y != null) {
540                 double yy = y.doubleValue();
541                 if (yy > 0.0) {
542                     return false;
543                 }
544             }
545         }
546
547         return true;
548
549     }
550
551     /**
552      * Returns <code>true</code> if the dataset is empty (or <code>null</code>),
553      * and <code>false</code> otherwise.
554      *
555      * @param dataset the dataset (<code>null</code> permitted).
556      *
557      * @return A boolean.
558      */

559     public static boolean isEmptyOrNull(CategoryDataset dataset) {
560
561         if (dataset == null) {
562             return true;
563         }
564
565         int rowCount = dataset.getRowCount();
566         int columnCount = dataset.getColumnCount();
567         if (rowCount == 0 || columnCount == 0) {
568             return true;
569         }
570
571         for (int r = 0; r < rowCount; r++) {
572             for (int c = 0; c < columnCount; c++) {
573                 if (dataset.getValue(r, c) != null) {
574                     return false;
575                 }
576
577             }
578         }
579
580         return true;
581
582     }
583
584     /**
585      * Returns <code>true</code> if the dataset is empty (or <code>null</code>),
586      * and <code>false</code> otherwise.
587      *
588      * @param dataset the dataset (<code>null</code> permitted).
589      *
590      * @return A boolean.
591      */

592     public static boolean isEmptyOrNull(XYDataset dataset) {
593
594         boolean result = true;
595
596         if (dataset != null) {
597             for (int s = 0; s < dataset.getSeriesCount(); s++) {
598                 if (dataset.getItemCount(s) > 0) {
599                     result = false;
600                     continue;
601                 }
602             }
603         }
604
605         return result;
606
607     }
608
609     /**
610      * Returns the range of values in the domain (x-values) of a dataset.
611      *
612      * @param dataset the dataset (<code>null</code> not permitted).
613      *
614      * @return The range of values (possibly <code>null</code>).
615      */

616     public static Range findDomainBounds(XYDataset dataset) {
617         return findDomainBounds(dataset, true);
618     }
619
620     /**
621      * Returns the range of values in the domain (x-values) of a dataset.
622      *
623      * @param dataset the dataset (<code>null</code> not permitted).
624      * @param includeInterval determines whether or not the x-interval is taken
625      * into account (only applies if the dataset is an
626      * {@link IntervalXYDataset}).
627      *
628      * @return The range of values (possibly <code>null</code>).
629      */

630     public static Range findDomainBounds(XYDataset dataset,
631                                          boolean includeInterval) {
632
633         if (dataset == null) {
634             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
635         }
636
637         Range result = null;
638         // if the dataset implements DomainInfo, life is easier
639
if (dataset instanceof DomainInfo) {
640             DomainInfo info = (DomainInfo) dataset;
641             result = info.getDomainBounds(includeInterval);
642         }
643         else {
644             result = iterateDomainBounds(dataset, includeInterval);
645         }
646         return result;
647         
648     }
649
650     /**
651      * Iterates over the items in an {@link XYDataset} to find
652      * the range of x-values.
653      *
654      * @param dataset the dataset (<code>null</code> not permitted).
655      *
656      * @return The range (possibly <code>null</code>).
657      */

658     public static Range iterateDomainBounds(XYDataset dataset) {
659         return iterateDomainBounds(dataset, true);
660     }
661
662     /**
663      * Iterates over the items in an {@link XYDataset} to find
664      * the range of x-values.
665      *
666      * @param dataset the dataset (<code>null</code> not permitted).
667      * @param includeInterval a flag that determines, for an IntervalXYDataset,
668      * whether the x-interval or just the x-value is
669      * used to determine the overall range.
670      *
671      * @return The range (possibly <code>null</code>).
672      */

673     public static Range iterateDomainBounds(XYDataset dataset,
674                                             boolean includeInterval) {
675         if (dataset == null) {
676             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
677         }
678         double minimum = Double.POSITIVE_INFINITY;
679         double maximum = Double.NEGATIVE_INFINITY;
680         int seriesCount = dataset.getSeriesCount();
681         double lvalue;
682         double uvalue;
683         if (includeInterval && dataset instanceof IntervalXYDataset) {
684             IntervalXYDataset intervalXYData = (IntervalXYDataset) dataset;
685             for (int series = 0; series < seriesCount; series++) {
686                 int itemCount = dataset.getItemCount(series);
687                 for (int item = 0; item < itemCount; item++) {
688                     lvalue = intervalXYData.getStartXValue(series, item);
689                     uvalue = intervalXYData.getEndXValue(series, item);
690                     minimum = Math.min(minimum, lvalue);
691                     maximum = Math.max(maximum, uvalue);
692                 }
693             }
694         }
695         else {
696             for (int series = 0; series < seriesCount; series++) {
697                 int itemCount = dataset.getItemCount(series);
698                 for (int item = 0; item < itemCount; item++) {
699                     lvalue = dataset.getXValue(series, item);
700                     uvalue = lvalue;
701                     minimum = Math.min(minimum, lvalue);
702                     maximum = Math.max(maximum, uvalue);
703                 }
704             }
705         }
706         if (minimum > maximum) {
707             return null;
708         }
709         else {
710             return new Range(minimum, maximum);
711         }
712     }
713     
714     /**
715      * Returns the range of values in the range for the dataset. This method
716      * is the partner for the getDomainExtent method.
717      *
718      * @param dataset the dataset (<code>null</code> not permitted).
719      *
720      * @return The range (possibly <code>null</code>).
721      */

722     public static Range findRangeBounds(CategoryDataset dataset) {
723         return findRangeBounds(dataset, true);
724     }
725     
726     /**
727      * Returns the range of values in the range for the dataset. This method
728      * is the partner for the getDomainExtent method.
729      *
730      * @param dataset the dataset (<code>null</code> not permitted).
731      * @param includeInterval a flag that determines whether or not the
732      * y-interval is taken into account.
733      *
734      * @return The range (possibly <code>null</code>).
735      */

736     public static Range findRangeBounds(CategoryDataset dataset,
737                                         boolean includeInterval) {
738         if (dataset == null) {
739             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
740         }
741         Range result = null;
742         if (dataset instanceof RangeInfo) {
743             RangeInfo info = (RangeInfo) dataset;
744             result = info.getRangeBounds(includeInterval);
745         }
746         else {
747             result = iterateCategoryRangeBounds(dataset, includeInterval);
748         }
749         return result;
750     }
751     
752     /**
753      * Returns the range of values in the range for the dataset. This method
754      * is the partner for the {@link #findDomainBounds(XYDataset)} method.
755      *
756      * @param dataset the dataset (<code>null</code> not permitted).
757      *
758      * @return The range (possibly <code>null</code>).
759      */

760     public static Range findRangeBounds(XYDataset dataset) {
761         return findRangeBounds(dataset, true);
762     }
763     
764     /**
765      * Returns the range of values in the range for the dataset. This method
766      * is the partner for the {@link #findDomainBounds(XYDataset)} method.
767      *
768      * @param dataset the dataset (<code>null</code> not permitted).
769      * @param includeInterval a flag that determines whether or not the
770      * y-interval is taken into account.
771      *
772      *
773      * @return The range (possibly <code>null</code>).
774      */

775     public static Range findRangeBounds(XYDataset dataset,
776                                         boolean includeInterval) {
777         if (dataset == null) {
778             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
779         }
780         Range result = null;
781         if (dataset instanceof RangeInfo) {
782             RangeInfo info = (RangeInfo) dataset;
783             result = info.getRangeBounds(includeInterval);
784         }
785         else {
786             result = iterateXYRangeBounds(dataset);
787         }
788         return result;
789     }
790     
791     /**
792      * Iterates over the data item of the category dataset to find
793      * the range bounds.
794      *
795      * @param dataset the dataset (<code>null</code> not permitted).
796      * @param includeInterval a flag that determines whether or not the
797      * y-interval is taken into account.
798      *
799      * @return The range (possibly <code>null</code>).
800      */

801     public static Range iterateCategoryRangeBounds(CategoryDataset dataset,
802             boolean includeInterval) {
803         double minimum = Double.POSITIVE_INFINITY;
804         double maximum = Double.NEGATIVE_INFINITY;
805         boolean interval = includeInterval
806                            && dataset instanceof IntervalCategoryDataset;
807         int rowCount = dataset.getRowCount();
808         int columnCount = dataset.getColumnCount();
809         for (int row = 0; row < rowCount; row++) {
810             for (int column = 0; column < columnCount; column++) {
811                 Number JavaDoc lvalue;
812                 Number JavaDoc uvalue;
813                 if (interval) {
814                     IntervalCategoryDataset icd
815                 &