/** * Updates the cached values for the minimum and maximum data values on the basis that the * specified item has just been removed. * * @param item the item added (<code>null</code> not permitted). * @since 1.0.13 */ private void updateBoundsForRemovedItem(XYDataItem item) { boolean itemContributesToXBounds = false; boolean itemContributesToYBounds = false; double x = item.getXValue(); if (!Double.isNaN(x)) { if (x <= this.minX || x >= this.maxX) { itemContributesToXBounds = true; } } if (item.getY() != null) { double y = item.getYValue(); if (!Double.isNaN(y)) { if (y <= this.minY || y >= this.maxY) { itemContributesToYBounds = true; } } } if (itemContributesToYBounds) { findBoundsByIteration(); } else if (itemContributesToXBounds) { if (getAutoSort()) { this.minX = getX(0).doubleValue(); this.maxX = getX(getItemCount() - 1).doubleValue(); } else { findBoundsByIteration(); } } }
/** * Updates the cached values for the minimum and maximum data values. * * @param item the item added (<code>null</code> not permitted). * @since 1.0.13 */ private void updateBoundsForAddedItem(XYDataItem item) { double x = item.getXValue(); this.minX = minIgnoreNaN(this.minX, x); this.maxX = maxIgnoreNaN(this.maxX, x); if (item.getY() != null) { double y = item.getYValue(); this.minY = minIgnoreNaN(this.minY, y); this.maxY = maxIgnoreNaN(this.maxY, y); } }
/** * Returns the index of the item with the specified x-value, or a negative index if the series * does not contain an item with that x-value. Be aware that for an unsorted series, the index is * found by iterating through all items in the series. * * @param x the x-value (<code>null</code> not permitted). * @return The index. */ public int indexOf(Number x) { if (this.autoSort) { return Collections.binarySearch(this.data, new XYDataItem(x, null)); } else { for (int i = 0; i < this.data.size(); i++) { XYDataItem item = (XYDataItem) this.data.get(i); if (item.getX().equals(x)) { return i; } } return -1; } }
/** * Creates a new series by copying a subset of the data in this time series. * * @param start the index of the first item to copy. * @param end the index of the last item to copy. * @return A series containing a copy of this series from start until end. * @throws CloneNotSupportedException if there is a cloning problem. */ public XYSeries createCopy(int start, int end) throws CloneNotSupportedException { XYSeries copy = (XYSeries) super.clone(); copy.data = new java.util.ArrayList(); if (this.data.size() > 0) { for (int index = start; index <= end; index++) { XYDataItem item = (XYDataItem) this.data.get(index); XYDataItem clone = (XYDataItem) item.clone(); try { copy.add(clone); } catch (SeriesException e) { System.err.println("Unable to add cloned data item."); } } } return copy; }
/** * Adds or updates an item in the series and sends a {@link SeriesChangeEvent} to all registered * listeners. * * @param item the data item (<code>null</code> not permitted). * @return A copy of the overwritten data item, or <code>null</code> if no item was overwritten. * @since 1.0.14 */ public XYDataItem addOrUpdate(XYDataItem item) { ParamChecks.nullNotPermitted(item, "item"); if (this.allowDuplicateXValues) { add(item); return null; } // if we get to here, we know that duplicate X values are not permitted XYDataItem overwritten = null; int index = indexOf(item.getX()); if (index >= 0) { XYDataItem existing = (XYDataItem) this.data.get(index); overwritten = (XYDataItem) existing.clone(); // figure out if we need to iterate through all the y-values boolean iterate = false; double oldY = existing.getYValue(); if (!Double.isNaN(oldY)) { iterate = oldY <= this.minY || oldY >= this.maxY; } existing.setY(item.getY()); if (iterate) { findBoundsByIteration(); } else if (item.getY() != null) { double yy = item.getY().doubleValue(); this.minY = minIgnoreNaN(this.minY, yy); this.maxY = maxIgnoreNaN(this.maxY, yy); } } else { // if the series is sorted, the negative index is a result from // Collections.binarySearch() and tells us where to insert the // new item...otherwise it will be just -1 and we should just // append the value to the list... item = (XYDataItem) item.clone(); if (this.autoSort) { this.data.add(-index - 1, item); } else { this.data.add(item); } updateBoundsForAddedItem(item); // check if this addition will exceed the maximum item count... if (getItemCount() > this.maximumItemCount) { XYDataItem removed = (XYDataItem) this.data.remove(0); updateBoundsForRemovedItem(removed); } } fireSeriesChanged(); return overwritten; }
/** * Adds a data item to the series and, if requested, sends a {@link SeriesChangeEvent} to all * registered listeners. * * @param item the (x, y) item (<code>null</code> not permitted). * @param notify a flag that controls whether or not a {@link SeriesChangeEvent} is sent to all * registered listeners. */ public void add(XYDataItem item, boolean notify) { ParamChecks.nullNotPermitted(item, "item"); item = (XYDataItem) item.clone(); if (this.autoSort) { int index = Collections.binarySearch(this.data, item); if (index < 0) { this.data.add(-index - 1, item); } else { if (this.allowDuplicateXValues) { // need to make sure we are adding *after* any duplicates int size = this.data.size(); while (index < size && item.compareTo(this.data.get(index)) == 0) { index++; } if (index < this.data.size()) { this.data.add(index, item); } else { this.data.add(item); } } else { throw new SeriesException("X-value already exists."); } } } else { if (!this.allowDuplicateXValues) { // can't allow duplicate values, so we need to check whether // there is an item with the given x-value already int index = indexOf(item.getX()); if (index >= 0) { throw new SeriesException("X-value already exists."); } } this.data.add(item); } updateBoundsForAddedItem(item); if (getItemCount() > this.maximumItemCount) { XYDataItem removed = (XYDataItem) this.data.remove(0); updateBoundsForRemovedItem(removed); } if (notify) { fireSeriesChanged(); } }
/** * Updates the value of an item in the series and sends a {@link SeriesChangeEvent} to all * registered listeners. * * @param index the item (zero based index). * @param y the new value (<code>null</code> permitted). * @deprecated Renamed {@link #updateByIndex(int, Number)} to avoid confusion with the {@link * #update(Number, Number)} method. */ public void update(int index, Number y) { XYDataItem item = getRawDataItem(index); // figure out if we need to iterate through all the y-values boolean iterate = false; double oldY = item.getYValue(); if (!Double.isNaN(oldY)) { iterate = oldY <= this.minY || oldY >= this.maxY; } item.setY(y); if (iterate) { findBoundsByIteration(); } else if (y != null) { double yy = y.doubleValue(); this.minY = minIgnoreNaN(this.minY, yy); this.maxY = maxIgnoreNaN(this.maxY, yy); } fireSeriesChanged(); }
/** * Returns a hash code. * * @return A hash code. */ @Override public int hashCode() { int result = super.hashCode(); // it is too slow to look at every data item, so let's just look at // the first, middle and last items... int count = getItemCount(); if (count > 0) { XYDataItem item = getRawDataItem(0); result = 29 * result + item.hashCode(); } if (count > 1) { XYDataItem item = getRawDataItem(count - 1); result = 29 * result + item.hashCode(); } if (count > 2) { XYDataItem item = getRawDataItem(count / 2); result = 29 * result + item.hashCode(); } result = 29 * result + this.maximumItemCount; result = 29 * result + (this.autoSort ? 1 : 0); result = 29 * result + (this.allowDuplicateXValues ? 1 : 0); return result; }
/** * Returns the y-value for the specified series and item. * * @param series the series (zero-based index). * @param index the index of the item of interest (zero-based). * @return The value (possibly <code>null</code>). */ public Number getY(int series, int index) { XYSeries ts = (XYSeries) this.data.get(series); XYDataItem xyItem = ts.getDataItem(index); return xyItem.getY(); }
/** * Returns the x-value for the specified series and item. * * @param series the series (zero-based index). * @param item the item (zero-based index). * @return The x-value for the specified series and item. */ public Number getX(int series, int item) { XYSeries s = (XYSeries) this.data.get(series); XYDataItem dataItem = s.getDataItem(item); return dataItem.getX(); }
/** * Return the data item with the specified index. * * @param index the index. * @return The data item with the specified index. */ public XYDataItem getDataItem(int index) { XYDataItem item = (XYDataItem) this.data.get(index); return (XYDataItem) item.clone(); }