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. * XYBarDataset.java
  26. * -----------------
  27. * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: XYBarDataset.java,v 1.2 2005/01/14 17:27:06 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 02-Mar-2004 : Version 1 (DG);
  37. * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
  38. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  39. *
  40. */
  41. package org.jfree.data.xy;
  42. import org.jfree.data.general.DatasetChangeEvent;
  43. import org.jfree.data.general.DatasetChangeListener;
  44. /**
  45. * A dataset wrapper class that converts a standard {@link XYDataset} into an
  46. * {@link IntervalXYDataset} suitable for use in creating XY bar charts.
  47. */
  48. public class XYBarDataset extends AbstractIntervalXYDataset
  49. implements IntervalXYDataset, DatasetChangeListener {
  50. /** The underlying dataset. */
  51. private XYDataset underlying;
  52. /** The bar width. */
  53. private double barWidth;
  54. /**
  55. * Creates a new dataset.
  56. *
  57. * @param underlying the underlying dataset.
  58. * @param barWidth the width of the bars.
  59. */
  60. public XYBarDataset(XYDataset underlying, double barWidth) {
  61. this.underlying = underlying;
  62. this.underlying.addChangeListener(this);
  63. this.barWidth = barWidth;
  64. }
  65. /**
  66. * Returns the number of series in the dataset.
  67. *
  68. * @return The series count.
  69. */
  70. public int getSeriesCount() {
  71. return this.underlying.getSeriesCount();
  72. }
  73. /**
  74. * Returns the name of a series.
  75. *
  76. * @param series the series index (zero-based).
  77. *
  78. * @return The series name.
  79. */
  80. public String getSeriesName(int series) {
  81. return this.underlying.getSeriesName(series);
  82. }
  83. /**
  84. * Returns the number of items in a series.
  85. *
  86. * @param series the series index (zero-based).
  87. *
  88. * @return The item count.
  89. */
  90. public int getItemCount(int series) {
  91. return this.underlying.getItemCount(series);
  92. }
  93. /**
  94. * Returns the x-value for an item within a series. The x-values may or may not be returned
  95. * in ascending order, that is up to the class implementing the interface.
  96. *
  97. * @param series the series index (zero-based).
  98. * @param item the item index (zero-based).
  99. *
  100. * @return The x-value.
  101. */
  102. public Number getX(int series, int item) {
  103. return this.underlying.getX(series, item);
  104. }
  105. /**
  106. * Returns the y-value for an item within a series.
  107. *
  108. * @param series the series index (zero-based).
  109. * @param item the item index (zero-based).
  110. *
  111. * @return The y-value (possibly <code>null</code>).
  112. */
  113. public Number getY(int series, int item) {
  114. return this.underlying.getY(series, item);
  115. }
  116. /**
  117. * Returns the starting X value for the specified series and item.
  118. *
  119. * @param series the series index (zero-based).
  120. * @param item the item index (zero-based).
  121. *
  122. * @return The value.
  123. */
  124. public Number getStartX(int series, int item) {
  125. Number result = null;
  126. Number xnum = this.underlying.getX(series, item);
  127. if (xnum != null) {
  128. result = new Double(xnum.doubleValue() - this.barWidth / 2.0);
  129. }
  130. return result;
  131. }
  132. /**
  133. * Returns the ending X value for the specified series and item.
  134. *
  135. * @param series the series index (zero-based).
  136. * @param item the item index (zero-based).
  137. *
  138. * @return The value.
  139. */
  140. public Number getEndX(int series, int item) {
  141. Number result = null;
  142. Number xnum = this.underlying.getX(series, item);
  143. if (xnum != null) {
  144. result = new Double(xnum.doubleValue() + this.barWidth / 2.0);
  145. }
  146. return result;
  147. }
  148. /**
  149. * Returns the starting Y value for the specified series and item.
  150. *
  151. * @param series the series index (zero-based).
  152. * @param item the item index (zero-based).
  153. *
  154. * @return The value.
  155. */
  156. public Number getStartY(int series, int item) {
  157. return this.underlying.getY(series, item);
  158. }
  159. /**
  160. * Returns the ending Y value for the specified series and item.
  161. *
  162. * @param series the series index (zero-based).
  163. * @param item the item index (zero-based).
  164. *
  165. * @return The value.
  166. */
  167. public Number getEndY(int series, int item) {
  168. return this.underlying.getY(series, item);
  169. }
  170. /**
  171. * Receives notification of an dataset change event.
  172. *
  173. * @param event information about the event.
  174. */
  175. public void datasetChanged(DatasetChangeEvent event) {
  176. this.notifyListeners(event);
  177. }
  178. }