java/sql-dk/src/info/globalcode/sql/dk/formatting/BarChartFormatter.java
branchv_0
changeset 224 36db9fd27436
child 226 b40153eb7716
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/BarChartFormatter.java	Sun Aug 30 02:28:15 2015 +0200
     1.3 @@ -0,0 +1,96 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2015 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.formatting;
    1.22 +
    1.23 +import info.globalcode.sql.dk.Functions;
    1.24 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
    1.25 +import info.globalcode.sql.dk.logging.LoggerProducer;
    1.26 +import java.math.BigDecimal;
    1.27 +import java.math.MathContext;
    1.28 +import java.math.RoundingMode;
    1.29 +import java.util.List;
    1.30 +import java.util.logging.Level;
    1.31 +import java.util.logging.Logger;
    1.32 +
    1.33 +/**
    1.34 + *
    1.35 + * @author Ing. František Kučera (frantovo.cz)
    1.36 + */
    1.37 +@PropertyDeclaration(name = BarChartFormatter.PROPERTY_PRECISION, type = Integer.class, defaultValue = BarChartFormatter.PROPERTY_PRECISION_DEFAULT, description = "number of characters representing 100 % in the bar chart")
    1.38 +public class BarChartFormatter extends TabularPrefetchingFormatter {
    1.39 +
    1.40 +	public static final String NAME = "barchart"; // bash-completion:formatter
    1.41 +	public static final String PROPERTY_PRECISION = "precision";
    1.42 +	protected static final String PROPERTY_PRECISION_DEFAULT = "100";
    1.43 +	private static final MathContext mathContext = MathContext.DECIMAL128;
    1.44 +	public static final Logger log = LoggerProducer.getLogger();
    1.45 +	private final BigDecimal chartPrecision;
    1.46 +	private final char chartFull;
    1.47 +	private final char chartEmpty;
    1.48 +
    1.49 +	public BarChartFormatter(FormatterContext formatterContext) {
    1.50 +		super(formatterContext);
    1.51 +		chartPrecision = BigDecimal.valueOf(formatterContext.getProperties().getInteger(PROPERTY_PRECISION, Integer.parseInt(PROPERTY_PRECISION_DEFAULT)));
    1.52 +		chartFull = isAsciiNostalgia() ? '#' : '█';
    1.53 +		chartEmpty = isAsciiNostalgia() ? '~' : '░';
    1.54 +		// TODO: consider using partial blocks for more precision: https://en.wikipedia.org/wiki/Block_Elements
    1.55 +	}
    1.56 +
    1.57 +	@Override
    1.58 +	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
    1.59 +		super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
    1.60 +
    1.61 +		updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
    1.62 +
    1.63 +		BigDecimal maximum = BigDecimal.ZERO;
    1.64 +		BigDecimal minimum = BigDecimal.ZERO;
    1.65 +		int lastIndex = currentHeader.getColumnCount() - 1;
    1.66 +
    1.67 +		try {
    1.68 +			for (Object[] row : currentResultSet) {
    1.69 +				Object valueObject = row[lastIndex];
    1.70 +				if (valueObject != null) {
    1.71 +					BigDecimal value = new BigDecimal(valueObject.toString());
    1.72 +					maximum = maximum.max(value);
    1.73 +					minimum = minimum.min(value);
    1.74 +				}
    1.75 +			}
    1.76 +
    1.77 +			BigDecimal range = maximum.subtract(minimum);
    1.78 +
    1.79 +			for (Object[] row : currentResultSet) {
    1.80 +				Object valueObject = row[lastIndex];
    1.81 +				if (valueObject != null) {
    1.82 +					BigDecimal value = new BigDecimal(valueObject.toString());
    1.83 +					BigDecimal valueFromMinimum = value.subtract(minimum);
    1.84 +
    1.85 +					BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
    1.86 +					int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
    1.87 +					row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
    1.88 +				}
    1.89 +			}
    1.90 +
    1.91 +		} catch (NumberFormatException e) {
    1.92 +			// https://en.wiktionary.org/wiki/parsable
    1.93 +			log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number.");
    1.94 +			// FIXME: throw FormatterException
    1.95 +			throw e;
    1.96 +		}
    1.97 +	}
    1.98 +
    1.99 +}