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. * DefaultHighLowDataset.java
  26. * --------------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DefaultHighLowDataset.java,v 1.3 2005/01/11 17:35:03 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 21-Mar-2002 : Version 1 (DG);
  37. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 06-May-2004 : Now extends AbstractXYDataset and added new methods from
  39. * HighLowDataset (DG);
  40. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  41. *
  42. */
  43. package org.jfree.data.xy;
  44. import java.util.Date;
  45. /**
  46. * A simple implementation of the {@link OHLCDataset}.
  47. */
  48. public class DefaultHighLowDataset extends AbstractXYDataset implements OHLCDataset {
  49. /** The series name. */
  50. private String seriesName;
  51. /** Storage for the dates. */
  52. private Date[] date;
  53. /** Storage for the high values. */
  54. private Number[] high;
  55. /** Storage for the low values. */
  56. private Number[] low;
  57. /** Storage for the open values. */
  58. private Number[] open;
  59. /** Storage for the close values. */
  60. private Number[] close;
  61. /** Storage for the volume values. */
  62. private Number[] volume;
  63. /**
  64. * Constructs a new high/low/open/close dataset.
  65. * <p>
  66. * The current implementation allows only one series in the dataset.
  67. * This may be extended in a future version.
  68. *
  69. * @param seriesName the name of the series.
  70. * @param date the dates.
  71. * @param high the high values.
  72. * @param low the low values.
  73. * @param open the open values.
  74. * @param close the close values.
  75. * @param volume the volume values.
  76. */
  77. public DefaultHighLowDataset(String seriesName,
  78. Date[] date,
  79. double[] high, final double[] low,
  80. double[] open, final double[] close,
  81. double[] volume) {
  82. this.seriesName = seriesName;
  83. this.date = date;
  84. this.high = createNumberArray(high);
  85. this.low = createNumberArray(low);
  86. this.open = createNumberArray(open);
  87. this.close = createNumberArray(close);
  88. this.volume = createNumberArray(volume);
  89. }
  90. /**
  91. * Returns the name of the series stored in this dataset.
  92. *
  93. * @param i the index of the series. Currently ignored.
  94. * @return the name of this series.
  95. */
  96. public String getSeriesName(int i) {
  97. return this.seriesName;
  98. }
  99. /**
  100. * Returns the x-value for one item in a series.
  101. * <p>
  102. * The value returned is a Long object generated from the underlying Date object.
  103. *
  104. * @param series the series (zero-based index).
  105. * @param item the item (zero-based index).
  106. *
  107. * @return The x-value.
  108. */
  109. public Number getX(int series, int item) {
  110. return new Long(this.date[item].getTime());
  111. }
  112. /**
  113. * Returns the x-value for one item in a series, as a Date.
  114. * <p>
  115. * This method is provided for convenience only.
  116. *
  117. * @param series the series (zero-based index).
  118. * @param item the item (zero-based index).
  119. *
  120. * @return The x-value as a Date.
  121. */
  122. public Date getXDate(int series, int item) {
  123. return this.date[item];
  124. }
  125. /**
  126. * Returns the y-value for one item in a series.
  127. * <p>
  128. * This method (from the {@link XYDataset} interface) is mapped to the
  129. * {@link #getCloseValue(int, int)} method.
  130. *
  131. * @param series the series (zero-based index).
  132. * @param item the item (zero-based index).
  133. *
  134. * @return The y-value.
  135. */
  136. public Number getY(int series, int item) {
  137. return getClose(series, item);
  138. }
  139. /**
  140. * Returns the high-value for one item in a series.
  141. *
  142. * @param series the series (zero-based index).
  143. * @param item the item (zero-based index).
  144. *
  145. * @return The high-value.
  146. */
  147. public Number getHigh(int series, int item) {
  148. return this.high[item];
  149. }
  150. /**
  151. * Returns the high-value (as a double primitive) for an item within a series.
  152. *
  153. * @param series the series (zero-based index).
  154. * @param item the item (zero-based index).
  155. *
  156. * @return The high-value.
  157. */
  158. public double getHighValue(int series, int item) {
  159. double result = Double.NaN;
  160. Number high = getHigh(series, item);
  161. if (high != null) {
  162. result = high.doubleValue();
  163. }
  164. return result;
  165. }
  166. /**
  167. * Returns the low-value for one item in a series.
  168. *
  169. * @param series the series (zero-based index).
  170. * @param item the item (zero-based index).
  171. *
  172. * @return The low-value.
  173. */
  174. public Number getLow(int series, int item) {
  175. return this.low[item];
  176. }
  177. /**
  178. * Returns the low-value (as a double primitive) for an item within a series.
  179. *
  180. * @param series the series (zero-based index).
  181. * @param item the item (zero-based index).
  182. *
  183. * @return The low-value.
  184. */
  185. public double getLowValue(int series, int item) {
  186. double result = Double.NaN;
  187. Number low = getLow(series, item);
  188. if (low != null) {
  189. result = low.doubleValue();
  190. }
  191. return result;
  192. }
  193. /**
  194. * Returns the open-value for one item in a series.
  195. *
  196. * @param series the series (zero-based index).
  197. * @param item the item (zero-based index).
  198. *
  199. * @return The open-value.
  200. */
  201. public Number getOpen(int series, int item) {
  202. return this.open[item];
  203. }
  204. /**
  205. * Returns the open-value (as a double primitive) for an item within a series.
  206. *
  207. * @param series the series (zero-based index).
  208. * @param item the item (zero-based index).
  209. *
  210. * @return The open-value.
  211. */
  212. public double getOpenValue(int series, int item) {
  213. double result = Double.NaN;
  214. Number open = getOpen(series, item);
  215. if (open != null) {
  216. result = open.doubleValue();
  217. }
  218. return result;
  219. }
  220. /**
  221. * Returns the close-value for one item in a series.
  222. *
  223. * @param series the series (zero-based index).
  224. * @param item the item (zero-based index).
  225. *
  226. * @return The close-value.
  227. */
  228. public Number getClose(int series, int item) {
  229. return this.close[item];
  230. }
  231. /**
  232. * Returns the close-value (as a double primitive) for an item within a series.
  233. *
  234. * @param series the series (zero-based index).
  235. * @param item the item (zero-based index).
  236. *
  237. * @return The close-value.
  238. */
  239. public double getCloseValue(int series, int item) {
  240. double result = Double.NaN;
  241. Number close = getClose(series, item);
  242. if (close != null) {
  243. result = close.doubleValue();
  244. }
  245. return result;
  246. }
  247. /**
  248. * Returns the volume-value for one item in a series.
  249. *
  250. * @param series the series (zero-based index).
  251. * @param item the item (zero-based index).
  252. *
  253. * @return the volume-value.
  254. */
  255. public Number getVolume(int series, int item) {
  256. return this.volume[item];
  257. }
  258. /**
  259. * Returns the volume-value (as a double primitive) for an item within a series.
  260. *
  261. * @param series the series (zero-based index).
  262. * @param item the item (zero-based index).
  263. *
  264. * @return The volume-value.
  265. */
  266. public double getVolumeValue(int series, int item) {
  267. double result = Double.NaN;
  268. Number volume = getVolume(series, item);
  269. if (volume != null) {
  270. result = volume.doubleValue();
  271. }
  272. return result;
  273. }
  274. /**
  275. * Returns the number of series in the dataset.
  276. * <p>
  277. * This implementation only allows one series.
  278. *
  279. * @return The number of series.
  280. */
  281. public int getSeriesCount() {
  282. return 1;
  283. }
  284. /**
  285. * Returns the number of items in the specified series.
  286. *
  287. * @param series the index (zero-based) of the series.
  288. *
  289. * @return The number of items in the specified series.
  290. */
  291. public int getItemCount(int series) {
  292. return this.date.length;
  293. }
  294. /**
  295. * Constructs an array of Number objects from an array of doubles.
  296. *
  297. * @param data the double values to convert.
  298. *
  299. * @return The data as an array of Number objects.
  300. */
  301. public static Number[] createNumberArray(double[] data) {
  302. Number[] result = new Number[data.length];
  303. for (int i = 0; i < data.length; i++) {
  304. result[i] = new Double(data[i]);
  305. }
  306. return result;
  307. }
  308. }