Package org.jfree.data.time
Class TimeSeries
- java.lang.Object
-
- org.jfree.data.general.Series
-
- org.jfree.data.time.TimeSeries
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class TimeSeries extends Series implements java.lang.Cloneable, java.io.Serializable
Represents a sequence of zero or more data items in the form (period, value) where 'period' is some instance of a subclass ofRegularTimePeriod. The time series will ensure that (a) all data items have the same type of period (for example,Day) and (b) that each period appears at most one time in the series.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.ListdataThe list of data items in the series.protected static java.lang.StringDEFAULT_DOMAIN_DESCRIPTIONDefault value for the domain description.protected static java.lang.StringDEFAULT_RANGE_DESCRIPTIONDefault value for the range description.private java.lang.StringdomainA description of the domain.private longmaximumItemAgeThe maximum age of items for the series, specified as a number of time periods.private intmaximumItemCountThe maximum number of items for the series.private doublemaxYThe maximum y-value in the series.private doubleminYThe minimum y-value in the series.private java.lang.StringrangeA description of the range.private static longserialVersionUIDFor serialization.protected java.lang.ClasstimePeriodClassThe type of period for the data.
-
Constructor Summary
Constructors Constructor Description TimeSeries(java.lang.Comparable name)Creates a new (empty) time series.TimeSeries(java.lang.Comparable name, java.lang.String domain, java.lang.String range)Creates a new time series that contains no data.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(RegularTimePeriod period, double value)Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.voidadd(RegularTimePeriod period, double value, boolean notify)Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.voidadd(RegularTimePeriod period, java.lang.Number value)Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.voidadd(RegularTimePeriod period, java.lang.Number value, boolean notify)Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.voidadd(TimeSeriesDataItem item)Adds a data item to the series and sends aSeriesChangeEventto all registered listeners.voidadd(TimeSeriesDataItem item, boolean notify)Adds a data item to the series and sends aSeriesChangeEventto all registered listeners.TimeSeriesaddAndOrUpdate(TimeSeries series)Adds or updates data from one series to another.TimeSeriesDataItemaddOrUpdate(RegularTimePeriod period, double value)Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.TimeSeriesDataItemaddOrUpdate(RegularTimePeriod period, java.lang.Number value)Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.TimeSeriesDataItemaddOrUpdate(TimeSeriesDataItem item)Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.voidclear()Removes all data items from the series and sends aSeriesChangeEventto all registered listeners.java.lang.Objectclone()Returns a clone of the time series.TimeSeriescreateCopy(int start, int end)Creates a new timeseries by copying a subset of the data in this time series.TimeSeriescreateCopy(RegularTimePeriod start, RegularTimePeriod end)Creates a new timeseries by copying a subset of the data in this time series.voiddelete(int start, int end)Deletes data from start until end index (end inclusive).voiddelete(int start, int end, boolean notify)Deletes data from start until end index (end inclusive).voiddelete(RegularTimePeriod period)Deletes the data item for the given time period and sends aSeriesChangeEventto all registered listeners.booleanequals(java.lang.Object obj)Tests the series for equality with an arbitrary object.RangefindValueRange()Returns the range of y-values in the time series.RangefindValueRange(Range xRange, java.util.TimeZone timeZone)Returns the range of y-values in the time series that fall within the specified range of x-values.RangefindValueRange(Range xRange, TimePeriodAnchor xAnchor, java.util.Calendar calendar)Finds the range of y-values that fall within the specified range of x-values (where the x-values are interpreted as milliseconds since the epoch and converted to time periods using the specified calendar).RangefindValueRange(Range xRange, TimePeriodAnchor xAnchor, java.util.TimeZone zone)Finds the range of y-values that fall within the specified range of x-values (where the x-values are interpreted as milliseconds since the epoch and converted to time periods using the specified timezone).TimeSeriesDataItemgetDataItem(int index)Returns a data item from the dataset.TimeSeriesDataItemgetDataItem(RegularTimePeriod period)Returns the data item for a specific period.java.lang.StringgetDomainDescription()Returns the domain description.intgetIndex(RegularTimePeriod period)Returns the index for the item (if any) that corresponds to a time period.intgetItemCount()Returns the number of items in the series.java.util.ListgetItems()Returns the list of data items for the series (the list containsTimeSeriesDataItemobjects and is unmodifiable).longgetMaximumItemAge()Returns the maximum item age (in time periods) for the series.intgetMaximumItemCount()Returns the maximum number of items that will be retained in the series.doublegetMaxY()Returns the largest y-value in the series, ignoring anynullandDouble.NaNvalues.doublegetMinY()Returns the smallest y-value in the series, ignoring anynullandDouble.NaNvalues.RegularTimePeriodgetNextTimePeriod()Returns a time period that would be the next in sequence on the end of the time series.java.lang.StringgetRangeDescription()Returns the range description.(package private) TimeSeriesDataItemgetRawDataItem(int index)Returns a data item for the series.(package private) TimeSeriesDataItemgetRawDataItem(RegularTimePeriod period)Returns a data item for the series.RegularTimePeriodgetTimePeriod(int index)Returns the time period at the specified index.java.lang.ClassgetTimePeriodClass()Returns the time period class for this series.java.util.CollectiongetTimePeriods()Returns a collection of all the time periods in the time series.java.util.CollectiongetTimePeriodsUniqueToOtherSeries(TimeSeries series)Returns a collection of time periods in the specified series, but not in this series, and therefore unique to the specified series.java.lang.NumbergetValue(int index)Returns the value at the specified index.java.lang.NumbergetValue(RegularTimePeriod period)Returns the value for a time period.inthashCode()Returns a hash code value for the object.private doublemaxIgnoreNaN(double a, double b)A function to find the maximum of two values, but ignoring any Double.NaN values.private doubleminIgnoreNaN(double a, double b)A function to find the minimum of two values, but ignoring any Double.NaN values.voidremoveAgedItems(boolean notify)Age items in the series.voidremoveAgedItems(long latest, boolean notify)Age items in the series.voidsetDomainDescription(java.lang.String description)Sets the domain description and sends aPropertyChangeEvent(with the property nameDomain) to all registered property change listeners.voidsetMaximumItemAge(long periods)Sets the number of time units in the 'history' for the series.voidsetMaximumItemCount(int maximum)Sets the maximum number of items that will be retained in the series.voidsetRangeDescription(java.lang.String description)Sets the range description and sends aPropertyChangeEvent(with the property nameRange) to all registered listeners.voidupdate(int index, java.lang.Number value)Updates (changes) the value of a data item.voidupdate(RegularTimePeriod period, double value)Updates (changes) the value for a time period.voidupdate(RegularTimePeriod period, java.lang.Number value)Updates (changes) the value for a time period.private voidupdateBoundsForAddedItem(TimeSeriesDataItem item)Updates the cached values for the minimum and maximum data values.private voidupdateBoundsForRemovedItem(TimeSeriesDataItem item)Updates the cached values for the minimum and maximum data values on the basis that the specified item has just been removed.private voidupdateMinMaxYByIteration()Finds the bounds of the x and y values for the series, by iterating through all the data items.-
Methods inherited from class org.jfree.data.general.Series
addChangeListener, addPropertyChangeListener, addVetoableChangeListener, canEqual, firePropertyChange, fireSeriesChanged, fireVetoableChange, getDescription, getKey, getNotify, isEmpty, notifyListeners, removeChangeListener, removePropertyChangeListener, removeVetoableChangeListener, setDescription, setKey, setNotify
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
For serialization.- See Also:
- Constant Field Values
-
DEFAULT_DOMAIN_DESCRIPTION
protected static final java.lang.String DEFAULT_DOMAIN_DESCRIPTION
Default value for the domain description.- See Also:
- Constant Field Values
-
DEFAULT_RANGE_DESCRIPTION
protected static final java.lang.String DEFAULT_RANGE_DESCRIPTION
Default value for the range description.- See Also:
- Constant Field Values
-
domain
private java.lang.String domain
A description of the domain.
-
range
private java.lang.String range
A description of the range.
-
timePeriodClass
protected java.lang.Class timePeriodClass
The type of period for the data.
-
data
protected java.util.List data
The list of data items in the series.
-
maximumItemCount
private int maximumItemCount
The maximum number of items for the series.
-
maximumItemAge
private long maximumItemAge
The maximum age of items for the series, specified as a number of time periods.
-
minY
private double minY
The minimum y-value in the series.
-
maxY
private double maxY
The maximum y-value in the series.
-
-
Constructor Detail
-
TimeSeries
public TimeSeries(java.lang.Comparable name)
Creates a new (empty) time series. By default, a daily time series is created. Use one of the other constructors if you require a different time period.- Parameters:
name- the series name (nullnot permitted).
-
TimeSeries
public TimeSeries(java.lang.Comparable name, java.lang.String domain, java.lang.String range)Creates a new time series that contains no data.Descriptions can be specified for the domain and range. One situation where this is helpful is when generating a chart for the time series - axis labels can be taken from the domain and range description.
- Parameters:
name- the name of the series (nullnot permitted).domain- the domain description (nullpermitted).range- the range description (nullpermitted).
-
-
Method Detail
-
getDomainDescription
public java.lang.String getDomainDescription()
Returns the domain description.- Returns:
- The domain description (possibly
null). - See Also:
setDomainDescription(String)
-
setDomainDescription
public void setDomainDescription(java.lang.String description)
Sets the domain description and sends aPropertyChangeEvent(with the property nameDomain) to all registered property change listeners.- Parameters:
description- the description (nullpermitted).- See Also:
getDomainDescription()
-
getRangeDescription
public java.lang.String getRangeDescription()
Returns the range description.- Returns:
- The range description (possibly
null). - See Also:
setRangeDescription(String)
-
setRangeDescription
public void setRangeDescription(java.lang.String description)
Sets the range description and sends aPropertyChangeEvent(with the property nameRange) to all registered listeners.- Parameters:
description- the description (nullpermitted).- See Also:
getRangeDescription()
-
getItemCount
public int getItemCount()
Returns the number of items in the series.- Specified by:
getItemCountin classSeries- Returns:
- The item count.
-
getItems
public java.util.List getItems()
Returns the list of data items for the series (the list containsTimeSeriesDataItemobjects and is unmodifiable).- Returns:
- The list of data items.
-
getMaximumItemCount
public int getMaximumItemCount()
Returns the maximum number of items that will be retained in the series. The default value isInteger.MAX_VALUE.- Returns:
- The maximum item count.
- See Also:
setMaximumItemCount(int)
-
setMaximumItemCount
public void setMaximumItemCount(int maximum)
Sets the maximum number of items that will be retained in the series. If you add a new item to the series such that the number of items will exceed the maximum item count, then the FIRST element in the series is automatically removed, ensuring that the maximum item count is not exceeded.- Parameters:
maximum- the maximum (requires >= 0).- See Also:
getMaximumItemCount()
-
getMaximumItemAge
public long getMaximumItemAge()
Returns the maximum item age (in time periods) for the series.- Returns:
- The maximum item age.
- See Also:
setMaximumItemAge(long)
-
setMaximumItemAge
public void setMaximumItemAge(long periods)
Sets the number of time units in the 'history' for the series. This provides one mechanism for automatically dropping old data from the time series. For example, if a series contains daily data, you might set the history count to 30. Then, when you add a new data item, all data items more than 30 days older than the latest value are automatically dropped from the series.- Parameters:
periods- the number of time periods.- See Also:
getMaximumItemAge()
-
findValueRange
public Range findValueRange()
Returns the range of y-values in the time series. AnynullorDouble.NaNdata values in the series will be ignored (except for the special case where all data values arenull, in which case the return value isRange(Double.NaN, Double.NaN)). If the time series contains no items, this method will returnnull.- Returns:
- The range of y-values in the time series (possibly
null).
-
findValueRange
public Range findValueRange(Range xRange, java.util.TimeZone timeZone)
Returns the range of y-values in the time series that fall within the specified range of x-values. This is equivalent tofindValueRange(xRange, TimePeriodAnchor.MIDDLE, timeZone).- Parameters:
xRange- the subrange of x-values (nullnot permitted).timeZone- the time zone used to convert x-values to time periods (nullnot permitted).- Returns:
- The range.
-
findValueRange
public Range findValueRange(Range xRange, TimePeriodAnchor xAnchor, java.util.TimeZone zone)
Finds the range of y-values that fall within the specified range of x-values (where the x-values are interpreted as milliseconds since the epoch and converted to time periods using the specified timezone).- Parameters:
xRange- the subset of x-values to use (nullnot permitted).xAnchor- the anchor point for the x-values (nullnot permitted).zone- the time zone (nullnot permitted).- Returns:
- The range of y-values.
-
findValueRange
public Range findValueRange(Range xRange, TimePeriodAnchor xAnchor, java.util.Calendar calendar)
Finds the range of y-values that fall within the specified range of x-values (where the x-values are interpreted as milliseconds since the epoch and converted to time periods using the specified calendar).- Parameters:
xRange- the subset of x-values to use (nullnot permitted).xAnchor- the anchor point for the x-values (nullnot permitted).calendar- the calendar (nullnot permitted).- Returns:
- The range of y-values.
-
getMinY
public double getMinY()
Returns the smallest y-value in the series, ignoring anynullandDouble.NaNvalues. This method returnsDouble.NaNif there is no smallest y-value (for example, when the series is empty).- Returns:
- The smallest y-value.
- See Also:
getMaxY()
-
getMaxY
public double getMaxY()
Returns the largest y-value in the series, ignoring anynullandDouble.NaNvalues. This method returnsDouble.NaNif there is no largest y-value (for example, when the series is empty).- Returns:
- The largest y-value.
- See Also:
getMinY()
-
getTimePeriodClass
public java.lang.Class getTimePeriodClass()
Returns the time period class for this series.Only one time period class can be used within a single series (enforced). If you add a data item with a
Yearfor the time period, then all subsequent data items must also have aYearfor the time period.- Returns:
- The time period class (may be
nullbut only for an empty series).
-
getDataItem
public TimeSeriesDataItem getDataItem(int index)
Returns a data item from the dataset. Note that the returned object is a clone of the item in the series, so modifying it will have no effect on the data series.- Parameters:
index- the item index.- Returns:
- The data item.
-
getDataItem
public TimeSeriesDataItem getDataItem(RegularTimePeriod period)
Returns the data item for a specific period. Note that the returned object is a clone of the item in the series, so modifying it will have no effect on the data series.- Parameters:
period- the period of interest (nullnot allowed).- Returns:
- The data item matching the specified period (or
nullif there is no match). - See Also:
getDataItem(int)
-
getRawDataItem
TimeSeriesDataItem getRawDataItem(int index)
Returns a data item for the series. This method returns the object that is used for the underlying storage - you should not modify the contents of the returned value unless you know what you are doing.- Parameters:
index- the item index (zero-based).- Returns:
- The data item.
- See Also:
getDataItem(int)
-
getRawDataItem
TimeSeriesDataItem getRawDataItem(RegularTimePeriod period)
Returns a data item for the series. This method returns the object that is used for the underlying storage - you should not modify the contents of the returned value unless you know what you are doing.- Parameters:
period- the item index (zero-based).- Returns:
- The data item.
- See Also:
getDataItem(RegularTimePeriod)
-
getTimePeriod
public RegularTimePeriod getTimePeriod(int index)
Returns the time period at the specified index.- Parameters:
index- the index of the data item.- Returns:
- The time period.
-
getNextTimePeriod
public RegularTimePeriod getNextTimePeriod()
Returns a time period that would be the next in sequence on the end of the time series.- Returns:
- The next time period.
-
getTimePeriods
public java.util.Collection getTimePeriods()
Returns a collection of all the time periods in the time series.- Returns:
- A collection of all the time periods.
-
getTimePeriodsUniqueToOtherSeries
public java.util.Collection getTimePeriodsUniqueToOtherSeries(TimeSeries series)
Returns a collection of time periods in the specified series, but not in this series, and therefore unique to the specified series.- Parameters:
series- the series to check against this one.- Returns:
- The unique time periods.
-
getIndex
public int getIndex(RegularTimePeriod period)
Returns the index for the item (if any) that corresponds to a time period.- Parameters:
period- the time period (nullnot permitted).- Returns:
- The index.
-
getValue
public java.lang.Number getValue(int index)
Returns the value at the specified index.- Parameters:
index- index of a value.- Returns:
- The value (possibly
null).
-
getValue
public java.lang.Number getValue(RegularTimePeriod period)
Returns the value for a time period. If there is no data item with the specified period, this method will returnnull.- Parameters:
period- time period (nullnot permitted).- Returns:
- The value (possibly
null).
-
add
public void add(TimeSeriesDataItem item)
Adds a data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
item- the (timeperiod, value) pair (nullnot permitted).
-
add
public void add(TimeSeriesDataItem item, boolean notify)
Adds a data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
item- the (timeperiod, value) pair (nullnot permitted).notify- notify listeners?
-
add
public void add(RegularTimePeriod period, double value)
Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period (nullnot permitted).value- the value.
-
add
public void add(RegularTimePeriod period, double value, boolean notify)
Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period (nullnot permitted).value- the value.notify- notify listeners?
-
add
public void add(RegularTimePeriod period, java.lang.Number value)
Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period (nullnot permitted).value- the value (nullpermitted).
-
add
public void add(RegularTimePeriod period, java.lang.Number value, boolean notify)
Adds a new data item to the series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period (nullnot permitted).value- the value (nullpermitted).notify- notify listeners?
-
update
public void update(RegularTimePeriod period, double value)
Updates (changes) the value for a time period. Throws aSeriesExceptionif the period does not exist.- Parameters:
period- the period (nullnot permitted).value- the value.
-
update
public void update(RegularTimePeriod period, java.lang.Number value)
Updates (changes) the value for a time period. Throws aSeriesExceptionif the period does not exist.- Parameters:
period- the period (nullnot permitted).value- the value (nullpermitted).
-
update
public void update(int index, java.lang.Number value)Updates (changes) the value of a data item.- Parameters:
index- the index of the data item.value- the new value (nullpermitted).
-
addAndOrUpdate
public TimeSeries addAndOrUpdate(TimeSeries series)
Adds or updates data from one series to another. Returns another series containing the values that were overwritten.- Parameters:
series- the series to merge with this.- Returns:
- A series containing the values that were overwritten.
-
addOrUpdate
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, double value)
Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period to add/update (nullnot permitted).value- the new value.- Returns:
- A copy of the overwritten data item, or
nullif no item was overwritten.
-
addOrUpdate
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, java.lang.Number value)
Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.- Parameters:
period- the time period to add/update (nullnot permitted).value- the new value (nullpermitted).- Returns:
- A copy of the overwritten data item, or
nullif no item was overwritten.
-
addOrUpdate
public TimeSeriesDataItem addOrUpdate(TimeSeriesDataItem item)
Adds or updates an item in the times series and sends aSeriesChangeEventto all registered listeners.- Parameters:
item- the data item (nullnot permitted).- Returns:
- A copy of the overwritten data item, or
nullif no item was overwritten.
-
removeAgedItems
public void removeAgedItems(boolean notify)
Age items in the series. Ensure that the timespan from the youngest to the oldest record in the series does not exceed maximumItemAge time periods. Oldest items will be removed if required.- Parameters:
notify- controls whether or not aSeriesChangeEventis sent to registered listeners IF any items are removed.
-
removeAgedItems
public void removeAgedItems(long latest, boolean notify)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.- Parameters:
latest- the time to be compared against when aging data (specified in milliseconds).notify- controls whether or not aSeriesChangeEventis sent to registered listeners IF any items are removed.
-
clear
public void clear()
Removes all data items from the series and sends aSeriesChangeEventto all registered listeners.
-
delete
public void delete(RegularTimePeriod period)
Deletes the data item for the given time period and sends aSeriesChangeEventto all registered listeners. If there is no item with the specified time period, this method does nothing.- Parameters:
period- the period of the item to delete (nullnot permitted).
-
delete
public void delete(int start, int end)Deletes data from start until end index (end inclusive).- Parameters:
start- the index of the first period to delete.end- the index of the last period to delete.
-
delete
public void delete(int start, int end, boolean notify)Deletes data from start until end index (end inclusive).- Parameters:
start- the index of the first period to delete.end- the index of the last period to delete.notify- notify listeners?
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionReturns a clone of the time series.Notes:
- no need to clone the domain and range descriptions, since String object is immutable;
- we pass over to the more general method clone(start, end).
-
createCopy
public TimeSeries createCopy(int start, int end) throws java.lang.CloneNotSupportedException
Creates a new timeseries by copying a subset of the data in this time series.- Parameters:
start- the index of the first time period to copy.end- the index of the last time period to copy.- Returns:
- A series containing a copy of this times series from start until end.
- Throws:
java.lang.CloneNotSupportedException- if there is a cloning problem.
-
createCopy
public TimeSeries createCopy(RegularTimePeriod start, RegularTimePeriod end) throws java.lang.CloneNotSupportedException
Creates a new timeseries by copying a subset of the data in this time series.- Parameters:
start- the first time period to copy (nullnot permitted).end- the last time period to copy (nullnot permitted).- Returns:
- A time series containing a copy of this time series from start until end.
- Throws:
java.lang.CloneNotSupportedException- if there is a cloning problem.
-
equals
public boolean equals(java.lang.Object obj)
Tests the series for equality with an arbitrary object.
-
hashCode
public int hashCode()
Returns a hash code value for the object.
-
updateBoundsForAddedItem
private void updateBoundsForAddedItem(TimeSeriesDataItem item)
Updates the cached values for the minimum and maximum data values.- Parameters:
item- the item added (nullnot permitted).
-
updateBoundsForRemovedItem
private void updateBoundsForRemovedItem(TimeSeriesDataItem item)
Updates the cached values for the minimum and maximum data values on the basis that the specified item has just been removed.- Parameters:
item- the item added (nullnot permitted).
-
updateMinMaxYByIteration
private void updateMinMaxYByIteration()
Finds the bounds of the x and y values for the series, by iterating through all the data items.
-
minIgnoreNaN
private double minIgnoreNaN(double a, double b)A function to find the minimum of two values, but ignoring any Double.NaN values.- Parameters:
a- the first value.b- the second value.- Returns:
- The minimum of the two values.
-
maxIgnoreNaN
private double maxIgnoreNaN(double a, double b)A function to find the maximum of two values, but ignoring any Double.NaN values.- Parameters:
a- the first value.b- the second value.- Returns:
- The maximum of the two values.
-
-