1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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. * DefaultOHLCDataset.java
  26. * -----------------------
  27. * (C) Copyright 2003, 2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DefaultOHLCDataset.java,v 1.1 2004/09/08 09:14:58 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 03-Dec-2003 : Version 1 (DG);
  37. * 05-May-2004 : Now extends AbstractXYDataset (DG);
  38. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  39. *
  40. */
  41. package org.jfree.data.xy;
  42. import java.util.Arrays;
  43. import java.util.Date;
  44. /**
  45. * A simple implementation of the {@link OHLCDataset} interface. This implementation supports
  46. * only one series.
  47. */
  48. public class DefaultOHLCDataset extends AbstractXYDataset implements OHLCDataset {
  49. /** The series name. */
  50. private String name;
  51. /** Storage for the data items. */
  52. private OHLCDataItem[] data;
  53. /**
  54. * Creates a new dataset.
  55. *
  56. * @param name the series name.
  57. * @param data the data items.
  58. */
  59. public DefaultOHLCDataset(String name, OHLCDataItem[] data) {
  60. this.name = name;
  61. this.data = data;
  62. }
  63. /**
  64. * Returns the series name.
  65. *
  66. * @param series the series index (ignored).
  67. *
  68. * @return The series name.
  69. */
  70. public String getSeriesName(int series) {
  71. return this.name;
  72. }
  73. /**
  74. * Returns the x-value for a data item.
  75. *
  76. * @param series the series index (ignored).
  77. * @param item the item index (zero-based).
  78. *
  79. * @return The x-value.
  80. */
  81. public Number getX(int series, int item) {
  82. return new Long(this.data[item].getDate().getTime());
  83. }
  84. /**
  85. * Returns the x-value for a data item as a date.
  86. *
  87. * @param series the series index (ignored).
  88. * @param item the item index (zero-based).
  89. *
  90. * @return The x-value as a date.
  91. */
  92. public Date getXDate(int series, int item) {
  93. return this.data[item].getDate();
  94. }
  95. /**
  96. * Returns the y-value.
  97. *
  98. * @param series the series index (ignored).
  99. * @param item the item index (zero-based).
  100. *
  101. * @return the y value.
  102. */
  103. public Number getY(int series, int item) {
  104. return getClose(series, item);
  105. }
  106. /**
  107. * Returns the high value.
  108. *
  109. * @param series the series index (ignored).
  110. * @param item the item index (zero-based).
  111. *
  112. * @return the high value.
  113. */
  114. public Number getHigh(int series, int item) {
  115. return this.data[item].getHigh();
  116. }
  117. /**
  118. * Returns the high-value (as a double primitive) for an item within a series.
  119. *
  120. * @param series the series (zero-based index).
  121. * @param item the item (zero-based index).
  122. *
  123. * @return The high-value.
  124. */
  125. public double getHighValue(int series, int item) {
  126. double result = Double.NaN;
  127. Number high = getHigh(series, item);
  128. if (high != null) {
  129. result = high.doubleValue();
  130. }
  131. return result;
  132. }
  133. /**
  134. * Returns the low value.
  135. *
  136. * @param series the series index (ignored).
  137. * @param item the item index (zero-based).
  138. *
  139. * @return the low value.
  140. */
  141. public Number getLow(int series, int item) {
  142. return this.data[item].getLow();
  143. }
  144. /**
  145. * Returns the low-value (as a double primitive) for an item within a series.
  146. *
  147. * @param series the series (zero-based index).
  148. * @param item the item (zero-based index).
  149. *
  150. * @return The low-value.
  151. */
  152. public double getLowValue(int series, int item) {
  153. double result = Double.NaN;
  154. Number low = getLow(series, item);
  155. if (low != null) {
  156. result = low.doubleValue();
  157. }
  158. return result;
  159. }
  160. /**
  161. * Returns the open value.
  162. *
  163. * @param series the series index (ignored).
  164. * @param item the item index (zero-based).
  165. *
  166. * @return the open value.
  167. */
  168. public Number getOpen(int series, int item) {
  169. return this.data[item].getOpen();
  170. }
  171. /**
  172. * Returns the open-value (as a double primitive) for an item within a series.
  173. *
  174. * @param series the series (zero-based index).
  175. * @param item the item (zero-based index).
  176. *
  177. * @return The open-value.
  178. */
  179. public double getOpenValue(int series, int item) {
  180. double result = Double.NaN;
  181. Number open = getOpen(series, item);
  182. if (open != null) {
  183. result = open.doubleValue();
  184. }
  185. return result;
  186. }
  187. /**
  188. * Returns the close value.
  189. *
  190. * @param series the series index (ignored).
  191. * @param item the item index (zero-based).
  192. *
  193. * @return the close value.
  194. */
  195. public Number getClose(int series, int item) {
  196. return this.data[item].getClose();
  197. }
  198. /**
  199. * Returns the close-value (as a double primitive) for an item within a series.
  200. *
  201. * @param series the series (zero-based index).
  202. * @param item the item (zero-based index).
  203. *
  204. * @return The close-value.
  205. */
  206. public double getCloseValue(int series, int item) {
  207. double result = Double.NaN;
  208. Number close = getClose(series, item);
  209. if (close != null) {
  210. result = close.doubleValue();
  211. }
  212. return result;
  213. }
  214. /**
  215. * Returns the trading volume.
  216. *
  217. * @param series the series index (ignored).
  218. * @param item the item index (zero-based).
  219. *
  220. * @return the trading volume.
  221. */
  222. public Number getVolume(int series, int item) {
  223. return this.data[item].getVolume();
  224. }
  225. /**
  226. * Returns the volume-value (as a double primitive) for an item within a series.
  227. *
  228. * @param series the series (zero-based index).
  229. * @param item the item (zero-based index).
  230. *
  231. * @return The volume-value.
  232. */
  233. public double getVolumeValue(int series, int item) {
  234. double result = Double.NaN;
  235. Number volume = getVolume(series, item);
  236. if (volume != null) {
  237. result = volume.doubleValue();
  238. }
  239. return result;
  240. }
  241. /**
  242. * Returns the series count.
  243. *
  244. * @return 1.
  245. */
  246. public int getSeriesCount() {
  247. return 1;
  248. }
  249. /**
  250. * Returns the item count for the specified series.
  251. *
  252. * @param series the series index (ignored).
  253. *
  254. * @return The item count.
  255. */
  256. public int getItemCount(int series) {
  257. return this.data.length;
  258. }
  259. /**
  260. * Sorts the data into ascending order by date.
  261. */
  262. public void sortDataByDate() {
  263. Arrays.sort(this.data);
  264. }
  265. }