From: Jacco van den Berg Date: Mon, 26 Sep 2022 18:20:54 +0000 (+0200) Subject: Add warning if filler plugin is used but not registered (#10702) X-Git-Tag: v4.0.0~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5f37ba6fc44f427ad1afc50cc36f71a098e45c29;p=thirdparty%2FChart.js.git Add warning if filler plugin is used but not registered (#10702) * add warning if filler plugin is used but not registered * fix lint * increase size limit * add test, only put warning in dataset controller * fix register in docs, remove commented code * remove other mr * add documentation * Apply suggestions from code review Co-authored-by: Jukka Kurkela * Also return false if plugin is disabled by options * improve warning message * undo docs changes * update test Co-authored-by: Jukka Kurkela --- diff --git a/.size-limit.cjs b/.size-limit.cjs index 11673e883..175477082 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -7,7 +7,7 @@ function modifyWebpackConfig(config) { module.exports = [ { path: 'dist/chart.js', - limit: '77 KB', + limit: '77.2 KB', webpack: false, running: false }, @@ -41,7 +41,7 @@ module.exports = [ }, { path: 'dist/chart.js', - limit: '22.2 KB', + limit: '22.4 KB', import: '{ CategoryScale, LinearScale, LogarithmicScale, RadialLinearScale, TimeScale, TimeSeriesScale }', running: false, modifyWebpackConfig diff --git a/docs/developers/api.md b/docs/developers/api.md index 12b786631..4720b755f 100644 --- a/docs/developers/api.md +++ b/docs/developers/api.md @@ -200,6 +200,14 @@ chart.setActiveElements([ ]); ``` +## isPluginEnabled(pluginId) + +Returns a boolean if a plugin with the given ID has been registered to the chart instance. + +```javascript +chart.isPluginEnabled('filler'); +``` + ## Static: getChart(key) Finds the chart instance from the given key. If the key is a `string`, it is interpreted as the ID of the Canvas node for the Chart. The key can also be a `CanvasRenderingContext2D` or an `HTMLDOMElement`. This will return `undefined` if no Chart is found. To be found, the chart must have previously been created. diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 17394c933..396193a17 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -1136,6 +1136,15 @@ class Chart { return this._plugins.notify(this, hook, args, filter); } + /** + * Check if a plugin with the specific ID is registered and enabled + * @param {string} pluginId - The ID of the plugin of which to check if it is enabled + * @returns {boolean} + */ + isPluginEnabled(pluginId) { + return this._plugins._cache.filter(p => p.plugin.id === pluginId).length === 1; + } + /** * @private */ diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index 2d545ec9f..d241ec8f3 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -267,6 +267,10 @@ export default class DatasetController { this.linkScales(); meta._stacked = isStacked(meta.vScale, meta); this.addElements(); + + if (this.options.fill && !this.chart.isPluginEnabled('filler')) { + console.warn("Tried to use the 'fill' option without the 'Filler' plugin enabled. Please import and register the 'Filler' plugin and make sure it is not disabled in the options"); + } } updateIndex(datasetIndex) { diff --git a/test/specs/plugin.filler.tests.js b/test/specs/plugin.filler.tests.js index 067781ebe..a01f2ff53 100644 --- a/test/specs/plugin.filler.tests.js +++ b/test/specs/plugin.filler.tests.js @@ -1,4 +1,5 @@ describe('Plugin.filler', function() { + const fillerPluginRegisterWarning = 'Tried to use the \'fill\' option without the \'Filler\' plugin enabled. Please import and register the \'Filler\' plugin and make sure it is not disabled in the options'; function decodedFillValues(chart) { return chart.data.datasets.map(function(dataset, index) { var meta = chart.getDatasetMeta(index) || {}; @@ -10,6 +11,43 @@ describe('Plugin.filler', function() { describe('auto', jasmine.fixture.specs('plugin.filler')); describe('dataset.fill', function() { + it('Should show a warning when trying to use the filler plugin in the dataset when it\'s not registered', function() { + spyOn(console, 'warn'); + Chart.unregister(Chart.Filler); + window.acquireChart({ + type: 'line', + data: { + datasets: [{ + fill: true + }] + } + }); + + expect(console.warn).toHaveBeenCalledWith(fillerPluginRegisterWarning); + + Chart.register(Chart.Filler); + }); + + it('Should show a warning when trying to use the filler plugin in the root options when it\'s not registered', function() { + // jasmine.createSpy('warn'); + spyOn(console, 'warn'); + Chart.unregister(Chart.Filler); + window.acquireChart({ + type: 'line', + data: { + datasets: [{ + }] + }, + options: { + fill: true + } + }); + + expect(console.warn).toHaveBeenCalledWith(fillerPluginRegisterWarning); + + Chart.register(Chart.Filler); + }); + it('should support boundaries', function() { var chart = window.acquireChart({ type: 'line', diff --git a/types/index.d.ts b/types/index.d.ts index 7a49a3e29..4304319fd 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -530,6 +530,8 @@ export declare class Chart< notifyPlugins(hook: string, args?: AnyObject): boolean | void; + isPluginEnabled(pluginId: string): boolean; + static readonly defaults: Defaults; static readonly overrides: Overrides; static readonly version: string;