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
Loading global plugin seems not to work #8
Comments
So the Chart JS plugin and specifically global plugin registration API is fairly new and unfortunately, a lot of plugins still use the old method of expecting a global Chart variable or So you will have to address and load each plugin based on how its implemented, for example Firstly, it retrieves its own reference to module.exports = factory(require('chart.js')) Then it registers itself: // TODO Remove at version 1, we shouldn't automatically register plugins.
// https://github.com/chartjs/chartjs-plugin-datalabels/issues/42
Chart.plugins.register(plugin); And finally returns itself: return plugin; I have updated the package to allow for your use case and added to the readme to include some details about loading plugins. For your requirements: const freshRequire = require('fresh-require');
const callback = (ChartJS) => {
ChartJS.plugins.register(freshRequire('chartjs-plugin-annotation', require));
};
const chartJsFactory = () => {
const chartJS = require('chart.js');
require('chartjs-plugin-datalabels');
delete require.cache[require.resolve('chart.js')];
delete require.cache[require.resolve('chartjs-plugin-datalabels')];
return chartJS;
};
const canvasRenderService = new CanvasRenderService(width, height, callback, undefined, chartJsFactory); See the tests for more details where I have each plugin working. |
nice, the solutions works perfect 👍 thanks for the super fast support |
Hello, I tried your solution i got this issue :
I'm using TypeScript on NestJS :
Here is my tsconfig :
Is it because of the way Typescript resolves modules ? Thanks |
What is the best way to load a global plugin ?
In this case chartjs-plugin-datalabels (https://chartjs-plugin-datalabels.netlify.com/) and chartjs-plugin-annotation (https://github.com/chartjs/chartjs-plugin-annotation)
Trying to require it seems not to load the module as an global plugin or cant be registered.
package.json
index.js
The text was updated successfully, but these errors were encountered: