Skip to content

Commit ee74dd6

Browse files
authored
Add resizeDelay option (#8509)
* Add resizeDelay option * Extract helper
1 parent 4207645 commit ee74dd6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

docs/docs/configuration/responsive.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Namespace: `options`
2121
| `maintainAspectRatio` | `boolean` | `true` | Maintain the original canvas aspect ratio `(width / height)` when resizing.
2222
| `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.
2323
| `onResize` | `function` | `null` | Called when a resize occurs. Gets passed two arguments: the chart instance and the new size.
24+
| `resizeDelay` | `number` | `0` | Delay the resize update by give amount of milliseconds. This can ease the resize process by debouncing update of the elements.
2425

2526
## Important Note
2627

src/core/core.controller.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual} fro
1111
import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
1212
// @ts-ignore
1313
import {version} from '../../package.json';
14+
import {debounce} from '../helpers/helpers.extras';
1415

1516
/**
1617
* @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
@@ -122,6 +123,7 @@ class Chart {
122123
this.attached = false;
123124
this._animationsDisabled = undefined;
124125
this.$context = undefined;
126+
this._doResize = debounce(() => this.update('resize'), options.resizeDelay || 0);
125127

126128
// Add the chart instance to the global namespace
127129
instances[me.id] = me;
@@ -242,7 +244,10 @@ class Chart {
242244
callCallback(options.onResize, [newSize], me);
243245

244246
if (me.attached) {
245-
me.update('resize');
247+
if (me._doResize()) {
248+
// The resize update is delayed, only draw without updating.
249+
me.render();
250+
}
246251
}
247252
}
248253

src/helpers/helpers.extras.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ export function throttled(fn, thisArg, updateFn) {
4040
};
4141
}
4242

43+
/**
44+
* Debounces calling `fn` for `delay` ms
45+
* @param {function} fn - Function to call. No arguments are passed.
46+
* @param {number} delay - Delay in ms. 0 = immediate invocation.
47+
* @returns {function}
48+
*/
49+
export function debounce(fn, delay) {
50+
let timeout;
51+
return function() {
52+
if (delay) {
53+
clearTimeout(timeout);
54+
timeout = setTimeout(fn, delay);
55+
} else {
56+
fn();
57+
}
58+
return delay;
59+
};
60+
}
61+
4362

4463
/**
4564
* Converts 'start' to 'left', 'end' to 'right' and others to 'center'

0 commit comments

Comments
 (0)