1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/BarChartFormatter.java Mon Mar 04 17:06:42 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,104 +0,0 @@
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 - * TODO: min/max values – range for case that no value is 100 %
1.35 - *
1.36 - * TODO: multiple barcharts in same table (last column is still default) + multiple resultsets
1.37 - *
1.38 - * TODO: negative values - bar starting from the middle, not always from the left
1.39 - *
1.40 - * @author Ing. František Kučera (frantovo.cz)
1.41 - */
1.42 -@PropertyDeclaration(name = BarChartFormatter.PROPERTY_PRECISION, type = Integer.class, defaultValue = BarChartFormatter.PROPERTY_PRECISION_DEFAULT, description = "number of characters representing 100 % in the bar chart")
1.43 -public class BarChartFormatter extends TabularPrefetchingFormatter {
1.44 -
1.45 - public static final String NAME = "barchart"; // bash-completion:formatter
1.46 - public static final String PROPERTY_PRECISION = "precision";
1.47 - protected static final String PROPERTY_PRECISION_DEFAULT = "100";
1.48 - private static final MathContext mathContext = MathContext.DECIMAL128;
1.49 - public static final Logger log = LoggerProducer.getLogger();
1.50 - private final BigDecimal chartPrecision;
1.51 - private final char chartFull;
1.52 - private final char chartEmpty;
1.53 -
1.54 - public BarChartFormatter(FormatterContext formatterContext) {
1.55 - super(formatterContext);
1.56 - chartPrecision = BigDecimal.valueOf(formatterContext.getProperties().getInteger(PROPERTY_PRECISION, Integer.parseInt(PROPERTY_PRECISION_DEFAULT)));
1.57 - chartFull = isAsciiNostalgia() ? '#' : '█';
1.58 - chartEmpty = isAsciiNostalgia() ? '~' : '░';
1.59 - // TODO: consider using partial blocks for more precision: https://en.wikipedia.org/wiki/Block_Elements
1.60 - }
1.61 -
1.62 - @Override
1.63 - protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
1.64 - super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
1.65 -
1.66 - updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
1.67 -
1.68 - BigDecimal maximum = BigDecimal.ZERO;
1.69 - BigDecimal minimum = BigDecimal.ZERO;
1.70 - int lastIndex = currentHeader.getColumnCount() - 1;
1.71 -
1.72 - Object valueObject = null;
1.73 - try {
1.74 - for (Object[] row : currentResultSet) {
1.75 - valueObject = row[lastIndex];
1.76 - if (valueObject != null) {
1.77 - BigDecimal value = new BigDecimal(valueObject.toString());
1.78 - maximum = maximum.max(value);
1.79 - minimum = minimum.min(value);
1.80 - }
1.81 - }
1.82 -
1.83 - BigDecimal range = maximum.subtract(minimum);
1.84 -
1.85 - for (Object[] row : currentResultSet) {
1.86 - valueObject = row[lastIndex];
1.87 - if (valueObject == null) {
1.88 - row[lastIndex] = "";
1.89 - } else {
1.90 - BigDecimal value = new BigDecimal(valueObject.toString());
1.91 - BigDecimal valueFromMinimum = value.subtract(minimum);
1.92 -
1.93 - BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
1.94 - int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
1.95 - row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
1.96 - }
1.97 - }
1.98 -
1.99 - } catch (NumberFormatException e) {
1.100 - // https://en.wiktionary.org/wiki/parsable
1.101 - log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number. But was „{0}“", valueObject);
1.102 - // FIXME: throw FormatterException
1.103 - throw e;
1.104 - }
1.105 - }
1.106 -
1.107 -}