java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/BarChartFormatter.java
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, version 3 of the License.
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.
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/>.
17 package info.globalcode.sql.dk.formatting;
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;
30 * TODO: min/max values – range for case that no value is 100 %
32 * TODO: multiple barcharts in same table (last column is still default) + multiple resultsets
34 * TODO: negative values - bar starting from the middle, not always from the left
36 * @author Ing. František Kučera (frantovo.cz)
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 {
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;
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
59 protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
60 super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
62 updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
64 BigDecimal maximum = BigDecimal.ZERO;
65 BigDecimal minimum = BigDecimal.ZERO;
66 int lastIndex = currentHeader.getColumnCount() - 1;
68 Object valueObject = null;
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);
79 BigDecimal range = maximum.subtract(minimum);
81 for (Object[] row : currentResultSet) {
82 valueObject = row[lastIndex];
83 if (valueObject == null) {
86 BigDecimal value = new BigDecimal(valueObject.toString());
87 BigDecimal valueFromMinimum = value.subtract(minimum);
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);
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