简体   繁体   中英

How to fix Uncaught Error: This method is not implemented in react-chartjs-2

I'm using react-chartjs-2 to create a line graph. In that grpah, I wanna use time in xAxes. But I'm getting the following error.

Chart.js:10609 Uncaught Error: This method is not implemented: either no adapter can be found or an incomplete integration was provided

Here is my code.

render() {
    var startDate = moment().subtract(1, 'year').format('MMM YYYY');
    var endDate = moment().format('MMM YYYY');
    const data = {
      labels: [startDate, endDate],
      datasets: [
        {
          label: 'NPS Score',
          fill: false,
          lineTension: 0.2,
          backgroundColor: 'rgba(75,192,192,0.4)',
          borderColor: 'rgba(75,192,192,1)',
          borderDash: [],
          borderDashOffset: 0.0,
          pointBorderColor: 'rgba(75,192,192,1)',
          pointBackgroundColor: '#fff',
          pointBorderWidth: 1,
          pointHoverRadius: 5,
          pointHoverBackgroundColor: 'rgba(75,192,192,1)',
          pointHoverBorderColor: 'rgba(220,220,220,1)',
          pointHoverBorderWidth: 2,
          pointRadius: 1,
          pointHitRadius: 10,
          data: this.state.data,
        },
      ],
    };

    const lineOptions = {
      scales: {
        xAxes: [
          {
            type: 'time',
            time: {
              unit: 'month',
              tooltipFormat: 'lll',
            },
            ticks: {
              maxTicksLimit: 12,
            },
          },
        ],
        yAxes: [
          {
            stacked: true,
            gridLines: {
              display: false,
            },
            ticks: {
              suggestedMin: 100,
              suggestedMax: 0,
            },
          },
        ],
      },
      legend: {
        display: false,
      },
      tooltips: {
        enabled: true,
      },
    };
    return <Line data={data} options={lineOptions} height={120} />;

I can't find a way to fix this. There are some questions that has the same error but not with react js. Can anyone help me with this. thanks in advance.

The problem is with moment.js. The newer version has an issue. Your best bet is to downgrade the moment.js version. Here is how I fixed this.

1) Update your dependencies versions in the package.json

"dependencies": {
    "chart.js": "2.9.3",
    "moment": "2.24.0",
    "moment-timezone": "^0.5.28",
    "react-chartjs-2": "2.9.0",
 }

2) Add the following code to the package.json

"resolutions": {
    "moment": "2.24.0"
},

Now the package.json should look like this

"dependencies": {
    "chart.js": "2.9.3",
    "moment": "2.24.0",
    "moment-timezone": "^0.5.28",
    "react-chartjs-2": "2.9.0",
},
"resolutions": {
    "moment": "2.24.0"
},

Note that I haven't included all the packages here.

3) Delete the entire node_modules folder and .lock file (yarn.lock or package-lock.json)

4) run yarn install or npm install

This worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM