BasicTimeSeries Addition

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rahulb

BasicTimeSeries Addition

Post by rahulb » Tue Dec 17, 2002 8:55 pm

Hi there,

I was using BasicTimeSeries and wanted to sum quantities two datasets. BasicTimeSeries.addAndOrUpdate() doesn't do it, and I didn't see another method that would work, so I added the code below (copying addAndOrUpdate). Thought it might be useful to others:

/**
* Adds or sums data from one series and another. Returns another series
* containing the values that were overwritten.
*
* @param series the series to merge with this.
*
* @return series containing the values that were overwritten.
*/
public BasicTimeSeries addAndOrSum(BasicTimeSeries series) {

BasicTimeSeries overwritten = new BasicTimeSeries("Overwritten values from: " + getName(),timePeriodClass);

for (int i = 0; i < series.getItemCount(); i++) {
TimeSeriesDataPair pair = series.getDataPair(i);
TimeSeriesDataPair oldPair = addOrSum(pair.getPeriod(), pair.getValue());
if (oldPair != null) {
try {
overwritten.add(oldPair);
}
catch (SeriesException e) { // should not get here...
e.printStackTrace();
System.err.println("TimeSeries.addAndOrSum(series): "
+ "unable to add data to overwritten series.");
}
}
}

return overwritten;

}

/**
* Adds or sums the times series.
*
* @param period the time period to add/update.
* @param value the new value.
*
* @return A copy of the overwritten data pair (or null).
*/
public TimeSeriesDataPair addOrSum(TimePeriod period, Number value) {

TimeSeriesDataPair overwritten = null;

TimeSeriesDataPair key = new TimeSeriesDataPair(period, value);
int index = Collections.binarySearch(data, key);
if (index >= 0) {
TimeSeriesDataPair existing = (TimeSeriesDataPair) data.get(index);
overwritten = (TimeSeriesDataPair) existing.clone();
Number oldValue = existing.getValue();
existing.setValue( addNumbers(oldValue,value) );
fireSeriesChanged();
}
else {
data.add(-index - 1, new TimeSeriesDataPair(period, value));
}

return overwritten;

}

/**
* Return a Number object that is the sum of two of the same
* subclass of Number.
*
* @param a the first Number
* @param b the second Number
*
* @return the additive sum of the two
*/
public static Number addNumbers(Number a, Number b){
if(a.getClass() != b.getClass()){
System.err.println("addNumbers: "+"Numbers not same type.");
return new Integer(-1);
}
if(a instanceof Byte){
return new Byte(
(byte)((byte)a.byteValue()+(byte)b.byteValue()));
} else if (a instanceof Double){
return new Double(a.doubleValue()+b.doubleValue());
} else if (a instanceof Float){
return new Float(a.floatValue()+b.floatValue());
} else if (a instanceof Integer){
return new Integer(a.intValue()+b.intValue());
} else if (a instanceof Long){
return new Long(a.longValue()+b.longValue());
} else if (a instanceof Short){
return new Short(
(short)((short)a.shortValue()+(short)b.shortValue()));
}
System.err.println("addNumbers: "+"Unknown Number format.");
return null;
}

David Gilbert

Re: BasicTimeSeries Addition

Post by David Gilbert » Wed Dec 18, 2002 9:53 am

Thanks for posting your code. If it is something that others want to use, let me know and I'll incorporate it into the JFreeChart source code...

Regards,

DG.

Locked