use of org.jfree.data.category.DefaultCategoryDataset in project dhis2-core by dhis2.
the class DefaultChartService method getJFreeChartHistory.
@Override
public JFreeChart getJFreeChartHistory(DataElement dataElement, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo, Period lastPeriod, OrganisationUnit organisationUnit, int historyLength, I18nFormat format) {
lastPeriod = periodService.reloadPeriod(lastPeriod);
List<Period> periods = periodService.getPeriods(lastPeriod, historyLength);
MinMaxDataElement minMax = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, categoryOptionCombo);
UnivariateInterpolator interpolator = new SplineInterpolator();
Integer periodCount = 0;
List<Double> x = new ArrayList<>();
List<Double> y = new ArrayList<>();
// ---------------------------------------------------------------------
// DataValue, MinValue and MaxValue DataSets
// ---------------------------------------------------------------------
DefaultCategoryDataset dataValueDataSet = new DefaultCategoryDataset();
DefaultCategoryDataset metaDataSet = new DefaultCategoryDataset();
for (Period period : periods) {
++periodCount;
period.setName(format.formatPeriod(period));
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
double value = 0;
if (dataValue != null && dataValue.getValue() != null && MathUtils.isNumeric(dataValue.getValue())) {
value = Double.parseDouble(dataValue.getValue());
x.add(periodCount.doubleValue());
y.add(value);
}
dataValueDataSet.addValue(value, dataElement.getShortName(), period.getName());
if (minMax != null) {
metaDataSet.addValue(minMax.getMin(), "Min value", period.getName());
metaDataSet.addValue(minMax.getMax(), "Max value", period.getName());
}
}
if (// minimum 3 points required for interpolation
x.size() >= 3) {
periodCount = 0;
double[] xa = getArray(x);
int min = MathUtils.getMin(xa).intValue();
int max = MathUtils.getMax(xa).intValue();
try {
UnivariateFunction function = interpolator.interpolate(xa, getArray(y));
for (Period period : periods) {
if (++periodCount >= min && periodCount <= max) {
metaDataSet.addValue(function.value(periodCount), "Regression value", period.getName());
}
}
} catch (MathRuntimeException ex) {
throw new RuntimeException("Failed to interpolate", ex);
}
}
// ---------------------------------------------------------------------
// Plots
// ---------------------------------------------------------------------
CategoryPlot plot = getCategoryPlot(dataValueDataSet, getBarRenderer(), PlotOrientation.VERTICAL, CategoryLabelPositions.UP_45);
plot.setDataset(1, metaDataSet);
plot.setRenderer(1, getLineRenderer());
JFreeChart jFreeChart = getBasicJFreeChart(plot);
return jFreeChart;
}
Aggregations