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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * -------------------------------
  27. * TimePeriodValuesCollection.java
  28. * -------------------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: TimePeriodValuesCollection.java,v 1.6 2005/02/10 10:05:54 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 22-Apr-2003 : Version 1 (DG);
  39. * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
  40. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
  41. * getYValue() (DG);
  42. * 06-Oct-2004 : Updated for changes in DomainInfo interface (DG);
  43. * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
  44. *
  45. */
  46. package org.jfree.data.time;
  47. import java.io.Serializable;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import org.jfree.data.DomainInfo;
  51. import org.jfree.data.Range;
  52. import org.jfree.data.xy.AbstractIntervalXYDataset;
  53. import org.jfree.data.xy.IntervalXYDataset;
  54. /**
  55. * A collection of {@link TimePeriodValues} objects.
  56. * <P>
  57. * This class implements the {@link org.jfree.data.xy.XYDataset} interface, as
  58. * well as the extended {@link IntervalXYDataset} interface. This makes it a
  59. * convenient dataset for use with the {@link org.jfree.chart.plot.XYPlot}
  60. * class.
  61. */
  62. public class TimePeriodValuesCollection extends AbstractIntervalXYDataset
  63. implements IntervalXYDataset,
  64. DomainInfo,
  65. Serializable {
  66. /** Storage for the time series. */
  67. private List data;
  68. /**
  69. * The position within a time period to return as the x-value (START,
  70. * MIDDLE or END).
  71. */
  72. private TimePeriodAnchor xPosition;
  73. /**
  74. * A flag that indicates that the domain is 'points in time'. If this
  75. * flag is true, only the x-value is used to determine the range of values
  76. * in the domain, the start and end x-values are ignored.
  77. */
  78. private boolean domainIsPointsInTime;
  79. /**
  80. * Constructs an empty dataset.
  81. */
  82. public TimePeriodValuesCollection() {
  83. this((TimePeriodValues) null);
  84. }
  85. /**
  86. * Constructs a dataset containing a single series. Additional series can
  87. * be added.
  88. *
  89. * @param series the series.
  90. */
  91. public TimePeriodValuesCollection(TimePeriodValues series) {
  92. this.data = new java.util.ArrayList();
  93. this.xPosition = TimePeriodAnchor.MIDDLE;
  94. this.domainIsPointsInTime = true;
  95. if (series != null) {
  96. this.data.add(series);
  97. series.addChangeListener(this);
  98. }
  99. }
  100. /**
  101. * Returns the position of the X value within each time period.
  102. *
  103. * @return The position (never <code>null</code>).
  104. */
  105. public TimePeriodAnchor getXPosition() {
  106. return this.xPosition;
  107. }
  108. /**
  109. * Sets the position of the x axis within each time period.
  110. *
  111. * @param position the position (<code>null</code> not permitted).
  112. */
  113. public void setXPosition(TimePeriodAnchor position) {
  114. if (position == null) {
  115. throw new IllegalArgumentException("Null 'position' argument.");
  116. }
  117. this.xPosition = position;
  118. }
  119. /**
  120. * Returns a flag that controls whether the domain is treated as 'points
  121. * in time'. This flag is used when determining the max and min values for
  122. * the domain. If true, then only the x-values are considered for the max
  123. * and min values. If false, then the start and end x-values will also be
  124. * taken into consideration
  125. *
  126. * @return The flag.
  127. */
  128. public boolean getDomainIsPointsInTime() {
  129. return this.domainIsPointsInTime;
  130. }
  131. /**
  132. * Sets a flag that controls whether the domain is treated as 'points in
  133. * time', or time periods.
  134. *
  135. * @param flag the new value of the flag.
  136. */
  137. public void setDomainIsPointsInTime(boolean flag) {
  138. this.domainIsPointsInTime = flag;
  139. }
  140. /**
  141. * Returns the number of series in the collection.
  142. *
  143. * @return The series count.
  144. */
  145. public int getSeriesCount() {
  146. return this.data.size();
  147. }
  148. /**
  149. * Returns a series.
  150. *
  151. * @param series the index of the series (zero-based).
  152. *
  153. * @return The series.
  154. */
  155. public TimePeriodValues getSeries(int series) {
  156. if ((series < 0) || (series > getSeriesCount())) {
  157. throw new IllegalArgumentException("Index 'series' out of range.");
  158. }
  159. return (TimePeriodValues) this.data.get(series);
  160. }
  161. /**
  162. * Returns the name of a series.
  163. * <P>
  164. * This method is provided for convenience.
  165. *
  166. * @param series the index of the series (zero-based).
  167. *
  168. * @return The name of a series.
  169. */
  170. public String getSeriesName(int series) {
  171. // defer argument checking
  172. return getSeries(series).getName();
  173. }
  174. /**
  175. * Adds a series to the collection. A
  176. * {@link org.jfree.data.general.DatasetChangeEvent} is sent to all
  177. * registered listeners.
  178. *
  179. * @param series the time series.
  180. */
  181. public void addSeries(TimePeriodValues series) {
  182. if (series == null) {
  183. throw new IllegalArgumentException("Null 'series' argument.");
  184. }
  185. this.data.add(series);
  186. series.addChangeListener(this);
  187. fireDatasetChanged();
  188. }
  189. /**
  190. * Removes the specified series from the collection.
  191. *
  192. * @param series the series to remove.
  193. */
  194. public void removeSeries(TimePeriodValues series) {
  195. if (series == null) {
  196. throw new IllegalArgumentException("Null 'series' argument.");
  197. }
  198. this.data.remove(series);
  199. series.removeChangeListener(this);
  200. fireDatasetChanged();
  201. }
  202. /**
  203. * Removes a series from the collection.
  204. *
  205. * @param index the series index (zero-based).
  206. */
  207. public void removeSeries(int index) {
  208. TimePeriodValues series = getSeries(index);
  209. if (series != null) {
  210. removeSeries(series);
  211. }
  212. }
  213. /**
  214. * Returns the number of items in the specified series.
  215. * <P>
  216. * This method is provided for convenience.
  217. *
  218. * @param series the index of the series of interest (zero-based).
  219. *
  220. * @return The number of items in the specified series.
  221. */
  222. public int getItemCount(int series) {
  223. return getSeries(series).getItemCount();
  224. }
  225. /**
  226. * Returns the x-value for the specified series and item.
  227. *
  228. * @param series the series (zero-based index).
  229. * @param item the item (zero-based index).
  230. *
  231. * @return The x-value for the specified series and item.
  232. */
  233. public Number getX(int series, int item) {
  234. TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
  235. TimePeriodValue dp = ts.getDataItem(item);
  236. TimePeriod period = dp.getPeriod();
  237. return new Long(getX(period));
  238. }
  239. /**
  240. * Returns the x-value for a time period.
  241. *
  242. * @param period the time period.
  243. *
  244. * @return The x-value.
  245. */
  246. private long getX(TimePeriod period) {
  247. if (this.xPosition == TimePeriodAnchor.START) {
  248. return period.getStart().getTime();
  249. }
  250. else if (this.xPosition == TimePeriodAnchor.MIDDLE) {
  251. return period.getStart().getTime()
  252. / 2 + period.getEnd().getTime() / 2;
  253. }
  254. else if (this.xPosition == TimePeriodAnchor.END) {
  255. return period.getEnd().getTime();
  256. }
  257. else {
  258. throw new IllegalStateException("TimePeriodAnchor unknown.");
  259. }
  260. }
  261. /**
  262. * Returns the starting X value for the specified series and item.
  263. *
  264. * @param series the series (zero-based index).
  265. * @param item the item (zero-based index).
  266. *
  267. * @return The starting X value for the specified series and item.
  268. */
  269. public Number getStartX(int series, int item) {
  270. TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
  271. TimePeriodValue dp = ts.getDataItem(item);
  272. return new Long(dp.getPeriod().getStart().getTime());
  273. }
  274. /**
  275. * Returns the ending X value for the specified series and item.
  276. *
  277. * @param series the series (zero-based index).
  278. * @param item the item (zero-based index).
  279. *
  280. * @return The ending X value for the specified series and item.
  281. */
  282. public Number getEndX(int series, int item) {
  283. TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
  284. TimePeriodValue dp = ts.getDataItem(item);
  285. return new Long(dp.getPeriod().getEnd().getTime());
  286. }
  287. /**
  288. * Returns the y-value for the specified series and item.
  289. *
  290. * @param series the series (zero-based index).
  291. * @param item the item (zero-based index).
  292. *
  293. * @return The y-value for the specified series and item.
  294. */
  295. public Number getY(int series, int item) {
  296. TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
  297. TimePeriodValue dp = ts.getDataItem(item);
  298. return dp.getValue();
  299. }
  300. /**
  301. * Returns the starting Y value for the specified series and item.
  302. *
  303. * @param series the series (zero-based index).
  304. * @param item the item (zero-based index).
  305. *
  306. * @return The starting Y value for the specified series and item.
  307. */
  308. public Number getStartY(int series, int item) {
  309. return getY(series, item);
  310. }
  311. /**
  312. * Returns the ending Y value for the specified series and item.
  313. *
  314. * @param series the series (zero-based index).
  315. * @param item the item (zero-based index).
  316. *
  317. * @return The ending Y value for the specified series and item.
  318. */
  319. public Number getEndY(int series, int item) {
  320. return getY(series, item);
  321. }
  322. /**
  323. * Returns the minimum x-value in the dataset.
  324. *
  325. * @param includeInterval a flag that determines whether or not the
  326. * x-interval is taken into account.
  327. *
  328. * @return The minimum value.
  329. */
  330. public double getDomainLowerBound(boolean includeInterval) {
  331. double result = Double.NaN;
  332. Range r = getDomainBounds(includeInterval);
  333. if (r != null) {
  334. result = r.getLowerBound();
  335. }
  336. return result;
  337. }
  338. /**
  339. * Returns the maximum x-value in the dataset.
  340. *
  341. * @param includeInterval a flag that determines whether or not the
  342. * x-interval is taken into account.
  343. *
  344. * @return The maximum value.
  345. */
  346. public double getDomainUpperBound(boolean includeInterval) {
  347. double result = Double.NaN;
  348. Range r = getDomainBounds(includeInterval);
  349. if (r != null) {
  350. result = r.getUpperBound();
  351. }
  352. return result;
  353. }
  354. /**
  355. * Returns the range of the values in this dataset's domain.
  356. *
  357. * @param includeInterval a flag that determines whether or not the
  358. * x-interval is taken into account.
  359. *
  360. * @return The range.
  361. */
  362. public Range getDomainBounds(boolean includeInterval) {
  363. Range result = null;
  364. Range temp = null;
  365. Iterator iterator = this.data.iterator();
  366. while (iterator.hasNext()) {
  367. TimePeriodValues series = (TimePeriodValues) iterator.next();
  368. int count = series.getItemCount();
  369. if (count > 0) {
  370. TimePeriod start = series.getTimePeriod(
  371. series.getMinStartIndex()
  372. );
  373. TimePeriod end = series.getTimePeriod(series.getMaxEndIndex());
  374. if (this.domainIsPointsInTime) {
  375. if (this.xPosition == TimePeriodAnchor.START) {
  376. TimePeriod maxStart = series.getTimePeriod(
  377. series.getMaxStartIndex()
  378. );
  379. temp = new Range(
  380. start.getStart().getTime(),
  381. maxStart.getStart().getTime()
  382. );
  383. }
  384. else if (this.xPosition == TimePeriodAnchor.MIDDLE) {
  385. TimePeriod minMiddle = series.getTimePeriod(
  386. series.getMinMiddleIndex()
  387. );
  388. long s1 = minMiddle.getStart().getTime();
  389. long e1 = minMiddle.getEnd().getTime();
  390. TimePeriod maxMiddle = series.getTimePeriod(
  391. series.getMaxMiddleIndex()
  392. );
  393. long s2 = maxMiddle.getStart().getTime();
  394. long e2 = maxMiddle.getEnd().getTime();
  395. temp = new Range(
  396. s1 + (e1 - s1) / 2, s2 + (e2 - s2) / 2
  397. );
  398. }
  399. else if (this.xPosition == TimePeriodAnchor.END) {
  400. TimePeriod minEnd = series.getTimePeriod(
  401. series.getMinEndIndex()
  402. );
  403. temp = new Range(
  404. minEnd.getEnd().getTime(), end.getEnd().getTime()
  405. );
  406. }
  407. }
  408. else {
  409. temp = new Range(
  410. start.getStart().getTime(), end.getEnd().getTime()
  411. );
  412. }
  413. result = Range.combine(result, temp);
  414. }
  415. }
  416. return result;
  417. }
  418. }