use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class GridUtils method toCsv.
/**
* Writes a CSV representation of the given Grid to the given OutputStream.
*/
public static void toCsv(Grid grid, Writer writer) throws IOException {
if (grid == null) {
return;
}
CsvWriter csvWriter = new CsvWriter(writer, CSV_DELIMITER);
Iterator<GridHeader> headers = grid.getHeaders().iterator();
if (!grid.getHeaders().isEmpty()) {
while (headers.hasNext()) {
csvWriter.write(headers.next().getColumn());
}
csvWriter.endRecord();
}
for (List<Object> row : grid.getRows()) {
for (Object value : row) {
csvWriter.write(value != null ? String.valueOf(maybeFormat(value)) : StringUtils.EMPTY);
}
csvWriter.endRecord();
}
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method addRegressionColumn.
@Override
public Grid addRegressionColumn(int columnIndex, boolean addHeader) {
verifyGridState();
SimpleRegression regression = new SimpleRegression();
List<Object> column = getColumn(columnIndex);
int index = 0;
for (Object value : column) {
if (value != null && !MathUtils.isEqual(Double.parseDouble(String.valueOf(value)), 0d)) {
regression.addData(index++, Double.parseDouble(String.valueOf(value)));
}
}
List<Object> regressionColumn = new ArrayList<>();
for (int i = 0; i < column.size(); i++) {
final double predicted = regression.predict(i);
if (!Double.isNaN(predicted)) {
regressionColumn.add(Precision.round(predicted, 1));
} else {
regressionColumn.add(null);
}
}
addColumn(regressionColumn);
if (addHeader && columnIndex < headers.size()) {
GridHeader header = headers.get(columnIndex);
if (header != null) {
GridHeader regressionHeader = new GridHeader(header.getName() + REGRESSION_SUFFIX, header.getColumn() + REGRESSION_SUFFIX, header.getValueType(), header.isHidden(), header.isMeta());
addHeader(regressionHeader);
}
}
return this;
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method addCumulativeColumn.
@Override
public Grid addCumulativeColumn(int columnIndex, boolean addHeader) {
verifyGridState();
List<Object> column = getColumn(columnIndex);
List<Object> cumulativeColumn = new ArrayList<>();
double sum = 0d;
for (Object value : column) {
double number = value != null ? Double.parseDouble(String.valueOf(value)) : 0d;
sum += number;
cumulativeColumn.add(sum);
}
addColumn(cumulativeColumn);
if (addHeader && columnIndex < headers.size()) {
GridHeader header = headers.get(columnIndex);
if (header != null) {
GridHeader regressionHeader = new GridHeader(header.getName() + CUMULATIVE_SUFFIX, header.getColumn() + CUMULATIVE_SUFFIX, header.getValueType(), header.isHidden(), header.isMeta());
addHeader(regressionHeader);
}
}
return this;
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method toString.
// -------------------------------------------------------------------------
// toString
// -------------------------------------------------------------------------
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[\n");
if (headers != null && headers.size() > 0) {
List<String> headerNames = new ArrayList<>();
for (GridHeader header : headers) {
headerNames.add(header.getName());
}
builder.append(headerNames).append("\n");
}
for (List<Object> row : grid) {
builder.append(row).append("\n");
}
return builder.append("]").toString();
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method repositionHeaders.
/**
* Re-order the GridHeaders of the given Grid based on the List headers. The
* final Grid will have the all its headers defined in the same order as the
* given List of headers.
*
* @param headers
* @return a Set of indexes that holds the holds the new order
*/
@Override
public Set<Integer> repositionHeaders(final Set<String> headers) {
verifyGridState();
final List<String> gridHeaders = getHeaders().stream().map(GridHeader::getName).collect(toList());
final List<GridHeader> orderedHeaders = new ArrayList<>();
final Set<Integer> newColumnIndexes = new LinkedHashSet<>();
for (final String header : headers) {
if (gridHeaders.contains(header)) {
final int gridHeaderIndex = getIndexOfHeader(header);
orderedHeaders.add(getHeaders().get(gridHeaderIndex));
newColumnIndexes.add(gridHeaderIndex);
} else {
throw new IllegalQueryException(new ErrorMessage(E7230, header));
}
}
replaceHeaders(orderedHeaders);
return newColumnIndexes;
}
Aggregations