3 * Copyright © 2015 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk.formatting;
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;
32 * @author Ing. František Kučera (frantovo.cz)
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 {
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;
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
55 protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
56 super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
58 updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
60 BigDecimal maximum = BigDecimal.ZERO;
61 BigDecimal minimum = BigDecimal.ZERO;
62 int lastIndex = currentHeader.getColumnCount() - 1;
64 Object valueObject = null;
66 for (Object[] row : currentResultSet) {
67 valueObject = row[lastIndex];
68 if (valueObject != null) {
69 BigDecimal value = new BigDecimal(valueObject.toString());
70 maximum = maximum.max(value);
71 minimum = minimum.min(value);
75 BigDecimal range = maximum.subtract(minimum);
77 for (Object[] row : currentResultSet) {
78 valueObject = row[lastIndex];
79 if (valueObject == null) {
82 BigDecimal value = new BigDecimal(valueObject.toString());
83 BigDecimal valueFromMinimum = value.subtract(minimum);
85 BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
86 int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
87 row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
91 } catch (NumberFormatException e) {
92 // https://en.wiktionary.org/wiki/parsable
93 log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number. But was „{0}“", valueObject);
94 // FIXME: throw FormatterException