SeanSobey / ChartjsNodeCanvas

A node renderer for Chart.js using canvas.
MIT License
230 stars 79 forks source link

ChartJS.plugins.register not working with chartjs version 3.x #67

Closed HoangMinh991 closed 3 years ago

HoangMinh991 commented 3 years ago

ChartJS.plugins.register not working with chartjs 3.x version , it only working with 2.x version UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'register' of undefined

Cartman720 commented 3 years ago

Hi @HoangMinh991,

Recently I encountered this problem and could solve without significant effort.

The reason of the problem is breaking change in v3, ChartJS.plugins.register was replaced by ChartJS.register, therefore you can do this trick to solve the issue.

import { ChartJSNodeCanvas } from 'chartjs-node-canvas';

const canvas = new ChartJSNodeCanvas({
  width: 700,
  height: 400,
  chartCallback: (ChartJS) => {
    /** Add plugins here **/
    ChartJS.register(require('chartjs-plugin-datalabels'))
  }
});

Hope this helped you!

Regards, Aren Hovsepyan

SeanSobey commented 3 years ago

I have not yet looked at officially supporting 3.x.x yet (hence the peer dependency of ^2.7.3), but it seems like Aren's fix should work. Thanks for posting @Cartman720 ! I will look at trying to support v3 soon