Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested scriptable options for datasets #9758

Merged
merged 2 commits into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/core/core.config.js
Expand Up @@ -371,12 +371,17 @@ function getResolver(resolverCache, scopes, prefixes) {
return cached;
}

const hasFunction = obj => Object.keys(obj).reduce((acc, key) => acc || isFunction(obj[key]), false);

function needContext(proxy, names) {
const {isScriptable, isIndexable} = _descriptors(proxy);

for (const prop of names) {
if ((isScriptable(prop) && isFunction(proxy[prop]))
|| (isIndexable(prop) && isArray(proxy[prop]))) {
const scriptable = isScriptable(prop);
const indexable = isIndexable(prop);
const value = (indexable || scriptable) && proxy[prop];
if ((scriptable && (isFunction(value) || (isObject(value) && hasFunction(value))))
|| (indexable && isArray(value))) {
return true;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/core.controller.js
Expand Up @@ -7,7 +7,7 @@ import PluginService from './core.plugins';
import registry from './core.registry';
import Config, {determineAxis, getIndexAxis} from './core.config';
import {retinaScale, _isDomSupported} from '../helpers/helpers.dom';
import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual, isNullOrUndef, setsEqual, defined} from '../helpers/helpers.core';
import {each, callback as callCallback, uid, valueOrDefault, _elementsEqual, isNullOrUndef, setsEqual, defined, isFunction} from '../helpers/helpers.core';
import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
// @ts-ignore
import {version} from '../../package.json';
Expand Down Expand Up @@ -536,14 +536,12 @@ class Chart {
* @private
*/
_updateDatasets(mode) {
const isFunction = typeof mode === 'function';

if (this.notifyPlugins('beforeDatasetsUpdate', {mode, cancelable: true}) === false) {
return;
}

for (let i = 0, ilen = this.data.datasets.length; i < ilen; ++i) {
this._updateDataset(i, isFunction ? mode({datasetIndex: i}) : mode);
this._updateDataset(i, isFunction(mode) ? mode({datasetIndex: i}) : mode);
}

this.notifyPlugins('afterDatasetsUpdate', {mode});
Expand Down
22 changes: 22 additions & 0 deletions test/specs/core.datasetController.tests.js
Expand Up @@ -897,6 +897,28 @@ describe('Chart.DatasetController', function() {
expect(opts0.$shared).not.toBeTrue();
expect(Object.isFrozen(opts0)).toBeFalse();
});

it('should support nested scriptable options', function() {
const chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: [100, 120, 130],
fill: {
value: (ctx) => ctx.type === 'dataset' ? 75 : 0
}
}]
},
});

const controller = chart.getDatasetMeta(0).controller;
const opts = controller.resolveDatasetElementOptions();
expect(opts).toEqualOptions({
fill: {
value: 75
}
});
});
});

describe('_resolveAnimations', function() {
Expand Down