KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > CategoryToPieDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -------------------------
23  * CategoryToPieDataset.java
24  * -------------------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Christian W. Zuckschwerdt;
29  *
30  * $Id: CategoryToPieDataset.java,v 1.4 2003/07/30 15:58:20 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 23-Jan-2003 : Version 1 (DG);
35  * 30-Jul-2003 : Pass through DatasetChangeEvent (CZ);
36  *
37  */

38
39 package org.jfree.data;
40
41 import java.util.List JavaDoc;
42
43 /**
44  * A {@link PieDataset} implementation that obtains its data from one row or column of a
45  * {@link CategoryDataset}.
46  *
47  * @author David Gilbert
48  */

49 public class CategoryToPieDataset extends AbstractDataset
50                                   implements PieDataset, DatasetChangeListener {
51
52     /** A constant indicating that data should be extracted from a row. */
53     public static final int ROW = 0;
54
55     /** A constant indicating that data should be extracted from a column. */
56     public static final int COLUMN = 1;
57
58     /** The source. */
59     private CategoryDataset source;
60
61     /** The extract type. */
62     private int extract;
63
64     /** The row or column index. */
65     private int index;
66
67     /**
68      * An adaptor class that converts any {@link CategoryDataset} into a {@link PieDataset}, by
69      * taking the values from a single row or column.
70      *
71      * @param source the source dataset.
72      * @param extract ROW or COLUMN.
73      * @param index the row or column index.
74      */

75     public CategoryToPieDataset(CategoryDataset source, int extract, int index) {
76         this.source = source;
77         this.source.addChangeListener(this);
78         this.extract = extract;
79         this.index = index;
80     }
81
82     /**
83      * Returns the number of items (values) in the collection.
84      *
85      * @return the item count.
86      */

87     public int getItemCount() {
88
89         int result = 0;
90         switch (this.extract) {
91             case (ROW) :
92                 result = source.getColumnCount();
93                 break;
94             case (COLUMN) :
95                 result = source.getRowCount();
96                 break;
97             default : // error
98
}
99         return result;
100     }
101
102     /**
103      * Returns a value.
104      *
105      * @param item the item index (zero-based).
106      *
107      * @return the value.
108      */

109     public Number JavaDoc getValue(int item) {
110
111         Number JavaDoc result = null;
112         switch (this.extract) {
113             case (ROW) :
114                 result = source.getValue(this.index, item);
115                 break;
116             case (COLUMN) :
117                 result = source.getValue(item, this.index);
118                 break;
119             default : // error
120
}
121         return result;
122
123     }
124
125     /**
126      * Returns a key.
127      *
128      * @param index the item index (zero-based).
129      *
130      * @return the key.
131      */

132     public Comparable JavaDoc getKey(int index) {
133
134         Comparable JavaDoc result = null;
135         switch (this.extract) {
136             case (ROW) :
137                 result = source.getColumnKey(index);
138                 break;
139             case (COLUMN) :
140                 result = source.getRowKey(index);
141                 break;
142             default : // error
143
}
144         return result;
145
146     }
147
148     /**
149      * Returns the index for a given key.
150      *
151      * @param key the key.
152      *
153      * @return the index.
154      */

155     public int getIndex(Comparable JavaDoc key) {
156
157         int result = -1;
158         switch (this.extract) {
159             case (ROW) :
160                 result = source.getColumnIndex(key);
161                 break;
162             case (COLUMN) :
163                 result = source.getRowIndex(key);
164                 break;
165             default : // error
166
}
167         return result;
168
169     }
170
171     /**
172      * Returns the keys.
173      *
174      * @return the keys.
175      */

176     public List JavaDoc getKeys() {
177
178         List JavaDoc result = null;
179         switch (this.extract) {
180             case (ROW) :
181                 result = source.getColumnKeys();
182                 break;
183             case (COLUMN) :
184                 result = source.getRowKeys();
185                 break;
186             default : // error
187
}
188         return result;
189
190     }
191
192     /**
193      * Returns the value (possibly null) for a given key.
194      * <P>
195      * If the key is not recognised, the method should return null.
196      *
197      * @param key the key.
198      *
199      * @return the value.
200      */

201     public Number JavaDoc getValue(Comparable JavaDoc key) {
202
203         Number JavaDoc result = null;
204         int keyIndex = getIndex(key);
205         switch (this.extract) {
206             case (ROW) :
207                 result = source.getValue(this.index, keyIndex);
208                 break;
209             case (COLUMN) :
210                 result = source.getValue(keyIndex, this.index);
211                 break;
212             default : // error
213
}
214         return result;
215
216     }
217     
218     /**
219      * Passes the {@link DatasetChangeEvent} through.
220      *
221      * @param event the event.
222      */

223     public void datasetChanged (DatasetChangeEvent event) {
224         fireDatasetChanged ();
225     }
226     
227 }
228
Popular Tags