]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Use cached Intl.NumberFormat everywhere (#8244)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 28 Dec 2020 17:31:19 +0000 (19:31 +0200)
committerGitHub <noreply@github.com>
Mon, 28 Dec 2020 17:31:19 +0000 (12:31 -0500)
src/controllers/controller.doughnut.js
src/core/core.intl.js [new file with mode: 0644]
src/core/core.ticks.js
src/scales/scale.linearbase.js
src/scales/scale.logarithmic.js

index b71bc4c6c2368feef9746c42a313e85a6db1a915..2374fcd0e455015c650b0a4a5528a95a55c0d57f 100644 (file)
@@ -1,4 +1,5 @@
 import DatasetController from '../core/core.datasetController';
+import {formatNumber} from '../core/core.intl';
 import {isArray, valueOrDefault} from '../helpers/helpers.core';
 import {toRadians, PI, TAU, HALF_PI} from '../helpers/helpers.math';
 
@@ -235,7 +236,7 @@ export default class DoughnutController extends DatasetController {
                const meta = me._cachedMeta;
                const chart = me.chart;
                const labels = chart.data.labels || [];
-               const value = new Intl.NumberFormat(chart.options.locale).format(meta._parsed[index]);
+               const value = formatNumber(meta._parsed[index], chart.options.locale);
 
                return {
                        label: labels[index] || '',
diff --git a/src/core/core.intl.js b/src/core/core.intl.js
new file mode 100644 (file)
index 0000000..88803cd
--- /dev/null
@@ -0,0 +1,17 @@
+
+const intlCache = new Map();
+
+export function getNumberFormat(locale, options) {
+       options = options || {};
+       const cacheKey = locale + JSON.stringify(options);
+       let formatter = intlCache.get(cacheKey);
+       if (!formatter) {
+               formatter = new Intl.NumberFormat(locale, options);
+               intlCache.set(cacheKey, formatter);
+       }
+       return formatter;
+}
+
+export function formatNumber(num, locale, options) {
+       return getNumberFormat(locale, options).format(num);
+}
index 1b75c13b069394d495c07bc624575316e3c095cd..1e6770d643dc4aa06022945fe46998abd2de8c50 100644 (file)
@@ -1,7 +1,7 @@
 import {isArray} from '../helpers/helpers.core';
 import {log10} from '../helpers/helpers.math';
+import {formatNumber} from './core.intl';
 
-const intlCache = new Map();
 /**
  * Namespace to hold formatters for different types of ticks
  * @namespace Chart.Ticks.formatters
@@ -55,14 +55,7 @@ const formatters = {
                const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal};
                Object.assign(options, this.options.ticks.format);
 
-               const cacheKey = locale + JSON.stringify(options);
-               let formatter = intlCache.get(cacheKey);
-               if (!formatter) {
-                       formatter = new Intl.NumberFormat(locale, options);
-                       intlCache.set(cacheKey, formatter);
-               }
-
-               return formatter.format(tickValue);
+               return formatNumber(tickValue, locale, options);
        }
 };
 
index 1f26e00934e2146c2f90635942d9f95a6f634eaa..0ea2b9d1cef203c52aab24f0c3c26a34416ac77c 100644 (file)
@@ -1,6 +1,7 @@
 import {isNullOrUndef, valueOrDefault} from '../helpers/helpers.core';
 import {almostEquals, almostWhole, log10, _decimalPlaces, _setMinAndMaxByKey, sign} from '../helpers/helpers.math';
 import Scale from '../core/core.scale';
+import {formatNumber} from '../core/core.intl';
 
 /**
  * Implementation of the nice number algorithm used in determining where axis labels will go
@@ -244,6 +245,6 @@ export default class LinearScaleBase extends Scale {
        }
 
        getLabelForValue(value) {
-               return new Intl.NumberFormat(this.options.locale).format(value);
+               return formatNumber(value, this.options.locale);
        }
 }
index 955496408159600798986912a538f9b79c0b0519..949d67a4ea7aa0fe408592554465eb8f47a0a9b8 100644 (file)
@@ -3,6 +3,7 @@ import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math';
 import Scale from '../core/core.scale';
 import LinearScaleBase from './scale.linearbase';
 import Ticks from '../core/core.ticks';
+import {formatNumber} from '../core/core.intl';
 
 function isMajor(tickVal) {
        const remain = tickVal / (Math.pow(10, Math.floor(log10(tickVal))));
@@ -148,7 +149,7 @@ export default class LogarithmicScale extends Scale {
         * @return {string}
         */
        getLabelForValue(value) {
-               return value === undefined ? '0' : new Intl.NumberFormat(this.options.locale).format(value);
+               return value === undefined ? '0' : formatNumber(value, this.options.locale);
        }
 
        /**