/** * 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; }
/** * 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 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(); }