]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Linear: determine grace amount from range (#9719)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 4 Oct 2021 21:43:04 +0000 (00:43 +0300)
committerGitHub <noreply@github.com>
Mon, 4 Oct 2021 21:43:04 +0000 (17:43 -0400)
src/core/core.scale.js
src/helpers/helpers.options.js
test/fixtures/scale.linear/grace/grace-10%.js [new file with mode: 0644]
test/fixtures/scale.linear/grace/grace-10%.png [new file with mode: 0644]
test/fixtures/scale.linear/grace/grace-beginAtZero.js [new file with mode: 0644]
test/fixtures/scale.linear/grace/grace-beginAtZero.png [new file with mode: 0644]
test/fixtures/scale.linear/grace/grace-neg.js
test/fixtures/scale.linear/grace/grace-pos.js
test/fixtures/scale.linear/grace/grace.js

index 68b3c1b1c9c348d16f5245d138188bc86e2b32ad..4549cadb9d93adbfcf38e488bac863152aa6c5a0 100644 (file)
@@ -387,7 +387,7 @@ export default class Scale extends Element {
         *     - thickness of scales or legends in another orientation
         */
   update(maxWidth, maxHeight, margins) {
-    const tickOpts = this.options.ticks;
+    const {beginAtZero, grace, ticks: tickOpts} = this.options;
     const sampleSize = tickOpts.sampleSize;
 
     // Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
@@ -422,7 +422,7 @@ export default class Scale extends Element {
       this.beforeDataLimits();
       this.determineDataLimits();
       this.afterDataLimits();
-      this._range = _addGrace(this, this.options.grace);
+      this._range = _addGrace(this, grace, beginAtZero);
       this._dataLimitsCached = true;
     }
 
index 7453a54bc548433696f11432bbc75b3098a08200..d42b3503b8e0ebcbc8dc13f1453d30b822d3a547 100644 (file)
@@ -172,12 +172,15 @@ export function resolve(inputs, context, index, info) {
 /**
  * @param {{min: number, max: number}} minmax
  * @param {number|string} grace
+ * @param {boolean} beginAtZero
  * @private
  */
-export function _addGrace(minmax, grace) {
+export function _addGrace(minmax, grace, beginAtZero) {
   const {min, max} = minmax;
+  const change = toDimension(grace, (max - min) / 2);
+  const keepZero = (value, add) => beginAtZero && value === 0 ? 0 : value + add;
   return {
-    min: min - Math.abs(toDimension(grace, min)),
-    max: max + toDimension(grace, max)
+    min: keepZero(min, -Math.abs(change)),
+    max: keepZero(max, change)
   };
 }
diff --git a/test/fixtures/scale.linear/grace/grace-10%.js b/test/fixtures/scale.linear/grace/grace-10%.js
new file mode 100644 (file)
index 0000000..3001fec
--- /dev/null
@@ -0,0 +1,29 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['a', 'b'],
+      datasets: [{
+        data: [90, -10],
+      }],
+    },
+    options: {
+      indexAxis: 'y',
+      scales: {
+        y: {
+          display: false
+        },
+        x: {
+          grace: '10%'
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 512,
+      height: 128
+    }
+  }
+};
diff --git a/test/fixtures/scale.linear/grace/grace-10%.png b/test/fixtures/scale.linear/grace/grace-10%.png
new file mode 100644 (file)
index 0000000..ae3db68
Binary files /dev/null and b/test/fixtures/scale.linear/grace/grace-10%.png differ
diff --git a/test/fixtures/scale.linear/grace/grace-beginAtZero.js b/test/fixtures/scale.linear/grace/grace-beginAtZero.js
new file mode 100644 (file)
index 0000000..77965d6
--- /dev/null
@@ -0,0 +1,42 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['a', 'b'],
+      datasets: [{
+        data: [100, 0],
+        backgroundColor: 'blue'
+      }, {
+        xAxisID: 'x2',
+        data: [0, 100],
+        backgroundColor: 'red'
+      }],
+    },
+    options: {
+      indexAxis: 'y',
+      scales: {
+        y: {
+          display: false
+        },
+        x: {
+          position: 'top',
+          beginAtZero: true,
+          grace: '10%',
+        },
+        x2: {
+          position: 'bottom',
+          type: 'linear',
+          beginAtZero: false,
+          grace: '10%',
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 512,
+      height: 128
+    }
+  }
+};
diff --git a/test/fixtures/scale.linear/grace/grace-beginAtZero.png b/test/fixtures/scale.linear/grace/grace-beginAtZero.png
new file mode 100644 (file)
index 0000000..4f1883f
Binary files /dev/null and b/test/fixtures/scale.linear/grace/grace-beginAtZero.png differ
index f606202ae521cbf2a988b1da3dcb80f43e307f2a..90bdf0eb5233f533bb94c9e059cbd2a0ff1defa2 100644 (file)
@@ -1,5 +1,4 @@
 module.exports = {
-  description: 'https://github.com/chartjs/Chart.js/issues/7734',
   config: {
     type: 'bar',
     data: {
index 76d6c693d6817d2f029db751030f82b74bcfc93e..72df79a86defd238d42622da6aba326b0aa8e736 100644 (file)
@@ -1,5 +1,4 @@
 module.exports = {
-  description: 'https://github.com/chartjs/Chart.js/issues/7734',
   config: {
     type: 'bar',
     data: {
index 8f3f09f9643ffa27b17f00863d4911bc89ed4810..a2950e5b1974042ec9dda40ffda5820e004c8522 100644 (file)
@@ -1,5 +1,4 @@
 module.exports = {
-  description: 'https://github.com/chartjs/Chart.js/issues/7734',
   config: {
     type: 'bar',
     data: {