timeseries.java

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

JAVA
894
字号
     * Adds a data item to the series and sends a      * {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param item  the (timeperiod, value) pair (<code>null</code> not      *              permitted).     */    public void add(TimeSeriesDataItem item) {        if (item == null) {            throw new IllegalArgumentException("Null 'item' argument.");        }        if (!item.getPeriod().getClass().equals(this.timePeriodClass)) {            StringBuffer b = new StringBuffer();            b.append("You are trying to add data where the time period class ");            b.append("is ");            b.append(item.getPeriod().getClass().getName());            b.append(", but the TimeSeries is expecting an instance of ");            b.append(this.timePeriodClass.getName());            b.append(".");            throw new SeriesException(b.toString());        }        // make the change (if it's not a duplicate time period)...        boolean added = false;        int count = getItemCount();        if (count == 0) {            this.data.add(item);            added = true;        }        else {            RegularTimePeriod last = getTimePeriod(getItemCount() - 1);            if (item.getPeriod().compareTo(last) > 0) {                this.data.add(item);                added = true;            }            else {                int index = Collections.binarySearch(this.data, item);                if (index < 0) {                    this.data.add(-index - 1, item);                    added = true;                }                else {                    StringBuffer b = new StringBuffer();                    b.append("You are attempting to add an observation for ");                    b.append("the time period ");                    b.append(item.getPeriod().toString());                    b.append(" but the series already contains an observation");                    b.append(" for that time period. Duplicates are not ");                    b.append("permitted.  Try using the addOrUpdate() method.");                    throw new SeriesException(b.toString());                }            }        }        if (added) {            // check if this addition will exceed the maximum item count...            if (getItemCount() > this.maximumItemCount) {                this.data.remove(0);            }            // check if there are any values earlier than specified by the            // history count...            ageHistoryCountItems();            fireSeriesChanged();        }    }    /**     * Adds a new data item to the series and sends      * a {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param period  the time period (<code>null</code> not permitted).     * @param value  the value.     */    public void add(RegularTimePeriod period, double value) {        // defer argument checking...        TimeSeriesDataItem item = new TimeSeriesDataItem(period, value);        add(item);    }    /**     * Adds a new data item to the series and sends      * a {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param period  the time period (<code>null</code> not permitted).     * @param value  the value (<code>null</code> permitted).     */    public void add(RegularTimePeriod period, Number value) {        // defer argument checking...        TimeSeriesDataItem item = new TimeSeriesDataItem(period, value);        add(item);    }    /**     * Updates (changes) the value for a time period.  Throws a      * {@link SeriesException} if the period does not exist.     *     * @param period  the period (<code>null</code> not permitted).     * @param value  the value (<code>null</code> permitted).     */    public void update(RegularTimePeriod period, Number value) {        TimeSeriesDataItem temp = new TimeSeriesDataItem(period, value);        int index = Collections.binarySearch(this.data, temp);        if (index >= 0) {            TimeSeriesDataItem pair = (TimeSeriesDataItem) this.data.get(index);            pair.setValue(value);            fireSeriesChanged();        }        else {            throw new SeriesException(                "TimeSeries.update(TimePeriod, Number):  period does not exist."            );        }    }    /**     * Updates (changes) the value of a data item.     *     * @param index  the index of the data item.     * @param value  the new value (<code>null</code> permitted).     */    public void update(int index, Number value) {        TimeSeriesDataItem item = getDataItem(index);        item.setValue(value);        fireSeriesChanged();    }    /**     * Adds or updates data from one series to another.  Returns another series     * containing the values that were overwritten.     *     * @param series  the series to merge with this.     *     * @return A series containing the values that were overwritten.     */    public TimeSeries addAndOrUpdate(TimeSeries series) {        TimeSeries overwritten = new TimeSeries(            "Overwritten values from: " + getKey(), series.getTimePeriodClass()        );        for (int i = 0; i < series.getItemCount(); i++) {            TimeSeriesDataItem item = series.getDataItem(i);            TimeSeriesDataItem oldItem = addOrUpdate(                item.getPeriod(), item.getValue()            );            if (oldItem != null) {                overwritten.add(oldItem);            }        }        return overwritten;    }    /**     * Adds or updates an item in the times series and sends a      * {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param period  the time period to add/update (<code>null</code> not      *                permitted).     * @param value  the new value.     *     * @return A copy of the overwritten data item, or <code>null</code> if no      *         item was overwritten.     */    public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period,                                           double value) {        return this.addOrUpdate(period, new Double(value));        }        /**     * Adds or updates an item in the times series and sends a      * {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param period  the time period to add/update (<code>null</code> not      *                permitted).     * @param value  the new value (<code>null</code> permitted).     *     * @return A copy of the overwritten data item, or <code>null</code> if no      *         item was overwritten.     */    public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period,                                           Number value) {        if (period == null) {            throw new IllegalArgumentException("Null 'period' argument.");           }        TimeSeriesDataItem overwritten = null;        TimeSeriesDataItem key = new TimeSeriesDataItem(period, value);        int index = Collections.binarySearch(this.data, key);        if (index >= 0) {            TimeSeriesDataItem existing                 = (TimeSeriesDataItem) this.data.get(index);            overwritten = (TimeSeriesDataItem) existing.clone();            existing.setValue(value);            ageHistoryCountItems();            fireSeriesChanged();        }        else {            this.data.add(-index - 1, new TimeSeriesDataItem(period, value));            // check if this addition will exceed the maximum item count...            if (getItemCount() > this.maximumItemCount) {                this.data.remove(0);            }            ageHistoryCountItems();            fireSeriesChanged();        }        return overwritten;    }    /**     * Age items in the series.  Ensure that the timespan from the youngest to      * the oldest record in the series does not exceed history count.  oldest      * items will be removed if required.     */    public void ageHistoryCountItems() {        // check if there are any values earlier than specified by the history         // count...        if ((getItemCount() > 1) && (this.historyCount > 0)) {            long latest = getTimePeriod(getItemCount() - 1).getSerialIndex();            while ((latest - getTimePeriod(0).getSerialIndex())                     >= this.historyCount) {                this.data.remove(0);            }        }    }    /**     * Age items in the series.  Ensure that the timespan from the supplied      * time to the oldest record in the series does not exceed history count.       * oldest items will be removed if required.     *     * @param latest  the time to be compared against when aging data.     */    public void ageHistoryCountItems(long latest) {        // check if there are any values earlier than specified by the history         // count...        if ((getItemCount() > 1) && (this.historyCount > 0)) {            while ((latest - getTimePeriod(0).getSerialIndex())                     >= this.historyCount) {                this.data.remove(0);            }        }    }    /**     * Removes all data items from the series and sends      * a {@link org.jfree.data.general.SeriesChangeEvent}     * to all registered listeners.     */    public void clear() {        if (this.data.size() > 0) {            this.data.clear();            fireSeriesChanged();        }    }    /**     * Deletes the data item for the given time period and sends      * a {@link org.jfree.data.general.SeriesChangeEvent} to all registered      * listeners.     *     * @param period  the period of the item to delete (<code>null</code> not      *                permitted).     */    public void delete(RegularTimePeriod period) {        int index = getIndex(period);        this.data.remove(index);        fireSeriesChanged();    }    /**     * Deletes data from start until end index (end inclusive).     *     * @param start  the index of the first period to delete.     * @param end  the index of the last period to delete.     */    public void delete(int start, int end) {        for (int i = 0; i <= (end - start); i++) {            this.data.remove(start);        }        fireSeriesChanged();    }    /**     * Returns a clone of the time series.     * <P>     * Notes:     * <ul>     *   <li>no need to clone the domain and range descriptions, since String      *     object is immutable;</li>     *   <li>we pass over to the more general method clone(start, end).</li>     * </ul>     *     * @return A clone of the time series.     *      * @throws CloneNotSupportedException not thrown by this class, but      *         subclasses may differ.     */    public Object clone() throws CloneNotSupportedException {        Object clone = createCopy(0, getItemCount() - 1);        return clone;    }    /**     * Creates a new timeseries by copying a subset of the data in this time     * series.     *     * @param start  the index of the first time period to copy.     * @param end  the index of the last time period to copy.     *     * @return A series containing a copy of this times series from start until     *         end.     *      * @throws CloneNotSupportedException if there is a cloning problem.     */    public TimeSeries createCopy(int start, int end)         throws CloneNotSupportedException {        TimeSeries copy = (TimeSeries) super.clone();        copy.data = new java.util.ArrayList();        if (this.data.size() > 0) {            for (int index = start; index <= end; index++) {                TimeSeriesDataItem item                     = (TimeSeriesDataItem) this.data.get(index);                TimeSeriesDataItem clone = (TimeSeriesDataItem) item.clone();                try {                    copy.add(clone);                }                catch (SeriesException e) {                    System.err.println("Unable to add cloned data item.");                }            }        }        return copy;    }    /**     * Creates a new timeseries by copying a subset of the data in this time      * series.     *     * @param start  the first time period to copy.     * @param end  the last time period to copy.     *     * @return A time series containing a copy of this time series from start      *         until end.     *      * @throws CloneNotSupportedException if there is a cloning problem.     */    public TimeSeries createCopy(RegularTimePeriod start, RegularTimePeriod end)        throws CloneNotSupportedException {        int startIndex = getIndex(start);        if (startIndex < 0) {            startIndex = -(startIndex + 1);        }        int endIndex = getIndex(end);        if (endIndex < 0) {             // end period is not in original series            endIndex = -(endIndex + 1); // this is first item AFTER end period            endIndex = endIndex - 1;    // so this is last item BEFORE end         }                TimeSeries result = createCopy(startIndex, endIndex);                return result;    }    /**     * Tests the series for equality with an arbitrary object.     *     * @param object  the object to test against (<code>null</code> permitted).     *     * @return A boolean.     */    public boolean equals(Object object) {        if (object == this) {            return true;        }        if (!(object instanceof TimeSeries) || !super.equals(object)) {            return false;        }        TimeSeries s = (TimeSeries) object;        if (!ObjectUtilities.equal(            getDomainDescription(), s.getDomainDescription()        )) {            return false;        }        if (!ObjectUtilities.equal(            getRangeDescription(), s.getRangeDescription()        )) {            return false;        }        if (!getClass().equals(s.getClass())) {            return false;        }        if (getHistoryCount() != s.getHistoryCount()) {            return false;        }        if (getMaximumItemCount() != s.getMaximumItemCount()) {            return false;        }        int count = getItemCount();        if (count != s.getItemCount()) {            return false;        }        for (int i = 0; i < count; i++) {            if (!getDataItem(i).equals(s.getDataItem(i))) {                return false;            }        }        return true;    }    /**     * Returns a hash code value for the object.     *     * @return The hashcode     */    public int hashCode() {        int result;        result = (this.domain != null ? this.domain.hashCode() : 0);        result = 29 * result + (this.range != null ? this.range.hashCode() : 0);        result = 29 * result + (this.timePeriodClass != null                     ? this.timePeriodClass.hashCode() : 0);        result = 29 * result + this.data.hashCode();        result = 29 * result + this.maximumItemCount;        result = 29 * result + this.historyCount;        return result;    }}

⌨️ 快捷键说明

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