1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------------
  25. * CategoryToPieDataset.java
  26. * -------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Christian W. Zuckschwerdt;
  31. *
  32. * $Id: CategoryToPieDataset.java,v 1.3 2005/01/14 17:33:23 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 23-Jan-2003 : Version 1 (DG);
  37. * 30-Jul-2003 : Pass through DatasetChangeEvent (CZ);
  38. * 29-Jan-2004 : Replaced 'extract' int with TableOrder (DG);
  39. * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 release (DG);
  40. *
  41. */
  42. package org.jfree.data.category;
  43. import java.util.List;
  44. import org.jfree.data.general.AbstractDataset;
  45. import org.jfree.data.general.DatasetChangeEvent;
  46. import org.jfree.data.general.DatasetChangeListener;
  47. import org.jfree.data.general.PieDataset;
  48. import org.jfree.util.TableOrder;
  49. /**
  50. * A {@link PieDataset} implementation that obtains its data from one row or column of a
  51. * {@link CategoryDataset}.
  52. *
  53. */
  54. public class CategoryToPieDataset extends AbstractDataset
  55. implements PieDataset, DatasetChangeListener {
  56. /** The source. */
  57. private CategoryDataset source;
  58. /** The extract type. */
  59. private TableOrder extract;
  60. /** The row or column index. */
  61. private int index;
  62. /**
  63. * An adaptor class that converts any {@link CategoryDataset} into a {@link PieDataset}, by
  64. * taking the values from a single row or column.
  65. *
  66. * @param source the source dataset (<code>null</code> permitted).
  67. * @param extract extract data from rows or columns? (<code>null</code> not permitted).
  68. * @param index the row or column index.
  69. */
  70. public CategoryToPieDataset(CategoryDataset source,
  71. TableOrder extract,
  72. int index) {
  73. if (extract == null) {
  74. throw new IllegalArgumentException("Null 'extract' argument.");
  75. }
  76. this.source = source;
  77. this.source.addChangeListener(this);
  78. this.extract = extract;
  79. this.index = index;
  80. }
  81. /**
  82. * Returns the number of items (values) in the collection. If the underlying
  83. * dataset is <code>null</code>, this method returns zero.
  84. *
  85. * @return The item count.
  86. */
  87. public int getItemCount() {
  88. int result = 0;
  89. if (this.source != null) {
  90. if (this.extract == TableOrder.BY_ROW) {
  91. result = this.source.getColumnCount();
  92. }
  93. else if (this.extract == TableOrder.BY_COLUMN) {
  94. result = this.source.getRowCount();
  95. }
  96. }
  97. return result;
  98. }
  99. /**
  100. * Returns a value.
  101. *
  102. * @param item the item index (zero-based).
  103. *
  104. * @return The value (possibly <code>null</code>).
  105. */
  106. public Number getValue(int item) {
  107. Number result = null;
  108. if (this.source != null) {
  109. if (this.extract == TableOrder.BY_ROW) {
  110. result = this.source.getValue(this.index, item);
  111. }
  112. else if (this.extract == TableOrder.BY_COLUMN) {
  113. result = this.source.getValue(item, this.index);
  114. }
  115. }
  116. return result;
  117. }
  118. /**
  119. * Returns a key.
  120. *
  121. * @param index the item index (zero-based).
  122. *
  123. * @return The key.
  124. */
  125. public Comparable getKey(int index) {
  126. Comparable result = null;
  127. if (this.extract == TableOrder.BY_ROW) {
  128. result = this.source.getColumnKey(index);
  129. }
  130. else if (this.extract == TableOrder.BY_COLUMN) {
  131. result = this.source.getRowKey(index);
  132. }
  133. return result;
  134. }
  135. /**
  136. * Returns the index for a given key.
  137. *
  138. * @param key the key.
  139. *
  140. * @return The index.
  141. */
  142. public int getIndex(Comparable key) {
  143. int result = -1;
  144. if (this.extract == TableOrder.BY_ROW) {
  145. result = this.source.getColumnIndex(key);
  146. }
  147. else if (this.extract == TableOrder.BY_COLUMN) {
  148. result = this.source.getRowIndex(key);
  149. }
  150. return result;
  151. }
  152. /**
  153. * Returns the keys.
  154. *
  155. * @return The keys.
  156. */
  157. public List getKeys() {
  158. List result = null;
  159. if (this.extract == TableOrder.BY_ROW) {
  160. result = this.source.getColumnKeys();
  161. }
  162. else if (this.extract == TableOrder.BY_COLUMN) {
  163. result = this.source.getRowKeys();
  164. }
  165. return result;
  166. }
  167. /**
  168. * Returns the value for a given key. If the key is not recognised, the method should
  169. * return <code>null</code> (but note that <code>null</code> can be associated with a
  170. * valid key also).
  171. *
  172. * @param key the key.
  173. *
  174. * @return The value (possibly <code>null</code>).
  175. */
  176. public Number getValue(Comparable key) {
  177. Number result = null;
  178. int keyIndex = getIndex(key);
  179. if (this.extract == TableOrder.BY_ROW) {
  180. result = this.source.getValue(this.index, keyIndex);
  181. }
  182. else if (this.extract == TableOrder.BY_COLUMN) {
  183. result = this.source.getValue(keyIndex, this.index);
  184. }
  185. return result;
  186. }
  187. /**
  188. * Passes the {@link DatasetChangeEvent} through.
  189. *
  190. * @param event the event.
  191. */
  192. public void datasetChanged (DatasetChangeEvent event) {
  193. fireDatasetChanged();
  194. }
  195. }