From 02c91f2dee93413c119d5fcb5e7bb4c7b20f8423 Mon Sep 17 00:00:00 2001 From: Tom Robinson <tlrobinson@gmail.com> Date: Tue, 11 Sep 2018 12:36:36 -0700 Subject: [PATCH] Fix scalar format options --- frontend/src/metabase/lib/formatting.js | 2 ++ .../visualizations/visualizations/Scalar.jsx | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/frontend/src/metabase/lib/formatting.js b/frontend/src/metabase/lib/formatting.js index 8a4da5d4e8a..53b30d6a7cb 100644 --- a/frontend/src/metabase/lib/formatting.js +++ b/frontend/src/metabase/lib/formatting.js @@ -52,6 +52,7 @@ export type FormattingOptions = { const DEFAULT_NUMBER_OPTIONS: FormattingOptions = { compact: false, + maximumFractionDigits: 2, useGrouping: true, }; @@ -96,6 +97,7 @@ export function formatNumber(number: number, options: FormattingOptions = {}) { try { return number.toLocaleString(options.locale, options); } catch (e) { + console.warn("Error calling toLocaleString", e); return String(number); } } diff --git a/frontend/src/metabase/visualizations/visualizations/Scalar.jsx b/frontend/src/metabase/visualizations/visualizations/Scalar.jsx index 2895bbffe54..fe6184e589f 100644 --- a/frontend/src/metabase/visualizations/visualizations/Scalar.jsx +++ b/frontend/src/metabase/visualizations/visualizations/Scalar.jsx @@ -11,22 +11,22 @@ import { formatValue } from "metabase/lib/formatting"; import { TYPE } from "metabase/lib/types"; import cx from "classnames"; +import _ from "underscore"; import type { VisualizationProps } from "metabase/meta/types/Visualization"; function scalarSettingsToFormatOptions(settings) { - const decimals = parseFloat(settings["scalar.decimals"]); - const scale = parseFloat(settings["scalar.scale"]); - return { - column: column, + const formatOptions = { suffix: settings["scalar.suffix"], prefix: settings["scalar.prefix"], - scale: isNaN(scale) ? null : scale, + scale: parseFloat(settings["scalar.scale"]), locale: settings["scalar.locale"], useGrouping: !!settings["scalar.locale"], - minimumFractionDigits: isNaN(decimals) ? null : decimals, - maximumFractionDigits: isNaN(decimals) ? null : decimals, + minimumFractionDigits: parseFloat(settings["scalar.decimals"]), + maximumFractionDigits: parseFloat(settings["scalar.decimals"]), }; + // remove null options to allow for defaults + return _.pick(formatOptions, v => v != null && v !== "" && !isNaN(v)); } export default class Scalar extends Component { @@ -133,7 +133,10 @@ export default class Scalar extends Component { const value = rows[0] && rows[0][0]; const column = cols[0]; - const formatOptions = scalarSettingsToFormatOptions(settings); + const formatOptions = { + column, + ...scalarSettingsToFormatOptions(settings), + }; const fullScalarValue = formatValue(value, formatOptions); const compactScalarValue = isSmall -- GitLab