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