package pl.edu.icm.saos.webapp.analysis.generator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pl.edu.icm.saos.webapp.analysis.request.UiySettings.UiyValueType;
import pl.edu.icm.saos.webapp.analysis.result.FlotChart.FlotSeries;
import com.google.common.base.Preconditions;
/**
* An implementation of {@link FlotSeriesAggregator} handling aggregation of
* {@link FlotSeries} holding y-values being rates (of occurrence of a specific phenomenon).
*
* @author Ćukasz Dumiszewski
*/
@Service("rateFlotSeriesAggregator")
class RateFlotSeriesAggregator implements FlotSeriesAggregator {
private AggregatedSeriesXValueGenerator aggregatedSeriesXValueGenerator;
//------------------------ LOGIC --------------------------
/**
* Aggregates the given series. Returns a flot series with one point.
* The x-value is generated by {{@link #setAggregatedSeriesXValueGenerator(AggregatedSeriesXValueGenerator)}
* and y-value is an arithmetic mean of y-values of the passed flotSeries.
*
* */
@Override
public FlotSeries aggregateFlotSeries(FlotSeries flotSeries, int flotSeriesIndex) {
FlotSeries aggregatedFlotSeries = new FlotSeries();
float sum = 0;
for (Number[] point : flotSeries.getPoints()) {
sum += point[1].floatValue();
}
aggregatedFlotSeries.addPoint(new Number[]{aggregatedSeriesXValueGenerator.generateXValue(flotSeriesIndex), sum/flotSeries.getPoints().size()});
return aggregatedFlotSeries;
}
@Override
public boolean handles(UiyValueType uiyValueType) {
Preconditions.checkNotNull(uiyValueType);
return UiyValueType.PERCENT == uiyValueType || UiyValueType.NUMBER_PER_1000 == uiyValueType;
}
//------------------------ SETTERS --------------------------
@Autowired
public void setAggregatedSeriesXValueGenerator(AggregatedSeriesXValueGenerator aggregatedSeriesXValueGenerator) {
this.aggregatedSeriesXValueGenerator = aggregatedSeriesXValueGenerator;
}
}