]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
fix: Scales correctly respect the locale setting when generating labels (#8710)
authorEvert Timberg <evert.timberg+github@gmail.com>
Wed, 24 Mar 2021 13:33:47 +0000 (09:33 -0400)
committerGitHub <noreply@github.com>
Wed, 24 Mar 2021 13:33:47 +0000 (09:33 -0400)
src/scales/scale.linearbase.js
src/scales/scale.logarithmic.js
test/specs/scale.linear.tests.js
test/specs/scale.logarithmic.tests.js

index e8a122199af4a187e96f1047cf619931d40a24dc..a215c7571c6dd11cdf60f69a31c180a51e24fce9 100644 (file)
@@ -275,6 +275,6 @@ export default class LinearScaleBase extends Scale {
   }
 
   getLabelForValue(value) {
-    return formatNumber(value, this.options.locale);
+    return formatNumber(value, this.chart.options.locale);
   }
 }
index 66751af7aeea6b3a8de516f94f2f8c248786df23..2223f1cff88892a62b9352284857c5493d3ab749 100644 (file)
@@ -149,7 +149,7 @@ export default class LogarithmicScale extends Scale {
         * @return {string}
         */
   getLabelForValue(value) {
-    return value === undefined ? '0' : formatNumber(value, this.options.locale);
+    return value === undefined ? '0' : formatNumber(value, this.chart.options.locale);
   }
 
   /**
index 9ed4641056da7f75cb33f2d8e0ce6314efb69588..9ff00e7240f91330d143aea4f0432d9f03493ecf 100644 (file)
@@ -303,6 +303,46 @@ describe('Linear Scale', function() {
     expect(chart.scales.y.getLabelForValue(7)).toBe('7');
   });
 
+  it('Should correctly use the locale setting when getting a label', function() {
+    var chart = window.acquireChart({
+      type: 'line',
+      data: {
+        datasets: [{
+          xAxisID: 'x',
+          yAxisID: 'y',
+          data: [{
+            x: 10,
+            y: 100
+          }, {
+            x: -10,
+            y: 0
+          }, {
+            x: 0,
+            y: 0
+          }, {
+            x: 99,
+            y: 7
+          }]
+        }],
+      },
+      options: {
+        locale: 'de-DE',
+        scales: {
+          x: {
+            type: 'linear',
+            position: 'bottom'
+          },
+          y: {
+            type: 'linear'
+          }
+        }
+      }
+    });
+    chart.update();
+
+    expect(chart.scales.y.getLabelForValue(7.07)).toBe('7,07');
+  });
+
   it('Should correctly determine the min and max data values when stacked mode is turned on', function() {
     var chart = window.acquireChart({
       type: 'line',
index 0f0dc3303ebf25c877f6b126ede53076b62ef1a8..221152b66b594fa08ad5a2c6e5455554228e72cd 100644 (file)
@@ -712,6 +712,39 @@ describe('Logarithmic Scale tests', function() {
     expect(chart.scales.y.getLabelForValue(150)).toBe('150');
   });
 
+  it('should correctly use the locale when generating the label', function() {
+    var chart = window.acquireChart({
+      type: 'bar',
+      data: {
+        datasets: [{
+          yAxisID: 'y',
+          data: [10, 5, 5000, 78, 450]
+        }, {
+          yAxisID: 'y1',
+          data: [1, 1000, 10, 100],
+        }, {
+          yAxisID: 'y',
+          data: [150]
+        }],
+        labels: []
+      },
+      options: {
+        locale: 'de-DE',
+        scales: {
+          y: {
+            type: 'logarithmic'
+          },
+          y1: {
+            position: 'right',
+            type: 'logarithmic'
+          }
+        }
+      }
+    });
+
+    expect(chart.scales.y.getLabelForValue(10.25)).toBe('10,25');
+  });
+
   describe('when', function() {
     var data = [
       {