xyseries.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 667 行 · 第 1/2 页

JAVA
667
字号
    public void add(XYDataItem item, boolean notify) {        if (item == null) {            throw new IllegalArgumentException("Null 'item' argument.");        }        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);        }        if (getItemCount() > this.maximumItemCount) {            this.data.remove(0);        }                            if (notify) {            fireSeriesChanged();        }    }    /**     * Deletes a range of items from the series and sends a      * {@link SeriesChangeEvent} to all registered listeners.     *     * @param start  the start index (zero-based).     * @param end  the end index (zero-based).     */    public void delete(int start, int end) {        for (int i = start; i <= end; i++) {            this.data.remove(start);        }        fireSeriesChanged();    }    /**     * Removes the item at the specified index and sends a      * {@link SeriesChangeEvent} to all registered listeners.     *      * @param index  the index.     *      * @return The item removed.     */    public XYDataItem remove(int index) {        XYDataItem result = (XYDataItem) this.data.remove(index);        fireSeriesChanged();        return result;    }        /**     * Removes the item with the specified x-value and sends a      * {@link SeriesChangeEvent} to all registered listeners.     *      * @param x  the x-value.     * @return The item removed.     */    public XYDataItem remove(Number x) {        return remove(indexOf(x));    }        /**     * Removes all data items from the series.     */    public void clear() {        if (this.data.size() > 0) {            this.data.clear();            fireSeriesChanged();        }    }    /**     * 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) {        return (XYDataItem) this.data.get(index);    }    /**     * Returns the x-value at the specified index.     *     * @param index  the index (zero-based).     *     * @return The x-value (never <code>null</code>).     */    public Number getX(int index) {        return getDataItem(index).getX();    }    /**     * Returns the y-value at the specified index.     *     * @param index  the index (zero-based).     *     * @return The y-value (possibly <code>null</code>).     */    public Number getY(int index) {        return getDataItem(index).getY();    }    /**     * 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).     */    public void update(int index, Number y) {        XYDataItem item = getDataItem(index);        item.setY(y);        fireSeriesChanged();    }        /**     * Updates an item in the series.     *      * @param x  the x-value (<code>null</code> not permitted).     * @param y  the y-value (<code>null</code> permitted).     *      * @throws SeriesException if there is no existing item with the specified     *         x-value.     */    public void update(Number x, Number y) {        int index = indexOf(x);        if (index < 0) {            throw new SeriesException("No observation for x = " + x);        }        else {            XYDataItem item = getDataItem(index);            item.setY(y);            fireSeriesChanged();        }    }        /**     * Adds or updates an item in the series and sends a      * {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param x  the x-value (<code>null</code> not permitted).     * @param y  the y-value (<code>null</code> permitted).     *     * @return A copy of the overwritten data item, or <code>null</code> if no      *         item was overwritten.     */    public XYDataItem addOrUpdate(Number x, Number y) {        if (x == null) {            throw new IllegalArgumentException("Null 'x' argument.");           }        XYDataItem overwritten = null;        int index = indexOf(x);        if (index >= 0) {            XYDataItem existing = (XYDataItem) this.data.get(index);            try {                overwritten = (XYDataItem) existing.clone();            }            catch (CloneNotSupportedException e) {                throw new SeriesException("Couldn't clone XYDataItem!");               }            existing.setY(y);        }        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...            if (this.autoSort) {                this.data.add(-index - 1, new XYDataItem(x, y));            }            else {                this.data.add(new XYDataItem(x, y));            }            // check if this addition will exceed the maximum item count...            if (getItemCount() > this.maximumItemCount) {                this.data.remove(0);            }        }                    fireSeriesChanged();        return overwritten;    }    /**     * 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;        }    }         /**     * Returns a clone of the series.     *     * @return A clone of the time series.     *      * @throws CloneNotSupportedException if there is a cloning problem.     */    public Object clone() throws CloneNotSupportedException {        Object clone = createCopy(0, getItemCount() - 1);        return clone;    }    /**     * 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;    }    /**     * Tests this series for equality with an arbitrary object.     *     * @param obj  the object to test against for equality      *             (<code>null</code> permitted).     *     * @return A boolean.     */    public boolean equals(Object obj) {        if (obj == this) {            return true;        }        if (!(obj instanceof XYSeries)) {            return false;        }        if (!super.equals(obj)) {            return false;        }        XYSeries that = (XYSeries) obj;        if (this.maximumItemCount != that.maximumItemCount) {            return false;        }        if (this.autoSort != that.autoSort) {            return false;        }        if (this.allowDuplicateXValues != that.allowDuplicateXValues) {            return false;        }        if (!ObjectUtilities.equal(this.data, that.data)) {            return false;        }        return true;    }        /**     * Returns a hash code.     *      * @return A hash code.     */    public int hashCode() {        int result = super.hashCode();        result = 29 * result + (this.data != null ? this.data.hashCode() : 0);        result = 29 * result + this.maximumItemCount;        result = 29 * result + (this.autoSort ? 1 : 0);        result = 29 * result + (this.allowDuplicateXValues ? 1 : 0);        return result;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?