]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add resizeDelay option (#8509)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 23 Feb 2021 23:40:57 +0000 (01:40 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Feb 2021 23:40:57 +0000 (18:40 -0500)
* Add resizeDelay option
* Extract helper

docs/docs/configuration/responsive.md
src/core/core.controller.js
src/helpers/helpers.extras.js

index d8b87f5dd91650cc3d74d8001951b8334b23d27c..37dcb67125f662d1ea39a523d040e1f0de253fee 100644 (file)
@@ -21,6 +21,7 @@ Namespace: `options`
 | `maintainAspectRatio` | `boolean` | `true` | Maintain the original canvas aspect ratio `(width / height)` when resizing.
 | `aspectRatio` | `number` | `2` | Canvas aspect ratio (i.e. `width / height`, a value of 1 representing a square canvas). Note that this option is ignored if the height is explicitly defined either as attribute or via the style.
 | `onResize` | `function` | `null` | Called when a resize occurs. Gets passed two arguments: the chart instance and the new size.
+| `resizeDelay` | `number` | `0` | Delay the resize update by give amount of milliseconds. This can ease the resize process by debouncing update of the elements.
 
 ## Important Note
 
index 3e9151a0ff61633636913e232269cff21f7927bd..7450587dfb97de9e7499b8845e6ab432e2f0a962 100644 (file)
@@ -11,6 +11,7 @@ import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual} fro
 import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
 // @ts-ignore
 import {version} from '../../package.json';
+import {debounce} from '../helpers/helpers.extras';
 
 /**
  * @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
@@ -122,6 +123,7 @@ class Chart {
     this.attached = false;
     this._animationsDisabled = undefined;
     this.$context = undefined;
+    this._doResize = debounce(() => this.update('resize'), options.resizeDelay || 0);
 
     // Add the chart instance to the global namespace
     instances[me.id] = me;
@@ -242,7 +244,10 @@ class Chart {
     callCallback(options.onResize, [newSize], me);
 
     if (me.attached) {
-      me.update('resize');
+      if (me._doResize()) {
+        // The resize update is delayed, only draw without updating.
+        me.render();
+      }
     }
   }
 
index 21857a6e993ba09867d0957dab1e53bd250fc7e0..703fbf208d483886775daf5ccf678997eafc1811 100644 (file)
@@ -40,6 +40,25 @@ export function throttled(fn, thisArg, updateFn) {
   };
 }
 
+/**
+ * Debounces calling `fn` for `delay` ms
+ * @param {function} fn - Function to call. No arguments are passed.
+ * @param {number} delay - Delay in ms. 0 = immediate invocation.
+ * @returns {function}
+ */
+export function debounce(fn, delay) {
+  let timeout;
+  return function() {
+    if (delay) {
+      clearTimeout(timeout);
+      timeout = setTimeout(fn, delay);
+    } else {
+      fn();
+    }
+    return delay;
+  };
+}
+
 
 /**
  * Converts 'start' to 'left', 'end' to 'right' and others to 'center'