PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/JFreeChart-1.0.13/source/org/jfree/data/xy/CategoryTableXYDataset.java

https://github.com/ph4r05/WirelessSensorNodeGeolocation
Java | 403 lines | 124 code | 33 blank | 246 comment | 7 complexity | 2988a4b89c81fc83290e8eb759c42e51 MD5 | raw file
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2008, 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
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ---------------------------
  28. * CategoryTableXYDataset.java
  29. * ---------------------------
  30. * (C) Copyright 2004-2008, by Andreas Schroeder and Contributors.
  31. *
  32. * Original Author: Andreas Schroeder;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * Changes
  36. * -------
  37. * 31-Mar-2004 : Version 1 (AS);
  38. * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
  39. * 15-Jul-2004 : Switched interval access method names (DG);
  40. * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.xy (DG);
  41. * 17-Nov-2004 : Updates required by changes to DomainInfo interface (DG);
  42. * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
  43. * 05-Oct-2005 : Made the interval delegate a dataset change listener (DG);
  44. * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  45. * 22-Apr-2008 : Implemented PublicCloneable, and fixed clone() method (DG);
  46. *
  47. */
  48. package org.jfree.data.xy;
  49. import org.jfree.data.DefaultKeyedValues2D;
  50. import org.jfree.data.DomainInfo;
  51. import org.jfree.data.Range;
  52. import org.jfree.data.general.DatasetChangeEvent;
  53. import org.jfree.data.general.DatasetUtilities;
  54. import org.jfree.util.PublicCloneable;
  55. /**
  56. * An implementation variant of the {@link TableXYDataset} where every series
  57. * shares the same x-values (required for generating stacked area charts).
  58. * This implementation uses a {@link DefaultKeyedValues2D} Object as backend
  59. * implementation and is hence more "category oriented" than the {@link
  60. * DefaultTableXYDataset} implementation.
  61. * <p>
  62. * This implementation provides no means to remove data items yet.
  63. * This is due to the lack of such facility in the DefaultKeyedValues2D class.
  64. * <p>
  65. * This class also implements the {@link IntervalXYDataset} interface, but this
  66. * implementation is provisional.
  67. */
  68. public class CategoryTableXYDataset extends AbstractIntervalXYDataset
  69. implements TableXYDataset, IntervalXYDataset, DomainInfo,
  70. PublicCloneable {
  71. /**
  72. * The backing data structure.
  73. */
  74. private DefaultKeyedValues2D values;
  75. /** A delegate for controlling the interval width. */
  76. private IntervalXYDelegate intervalDelegate;
  77. /**
  78. * Creates a new empty CategoryTableXYDataset.
  79. */
  80. public CategoryTableXYDataset() {
  81. this.values = new DefaultKeyedValues2D(true);
  82. this.intervalDelegate = new IntervalXYDelegate(this);
  83. addChangeListener(this.intervalDelegate);
  84. }
  85. /**
  86. * Adds a data item to this dataset and sends a {@link DatasetChangeEvent}
  87. * to all registered listeners.
  88. *
  89. * @param x the x value.
  90. * @param y the y value.
  91. * @param seriesName the name of the series to add the data item.
  92. */
  93. public void add(double x, double y, String seriesName) {
  94. add(new Double(x), new Double(y), seriesName, true);
  95. }
  96. /**
  97. * Adds a data item to this dataset and, if requested, sends a
  98. * {@link DatasetChangeEvent} to all registered listeners.
  99. *
  100. * @param x the x value.
  101. * @param y the y value.
  102. * @param seriesName the name of the series to add the data item.
  103. * @param notify notify listeners?
  104. */
  105. public void add(Number x, Number y, String seriesName, boolean notify) {
  106. this.values.addValue(y, (Comparable) x, seriesName);
  107. if (notify) {
  108. fireDatasetChanged();
  109. }
  110. }
  111. /**
  112. * Removes a value from the dataset.
  113. *
  114. * @param x the x-value.
  115. * @param seriesName the series name.
  116. */
  117. public void remove(double x, String seriesName) {
  118. remove(new Double(x), seriesName, true);
  119. }
  120. /**
  121. * Removes an item from the dataset.
  122. *
  123. * @param x the x-value.
  124. * @param seriesName the series name.
  125. * @param notify notify listeners?
  126. */
  127. public void remove(Number x, String seriesName, boolean notify) {
  128. this.values.removeValue((Comparable) x, seriesName);
  129. if (notify) {
  130. fireDatasetChanged();
  131. }
  132. }
  133. /**
  134. * Returns the number of series in the collection.
  135. *
  136. * @return The series count.
  137. */
  138. public int getSeriesCount() {
  139. return this.values.getColumnCount();
  140. }
  141. /**
  142. * Returns the key for a series.
  143. *
  144. * @param series the series index (zero-based).
  145. *
  146. * @return The key for a series.
  147. */
  148. public Comparable getSeriesKey(int series) {
  149. return this.values.getColumnKey(series);
  150. }
  151. /**
  152. * Returns the number of x values in the dataset.
  153. *
  154. * @return The item count.
  155. */
  156. public int getItemCount() {
  157. return this.values.getRowCount();
  158. }
  159. /**
  160. * Returns the number of items in the specified series.
  161. * Returns the same as {@link CategoryTableXYDataset#getItemCount()}.
  162. *
  163. * @param series the series index (zero-based).
  164. *
  165. * @return The item count.
  166. */
  167. public int getItemCount(int series) {
  168. return getItemCount(); // all series have the same number of items in
  169. // this dataset
  170. }
  171. /**
  172. * Returns the x-value for the specified series and item.
  173. *
  174. * @param series the series index (zero-based).
  175. * @param item the item index (zero-based).
  176. *
  177. * @return The value.
  178. */
  179. public Number getX(int series, int item) {
  180. return (Number) this.values.getRowKey(item);
  181. }
  182. /**
  183. * Returns the starting X value for the specified series and item.
  184. *
  185. * @param series the series index (zero-based).
  186. * @param item the item index (zero-based).
  187. *
  188. * @return The starting X value.
  189. */
  190. public Number getStartX(int series, int item) {
  191. return this.intervalDelegate.getStartX(series, item);
  192. }
  193. /**
  194. * Returns the ending X value for the specified series and item.
  195. *
  196. * @param series the series index (zero-based).
  197. * @param item the item index (zero-based).
  198. *
  199. * @return The ending X value.
  200. */
  201. public Number getEndX(int series, int item) {
  202. return this.intervalDelegate.getEndX(series, item);
  203. }
  204. /**
  205. * Returns the y-value for the specified series and item.
  206. *
  207. * @param series the series index (zero-based).
  208. * @param item the item index (zero-based).
  209. *
  210. * @return The y value (possibly <code>null</code>).
  211. */
  212. public Number getY(int series, int item) {
  213. return this.values.getValue(item, series);
  214. }
  215. /**
  216. * Returns the starting Y value for the specified series and item.
  217. *
  218. * @param series the series index (zero-based).
  219. * @param item the item index (zero-based).
  220. *
  221. * @return The starting Y value.
  222. */
  223. public Number getStartY(int series, int item) {
  224. return getY(series, item);
  225. }
  226. /**
  227. * Returns the ending Y value for the specified series and item.
  228. *
  229. * @param series the series index (zero-based).
  230. * @param item the item index (zero-based).
  231. *
  232. * @return The ending Y value.
  233. */
  234. public Number getEndY(int series, int item) {
  235. return getY(series, item);
  236. }
  237. /**
  238. * Returns the minimum x-value in the dataset.
  239. *
  240. * @param includeInterval a flag that determines whether or not the
  241. * x-interval is taken into account.
  242. *
  243. * @return The minimum value.
  244. */
  245. public double getDomainLowerBound(boolean includeInterval) {
  246. return this.intervalDelegate.getDomainLowerBound(includeInterval);
  247. }
  248. /**
  249. * Returns the maximum x-value in the dataset.
  250. *
  251. * @param includeInterval a flag that determines whether or not the
  252. * x-interval is taken into account.
  253. *
  254. * @return The maximum value.
  255. */
  256. public double getDomainUpperBound(boolean includeInterval) {
  257. return this.intervalDelegate.getDomainUpperBound(includeInterval);
  258. }
  259. /**
  260. * Returns the range of the values in this dataset's domain.
  261. *
  262. * @param includeInterval a flag that determines whether or not the
  263. * x-interval is taken into account.
  264. *
  265. * @return The range.
  266. */
  267. public Range getDomainBounds(boolean includeInterval) {
  268. if (includeInterval) {
  269. return this.intervalDelegate.getDomainBounds(includeInterval);
  270. }
  271. else {
  272. return DatasetUtilities.iterateDomainBounds(this, includeInterval);
  273. }
  274. }
  275. /**
  276. * Returns the interval position factor.
  277. *
  278. * @return The interval position factor.
  279. */
  280. public double getIntervalPositionFactor() {
  281. return this.intervalDelegate.getIntervalPositionFactor();
  282. }
  283. /**
  284. * Sets the interval position factor. Must be between 0.0 and 1.0 inclusive.
  285. * If the factor is 0.5, the gap is in the middle of the x values. If it
  286. * is lesser than 0.5, the gap is farther to the left and if greater than
  287. * 0.5 it gets farther to the right.
  288. *
  289. * @param d the new interval position factor.
  290. */
  291. public void setIntervalPositionFactor(double d) {
  292. this.intervalDelegate.setIntervalPositionFactor(d);
  293. fireDatasetChanged();
  294. }
  295. /**
  296. * Returns the full interval width.
  297. *
  298. * @return The interval width to use.
  299. */
  300. public double getIntervalWidth() {
  301. return this.intervalDelegate.getIntervalWidth();
  302. }
  303. /**
  304. * Sets the interval width to a fixed value, and sends a
  305. * {@link DatasetChangeEvent} to all registered listeners.
  306. *
  307. * @param d the new interval width (must be > 0).
  308. */
  309. public void setIntervalWidth(double d) {
  310. this.intervalDelegate.setFixedIntervalWidth(d);
  311. fireDatasetChanged();
  312. }
  313. /**
  314. * Returns whether the interval width is automatically calculated or not.
  315. *
  316. * @return whether the width is automatically calculated or not.
  317. */
  318. public boolean isAutoWidth() {
  319. return this.intervalDelegate.isAutoWidth();
  320. }
  321. /**
  322. * Sets the flag that indicates whether the interval width is automatically
  323. * calculated or not.
  324. *
  325. * @param b the flag.
  326. */
  327. public void setAutoWidth(boolean b) {
  328. this.intervalDelegate.setAutoWidth(b);
  329. fireDatasetChanged();
  330. }
  331. /**
  332. * Tests this dataset for equality with an arbitrary object.
  333. *
  334. * @param obj the object (<code>null</code> permitted).
  335. *
  336. * @return A boolean.
  337. */
  338. public boolean equals(Object obj) {
  339. if (!(obj instanceof CategoryTableXYDataset)) {
  340. return false;
  341. }
  342. CategoryTableXYDataset that = (CategoryTableXYDataset) obj;
  343. if (!this.intervalDelegate.equals(that.intervalDelegate)) {
  344. return false;
  345. }
  346. if (!this.values.equals(that.values)) {
  347. return false;
  348. }
  349. return true;
  350. }
  351. /**
  352. * Returns an independent copy of this dataset.
  353. *
  354. * @return A clone.
  355. *
  356. * @throws CloneNotSupportedException if there is some reason that cloning
  357. * cannot be performed.
  358. */
  359. public Object clone() throws CloneNotSupportedException {
  360. CategoryTableXYDataset clone = (CategoryTableXYDataset) super.clone();
  361. clone.values = (DefaultKeyedValues2D) this.values.clone();
  362. clone.intervalDelegate = new IntervalXYDelegate(clone);
  363. // need to configure the intervalDelegate to match the original
  364. clone.intervalDelegate.setFixedIntervalWidth(getIntervalWidth());
  365. clone.intervalDelegate.setAutoWidth(isAutoWidth());
  366. clone.intervalDelegate.setIntervalPositionFactor(
  367. getIntervalPositionFactor());
  368. return clone;
  369. }
  370. }