Skip to content

Commit 83c3455

Browse files
committed
[FIX] charts: correctly humanize decimal numbers
We have an `humanizeNumbers` option for the chart to make large numbers more readable. The option is enabled by default on every chart, but it was not correctly handling decimal numbers. The function was always rounding them to the nearest integer, which does not make sense for small numbers (eg. 0.15 should not be displayed as 0). Task: 5155591 Part-of: #7368 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com> Signed-off-by: Adrien Minne (adrm) <adrm@odoo.com>
1 parent a370995 commit 83c3455

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

packages/o-spreadsheet-engine/src/helpers/format/format.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toNumber } from "../../functions/helpers";
1+
import { toNumber, tryToNumber } from "../../functions/helpers";
22
import { _t } from "../../translation";
33
import { CellValue } from "../../types/cells";
44
import { Currency } from "../../types/currency";
@@ -708,14 +708,18 @@ function _roundFormat<T extends InternalFormat>(internalFormat: T): T {
708708
}
709709

710710
export function humanizeNumber({ value, format }: FunctionResultObject, locale: Locale): string {
711-
const numberFormat = formatLargeNumber(
712-
{
713-
value,
714-
format,
715-
},
716-
undefined,
717-
locale
718-
);
711+
const numberValue = tryToNumber(value, locale);
712+
if (numberValue === undefined) {
713+
return "";
714+
}
715+
let numberFormat: Format | undefined = format;
716+
if (Math.abs(numberValue) < 1000) {
717+
const hasDecimal = numberValue % 1 !== 0;
718+
numberFormat = !format && hasDecimal ? "0.####" : format;
719+
} else {
720+
numberFormat = formatLargeNumber({ value, format }, undefined, locale);
721+
}
722+
719723
return formatValue(value, { format: numberFormat, locale });
720724
}
721725

tests/figures/chart/chart_plugin.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,12 +3998,12 @@ describe("Can make numbers human-readable", () => {
39983998
"1"
39993999
);
40004000
let axis = getChartConfiguration(model, "1").options.scales.y;
4001-
const valuesBefore = [1e3, 1e6].map(axis.ticks.callback);
4002-
expect(valuesBefore).toEqual(["1,000", "1,000,000"]);
4001+
const valuesBefore = [1e3, 1e6, 0.1, 0.123456789].map(axis.ticks.callback);
4002+
expect(valuesBefore).toEqual(["1,000", "1,000,000", "0.1", "0.123456789"]);
40034003
updateChart(model, "1", { humanize: true });
40044004
axis = getChartConfiguration(model, "1").options.scales.y;
4005-
const valuesAfter = [1e3, 1e6].map(axis.ticks.callback);
4006-
expect(valuesAfter).toEqual(["1,000", "1,000k"]);
4005+
const valuesAfter = [1e3, 1e6, 0.1, 0.123456789].map(axis.ticks.callback);
4006+
expect(valuesAfter).toEqual(["1,000", "1,000k", "0.1", "0.1235"]);
40074007
}
40084008
);
40094009

0 commit comments

Comments
 (0)