Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/configuration/responsive.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/helpers/helpers.extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down