java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/BarChartFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@224
     1
/**
franta-hg@224
     2
 * SQL-DK
franta-hg@224
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
franta-hg@224
     4
 *
franta-hg@224
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@224
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@250
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@224
     8
 *
franta-hg@224
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@224
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@224
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@224
    12
 * GNU General Public License for more details.
franta-hg@224
    13
 *
franta-hg@224
    14
 * You should have received a copy of the GNU General Public License
franta-hg@224
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@224
    16
 */
franta-hg@224
    17
package info.globalcode.sql.dk.formatting;
franta-hg@224
    18
franta-hg@224
    19
import info.globalcode.sql.dk.Functions;
franta-hg@224
    20
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
franta-hg@224
    21
import info.globalcode.sql.dk.logging.LoggerProducer;
franta-hg@224
    22
import java.math.BigDecimal;
franta-hg@224
    23
import java.math.MathContext;
franta-hg@224
    24
import java.math.RoundingMode;
franta-hg@224
    25
import java.util.List;
franta-hg@224
    26
import java.util.logging.Level;
franta-hg@224
    27
import java.util.logging.Logger;
franta-hg@224
    28
franta-hg@224
    29
/**
franta-hg@228
    30
 * TODO: min/max values – range for case that no value is 100 %
franta-hg@228
    31
 *
franta-hg@228
    32
 * TODO: multiple barcharts in same table (last column is still default) + multiple resultsets
franta-hg@228
    33
 *
franta-hg@228
    34
 * TODO: negative values - bar starting from the middle, not always from the left
franta-hg@224
    35
 *
franta-hg@224
    36
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@224
    37
 */
franta-hg@224
    38
@PropertyDeclaration(name = BarChartFormatter.PROPERTY_PRECISION, type = Integer.class, defaultValue = BarChartFormatter.PROPERTY_PRECISION_DEFAULT, description = "number of characters representing 100 % in the bar chart")
franta-hg@224
    39
public class BarChartFormatter extends TabularPrefetchingFormatter {
franta-hg@224
    40
franta-hg@224
    41
	public static final String NAME = "barchart"; // bash-completion:formatter
franta-hg@224
    42
	public static final String PROPERTY_PRECISION = "precision";
franta-hg@224
    43
	protected static final String PROPERTY_PRECISION_DEFAULT = "100";
franta-hg@224
    44
	private static final MathContext mathContext = MathContext.DECIMAL128;
franta-hg@224
    45
	public static final Logger log = LoggerProducer.getLogger();
franta-hg@224
    46
	private final BigDecimal chartPrecision;
franta-hg@224
    47
	private final char chartFull;
franta-hg@224
    48
	private final char chartEmpty;
franta-hg@224
    49
franta-hg@224
    50
	public BarChartFormatter(FormatterContext formatterContext) {
franta-hg@224
    51
		super(formatterContext);
franta-hg@224
    52
		chartPrecision = BigDecimal.valueOf(formatterContext.getProperties().getInteger(PROPERTY_PRECISION, Integer.parseInt(PROPERTY_PRECISION_DEFAULT)));
franta-hg@224
    53
		chartFull = isAsciiNostalgia() ? '#' : '█';
franta-hg@224
    54
		chartEmpty = isAsciiNostalgia() ? '~' : '░';
franta-hg@224
    55
		// TODO: consider using partial blocks for more precision: https://en.wikipedia.org/wiki/Block_Elements
franta-hg@224
    56
	}
franta-hg@224
    57
franta-hg@224
    58
	@Override
franta-hg@224
    59
	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
franta-hg@224
    60
		super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
franta-hg@224
    61
franta-hg@224
    62
		updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
franta-hg@224
    63
franta-hg@224
    64
		BigDecimal maximum = BigDecimal.ZERO;
franta-hg@224
    65
		BigDecimal minimum = BigDecimal.ZERO;
franta-hg@224
    66
		int lastIndex = currentHeader.getColumnCount() - 1;
franta-hg@224
    67
franta-hg@226
    68
		Object valueObject = null;
franta-hg@224
    69
		try {
franta-hg@224
    70
			for (Object[] row : currentResultSet) {
franta-hg@226
    71
				valueObject = row[lastIndex];
franta-hg@224
    72
				if (valueObject != null) {
franta-hg@224
    73
					BigDecimal value = new BigDecimal(valueObject.toString());
franta-hg@224
    74
					maximum = maximum.max(value);
franta-hg@224
    75
					minimum = minimum.min(value);
franta-hg@224
    76
				}
franta-hg@224
    77
			}
franta-hg@224
    78
franta-hg@224
    79
			BigDecimal range = maximum.subtract(minimum);
franta-hg@224
    80
franta-hg@224
    81
			for (Object[] row : currentResultSet) {
franta-hg@226
    82
				valueObject = row[lastIndex];
franta-hg@226
    83
				if (valueObject == null) {
franta-hg@226
    84
					row[lastIndex] = "";
franta-hg@226
    85
				} else {
franta-hg@224
    86
					BigDecimal value = new BigDecimal(valueObject.toString());
franta-hg@224
    87
					BigDecimal valueFromMinimum = value.subtract(minimum);
franta-hg@224
    88
franta-hg@224
    89
					BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
franta-hg@224
    90
					int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
franta-hg@224
    91
					row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
franta-hg@224
    92
				}
franta-hg@224
    93
			}
franta-hg@224
    94
franta-hg@224
    95
		} catch (NumberFormatException e) {
franta-hg@224
    96
			// https://en.wiktionary.org/wiki/parsable
franta-hg@226
    97
			log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number. But was „{0}“", valueObject);
franta-hg@224
    98
			// FIXME: throw FormatterException
franta-hg@224
    99
			throw e;
franta-hg@224
   100
		}
franta-hg@224
   101
	}
franta-hg@224
   102
franta-hg@224
   103
}