]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: Don't use clip/unclipArea when `clip: false` (#9286)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 18 Jun 2021 18:11:33 +0000 (21:11 +0300)
committerGitHub <noreply@github.com>
Fri, 18 Jun 2021 18:11:33 +0000 (14:11 -0400)
src/controllers/controller.bar.js
src/core/core.controller.js
src/core/core.datasetController.js
test/specs/controller.bar.tests.js

index 43c406acaa255c9ae122e0bda9dbbaf871364f4f..cf08851f361259c8cb8910e5f2fb41418a58cc3a 100644 (file)
@@ -1,6 +1,6 @@
 import DatasetController from '../core/core.datasetController';
 import {
-  clipArea, unclipArea, _arrayUnique, isArray, isNullOrUndef,
+  _arrayUnique, isArray, isNullOrUndef,
   valueOrDefault, resolveObjectKey, sign, defined
 } from '../helpers';
 
@@ -224,6 +224,14 @@ export default class BarController extends DatasetController {
     }
   }
 
+  /**
+        * @return {number|boolean}
+        * @protected
+        */
+  getMaxOverflow() {
+    return 0;
+  }
+
   /**
         * @protected
         */
@@ -514,22 +522,17 @@ export default class BarController extends DatasetController {
 
   draw() {
     const me = this;
-    const chart = me.chart;
     const meta = me._cachedMeta;
     const vScale = meta.vScale;
     const rects = meta.data;
     const ilen = rects.length;
     let i = 0;
 
-    clipArea(chart.ctx, chart.chartArea);
-
     for (; i < ilen; ++i) {
       if (me.getParsed(i)[vScale.axis] !== null) {
         rects[i].draw(me._ctx);
       }
     }
-
-    unclipArea(chart.ctx);
   }
 
 }
index 9618bb3dc978a405ab5a054414d139ea19756a4f..3d1f6d4459816d64837d78d7c7327d985642b0aa 100644 (file)
@@ -706,6 +706,7 @@ class Chart {
     const me = this;
     const ctx = me.ctx;
     const clip = meta._clip;
+    const useClip = !clip.disabled;
     const area = me.chartArea;
     const args = {
       meta,
@@ -717,16 +718,20 @@ class Chart {
       return;
     }
 
-    clipArea(ctx, {
-      left: clip.left === false ? 0 : area.left - clip.left,
-      right: clip.right === false ? me.width : area.right + clip.right,
-      top: clip.top === false ? 0 : area.top - clip.top,
-      bottom: clip.bottom === false ? me.height : area.bottom + clip.bottom
-    });
+    if (useClip) {
+      clipArea(ctx, {
+        left: clip.left === false ? 0 : area.left - clip.left,
+        right: clip.right === false ? me.width : area.right + clip.right,
+        top: clip.top === false ? 0 : area.top - clip.top,
+        bottom: clip.bottom === false ? me.height : area.bottom + clip.bottom
+      });
+    }
 
     meta.controller.draw();
 
-    unclipArea(ctx);
+    if (useClip) {
+      unclipArea(ctx);
+    }
 
     args.cancelable = false;
     me.notifyPlugins('afterDatasetDraw', args);
index 231ade5fa1978d5b5233d4ab7461b4e91cd01763..5842f1e228f5297992f1ec36f02ba7fa6972dad7 100644 (file)
@@ -51,7 +51,8 @@ function toClip(value) {
     top: t,
     right: r,
     bottom: b,
-    left: l
+    left: l,
+    disabled: value === false
   };
 }
 
index dfebd643771e47eaf62f90e5ac99c45767812f46..e372d4c20a4caf0c70341e4eeb602d013089c4f2 100644 (file)
@@ -1628,4 +1628,31 @@ describe('Chart.controllers.bar', function() {
       expect(chart.scales.y.getMinMax()).toEqual({min: -10, max: 10});
     });
   });
+
+  describe('clip', function() {
+    it('Should not use ctx.clip when clip=false', function() {
+      var ctx = window.createMockContext();
+      ctx.resetTransform = function() {};
+
+      var chart = window.acquireChart({
+        type: 'bar',
+        data: {
+          labels: ['a', 'b', 'c'],
+          datasets: [{
+            data: [1, 2, 3],
+            clip: false
+          }]
+        }
+      });
+      var orig = chart.ctx;
+
+      // Draw on mock context
+      chart.ctx = ctx;
+      chart.draw();
+
+      chart.ctx = orig;
+
+      expect(ctx.getCalls().filter(x => x.name === 'clip').length).toEqual(0);
+    });
+  });
 });