[#2822] - Parametrized incrementer

Category:
Feature request
Priority:
High
Status:
New
Project: Severity:
Minor
Resolution:
Open
Component: Reproducibility:
N/A
Assigned to:

I would like to be able to have a parameterized incrementer.

I am currently using a the RunningAverageIncrementer described in the following StackOverflow question to calculate the trend in a graph.

My problem is that I would like the window size to be configurable, so it would be for instance set through another parameter in the report or dataset. But I don't see how to do that using the current api. At the moment I need to have its value hardcoded in the incrementer class.

Thanks

M

mollymay's picture
Joined: Mar 12 2013 - 6:17am
Last seen: 10 years 2 months ago

1 Comment:

#1
  • Assigned:nobody»

Sorry, it seemed that the link was not included. I paste here the RunningAverageIncrementer class

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.fill.AbstractValueProvider;
import net.sf.jasperreports.engine.fill.JRFillVariable;
import net.sf.jasperreports.engine.fill.JRIncrementer;

/**
* Used by an iReport variable to increment its average.
*/
public class RunningAverageIncrementer
implements JRIncrementer {
/** Default number of tallies. */
private static final int DEFAULT_TALLIES = 128;

/** Number of tallies within the sliding window. */
private static final int DEFAULT_SLIDING_WINDOW_SIZE = 30;

/** Stores a sliding window of values. */
private List<Double> values = new ArrayList<Double>( DEFAULT_TALLIES );

/**
* Instantiated by the RunningAverageIncrementerFactory class.
*/
public RunningAverageIncrementer() {
}

/**
* Calculates the average of previously known values.
* @return The average of the list of values returned by getValues().
*/
private double calculateAverage() {
double result = 0.0;
List<Double> values = getValues();

for( Double d: getValues() ) {
result += d.doubleValue();
}

return result / values.size();
}

/**
* Called each time a new value to be averaged is received.
* @param value The new value to include for the average.
*/
private void recordValue( Double value ) {
List<Double> values = getValues();

// Throw out old values that should no longer influence the trend.
//
if( values.size() > getSlidingWindowSize() ) {
values.remove( 0 );
}

this.values.add( value );
}

private List<Double> getValues() {
return values;
}

private int getIterations() {
return getValues().size();
}

/**
* Returns the newly incremented value, which is calculated by averaging
* the previous value from the previous call to this method.
*
* @param jrFillVariable Unused.
* @param tally New data point to average.
* @param abstractValueProvider Unused.
* @return The newly incremented value.
*/
public Object increment( JRFillVariable jrFillVariable, Object tally,
AbstractValueProvider abstractValueProvider ) {
double value = ((Number)tally).doubleValue();

recordValue( value );

double previousAverage = calculateAverage();
double newAverage =
((value - previousAverage) / (getIterations() + 1)) + previousAverage;

return new BigDecimal( newAverage );
}

protected int getSlidingWindowSize() {
return DEFAULT_SLIDING_WINDOW_SIZE;
}
}

Feedback
randomness