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