From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 27 Jan 2020 22:58:58 +0000 (-0800) Subject: Format numbers in tooltip (#7004) X-Git-Tag: v3.0.0-alpha~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ae11c4d402a4d10ae255b0ad176f62c3a9907e3;p=thirdparty%2FChart.js.git Format numbers in tooltip (#7004) * Format numbers in tooltip * Add check for typeof number * Implement only for linear and log scales --- diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index 0c5bc1eed..f5c136e9b 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -77,13 +77,14 @@ defaults._set('tooltips', { // Args are: (tooltipItem, data) beforeLabel: helpers.noop, label: function(tooltipItem, data) { - var label = data.datasets[tooltipItem.datasetIndex].label || ''; + let label = data.datasets[tooltipItem.datasetIndex].label || ''; if (label) { label += ': '; } - if (!helpers.isNullOrUndef(tooltipItem.value)) { - label += tooltipItem.value; + const value = tooltipItem.value; + if (!helpers.isNullOrUndef(value)) { + label += value; } return label; }, diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 4c5d73b54..3d894ad76 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -242,6 +242,10 @@ class LinearScaleBase extends Scale { me._endValue = end; me._valueRange = end - start; } + + getLabelForValue(value) { + return new Intl.NumberFormat().format(value); + } } export default LinearScaleBase; diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index d44963a41..3df308276 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -136,7 +136,7 @@ class LogarithmicScale extends Scale { } getLabelForValue(value) { - return value === undefined ? 0 : value; + return value === undefined ? 0 : new Intl.NumberFormat().format(value); } getPixelForTick(index) { diff --git a/test/specs/scale.linear.tests.js b/test/specs/scale.linear.tests.js index ef3f7e8d8..073aa31dc 100644 --- a/test/specs/scale.linear.tests.js +++ b/test/specs/scale.linear.tests.js @@ -332,7 +332,7 @@ describe('Linear Scale', function() { }); chart.update(); - expect(chart.scales.y.getLabelForValue(7)).toBe(7); + expect(chart.scales.y.getLabelForValue(7)).toBe('7'); }); it('Should correctly determine the min and max data values when stacked mode is turned on', function() { diff --git a/test/specs/scale.logarithmic.tests.js b/test/specs/scale.logarithmic.tests.js index 567bdcee4..0cab2eef7 100644 --- a/test/specs/scale.logarithmic.tests.js +++ b/test/specs/scale.logarithmic.tests.js @@ -738,7 +738,7 @@ describe('Logarithmic Scale tests', function() { } }); - expect(chart.scales.y.getLabelForValue(150)).toBe(150); + expect(chart.scales.y.getLabelForValue(150)).toBe('150'); }); describe('when', function() { diff --git a/test/specs/scale.radialLinear.tests.js b/test/specs/scale.radialLinear.tests.js index a25806615..576d0b4dc 100644 --- a/test/specs/scale.radialLinear.tests.js +++ b/test/specs/scale.radialLinear.tests.js @@ -414,7 +414,7 @@ describe('Test the radial linear scale', function() { } } }); - expect(chart.scales.r.getLabelForValue(5)).toBe(5); + expect(chart.scales.r.getLabelForValue(5)).toBe('5'); }); it('should get the correct distance from the center point', function() {