From 7154cc51de02b99425cac216e35696f045ee3222 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 19 Aug 2021 12:18:13 +0200 Subject: [PATCH] [jOOQ/jOOQ#12332] Result.formatChart() produces bad default formatting for Display.HUNDRED_PERCENT_STACKED --- jOOQ/src/main/java/org/jooq/ChartFormat.java | 72 +++++++++++++++---- .../java/org/jooq/impl/AbstractResult.java | 12 ++-- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/ChartFormat.java b/jOOQ/src/main/java/org/jooq/ChartFormat.java index 9e7404b906..5ec4d09d78 100644 --- a/jOOQ/src/main/java/org/jooq/ChartFormat.java +++ b/jOOQ/src/main/java/org/jooq/ChartFormat.java @@ -66,6 +66,7 @@ public final class ChartFormat { final boolean showVerticalLegend; final String newline; final DecimalFormat numericFormat; + final DecimalFormat percentFormat; public ChartFormat() { this( @@ -81,7 +82,8 @@ public final class ChartFormat { true, true, "\n", - new DecimalFormat("###,###.00") + new DecimalFormat("###,###.00"), + new DecimalFormat("##0.00'%'") ); } @@ -98,7 +100,8 @@ public final class ChartFormat { boolean showHorizontalLegend, boolean showVerticalLegend, String newline, - DecimalFormat numericFormat + DecimalFormat numericFormat, + DecimalFormat percentFormat ) { this.output = output; this.type = type; @@ -113,6 +116,7 @@ public final class ChartFormat { this.showVerticalLegend = showVerticalLegend; this.newline = newline; this.numericFormat = numericFormat; + this.percentFormat = percentFormat; } /** @@ -133,7 +137,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -163,7 +168,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -190,7 +196,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -220,7 +227,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -272,7 +280,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -301,7 +310,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -330,7 +340,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -360,7 +371,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -390,7 +402,8 @@ public final class ChartFormat { newShowHorizontalLegend, newShowVerticalLegend, newline, - numericFormat + numericFormat, + percentFormat ); } @@ -442,7 +455,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newNewline, - numericFormat + numericFormat, + percentFormat ); } @@ -472,7 +486,8 @@ public final class ChartFormat { showHorizontalLegend, showVerticalLegend, newline, - newNumericFormat + newNumericFormat, + percentFormat ); } @@ -484,6 +499,37 @@ public final class ChartFormat { return numericFormat; } + /** + * The new numeric format for percentages, defaulting to ###.00'%'. + */ + @NotNull + public ChartFormat percentFormat(DecimalFormat newPercentFormat) { + return new ChartFormat( + output, + type, + display, + width, + height, + category, + categoryAsText, + values, + shades, + showHorizontalLegend, + showVerticalLegend, + newline, + numericFormat, + newPercentFormat + ); + } + + /** + * The numeric format for percentages. + */ + @NotNull + public DecimalFormat percentFormat() { + return percentFormat; + } + /** * The chart output format. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java index 4cf79d398b..e4c975a388 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java @@ -917,10 +917,12 @@ abstract class AbstractResult extends AbstractFormattable impl } int verticalLegendWidth = format.showVerticalLegend() - ? Math.max( - format.numericFormat().format(axisMin).length(), - format.numericFormat().format(axisMax).length() - ) + ? (format.display() == Display.HUNDRED_PERCENT_STACKED) + ? fp.width(format.percentFormat().format(100.0)) + : Math.max( + format.numericFormat().format(axisMin).length(), + format.numericFormat().format(axisMax).length() + ) : 0; int horizontalLegendHeight = format.showHorizontalLegend() ? 1 : 0; @@ -940,7 +942,7 @@ abstract class AbstractResult extends AbstractFormattable impl if (format.showVerticalLegend()) { String axisLegendString = (format.display() == Display.HUNDRED_PERCENT_STACKED) - ? format.numericFormat().format(axisLegendPercent * 100.0) + "%" + ? format.percentFormat().format(axisLegendPercent * 100.0) : format.numericFormat().format(axisLegend); for (int x = fp.width(axisLegendString); x < verticalLegendWidth; x++)