ChartJS 错误:“category”不是注册的比例

dfddblmv  于 2022-12-29  发布在  Chart.js
关注(0)|答案(4)|浏览(276)

我尝试将Chart.js从2.9.3迁移到3.3.0,即使在应用更改(https://www.chartjs.org/docs/latest/getting-started/v3-migration.html)后,我仍然收到错误:
Error: "category" is not a registered scale.
这是我的

Chart.register(BarController, DoughnutController, LineController, PieController);
new Chart(this.id, {
    type: 'bar',
    data,
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
        title: {
            display: options.plugins.title ? true : false,
        },        
        tooltip: {
            mode: 'index',
            intersect: false
        },        
        scales: {
        x: {
            stacked: true,
            gridLines: {
            drawBorder: false,
            display: false,
            },
            ticks: {
            autoSkip: true,
            maxTicksLimit: 13,
            },
        },
        y: {
            stacked: true,
            gridLines: {
            color: '#e6e6e6',
            drawBorder: false,
            },
        }
    }
});

我能错过什么呢?

57hvy0tb

57hvy0tb1#

就像错误所说的,您正在使用category scale,所以您需要导入并注册它,如下所示:

import {CategoryScale} from 'chart.js'; 
Chart.register(CategoryScale);

或者你可以选择不使用shaking并像这样导入所有内容:

import Chart from 'chart.js/auto';

对于所有可用的东西,你可能需要导入和注册,请看这里:https://www.chartjs.org/docs/3.3.0/getting-started/integration.html#bundlers-webpack-rollup-etc

kq4fsx7k

kq4fsx7k2#

如果您使用的是react-chartjs-2
无树摇动:

import { Chart as ChartJS } from 'chart.js/auto'
import { Chart }            from 'react-chartjs-2'

树摇动:

import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
} from 'chart.js'
import { Chart } from 'react-chartjs-2'

ChartJS.register(
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend
)
v1uwarro

v1uwarro3#

与react-chartjs-2一起使用并导入所有内容;将图表更改为chartjs,以便它不会显示从react图表导入图表的错误

import { Chart as ChartJS, registerables } from 'chart.js';
import { Chart } from 'react-chartjs-2'
ChartJS.register(...registerables);
bjp0bcyl

bjp0bcyl4#

我正在使用折线图,我在更新到next.js版本时遇到了这个问题,下面的解决方案对我来说很好,解决了destroy()之前的图表错误
从“chart.js/auto”导入图表;从“react-chartjs-2”导入{ Line };

相关问题