]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Format numbers in tooltip (#7004)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Mon, 27 Jan 2020 22:58:58 +0000 (14:58 -0800)
committerEvert Timberg <evert.timberg+github@gmail.com>
Mon, 27 Jan 2020 22:58:58 +0000 (17:58 -0500)
* Format numbers in tooltip
* Add check for typeof number
* Implement only for linear and log scales

src/plugins/plugin.tooltip.js
src/scales/scale.linearbase.js
src/scales/scale.logarithmic.js
test/specs/scale.linear.tests.js
test/specs/scale.logarithmic.tests.js
test/specs/scale.radialLinear.tests.js

index 0c5bc1eed5e088eedcab017b2a2970d95a930ded..f5c136e9be93e6584fbc4533974ff4c22aa8f5cb 100644 (file)
@@ -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;
                },
index 4c5d73b546560270b1e65bdf3ad8e5dd1578e4de..3d894ad767cf09eb7a1070e57cf507a1f10d9c52 100644 (file)
@@ -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;
index d44963a41a350b7432629861db93288d6c9f0377..3df308276a325f954f1fbf303e30bfa65e1fa39f 100644 (file)
@@ -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) {
index ef3f7e8d87b83b24ef25bbb53a6df3b580ad9ab6..073aa31dc222eea03f4f14b9f7f16766eb267890 100644 (file)
@@ -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() {
index 567bdcee44ed689268327b0479c9fadc55eadf0c..0cab2eef7d6657433b0d6132fff4b9394257cc6f 100644 (file)
@@ -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() {
index a2580661579e5a56282705c444afae518a9c768e..576d0b4dcf51350c69795a7b0c22d58a8b8a68c8 100644 (file)
@@ -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() {