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