Search in sources :

Example 46 with GridHeader

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();
    }
}
Also used : CsvWriter(com.csvreader.CsvWriter) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) GridHeader(org.hisp.dhis.common.GridHeader)

Example 47 with GridHeader

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;
}
Also used : SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) ArrayList(java.util.ArrayList) GridHeader(org.hisp.dhis.common.GridHeader)

Example 48 with GridHeader

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;
}
Also used : ArrayList(java.util.ArrayList) GridHeader(org.hisp.dhis.common.GridHeader)

Example 49 with GridHeader

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();
}
Also used : ArrayList(java.util.ArrayList) GridHeader(org.hisp.dhis.common.GridHeader)

Example 50 with GridHeader

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) GridHeader(org.hisp.dhis.common.GridHeader)

Aggregations

GridHeader (org.hisp.dhis.common.GridHeader)60 Grid (org.hisp.dhis.common.Grid)39 ListGrid (org.hisp.dhis.system.grid.ListGrid)29 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)15 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)15 DimensionalObject (org.hisp.dhis.common.DimensionalObject)13 HashMap (java.util.HashMap)9 List (java.util.List)8 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)7 Map (java.util.Map)6 QueryItem (org.hisp.dhis.common.QueryItem)6 LinkedHashSet (java.util.LinkedHashSet)5 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)5 DataElement (org.hisp.dhis.dataelement.DataElement)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)3 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)3