首页 > 解决方案 > 从@types/chartjs 切换到@types/chart.js

问题描述

我一直在使用以下软件包https://www.npmjs.com/package/@types/chartjs,但现在已弃用。切换到https://www.npmjs.com/package/@types/chart.js. 我收到以下错误。

paydate.component.ts(42,45): error TS2345: Argument of type '{ type: string; 数据:{标签:数字[];数据集:{填充:真;线张力:数字;pointRadiu...' 不可分配给“ChartConfiguration”类型的参数。属性“数据”的类型不兼容。输入'{标签:数字[]; 数据集:{填充:真;线张力:数字;点半径:数字;borderWidth...' 不可分配给类型 'ChartData'。属性“标签”的类型不兼容。类型 'number[]' 不能分配给类型 '(string | string[])[]'。类型 'number' 不可分配给类型 'string | 细绳[]'。

import * as Chart from 'chart.js';

const myChart = new Chart(this.ctx, {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    fill: true,
                    lineTension: 0.5,
                    pointRadius: 0, // Removes indiviual points/tooltips
                    borderWidth: 1,
                    label: 'Monthly Spending',
                    data: data,
                    backgroundColor: 'rgb(124,190,235,0.6)',
                    borderColor: 'rgb(63,81,181)'
                }]
            },
            options: {
                plugins: {
                    datalabels: {
                        display: false,
                    }
                },
                legend: {
                    display: false
                },
                annotation: {
                    annotations: [
                        {
                            type: 'line',
                            mode: 'vertical',
                            scaleID: 'x-axis-0',
                            value: payday,
                            borderColor: 'red',
                            borderWidth: 2,
                            // label: {
                            //     content: 'TODAY',
                            //     enabled: true,
                            //     position: 'top'
                            // }
                        }
                    ]
                }
            }

        });

这里有什么问题?

更新:

将标签更改为字符串后,我得到了。

ERROR in src/app/reports/components/paydate/paydate.component.ts(69,17): error TS2345: Argument of type '{ type: string; data: { labels: string[]; datasets: { fill: true; lineTension: number; pointRadiu...' is not assignable to parameter of type 'ChartConfiguration'.
  Types of property 'options' are incompatible.
    Type '{ plugins: { datalabels: { display: boolean; }; }; legend: { display: false; }; annotation: { ann...' is not assignable to type 'ChartOptions'.
      Object literal may only specify known properties, but 'annotation' does not exist in type 'ChartOptions'. Did you mean to write 'rotation'?

标签: angulartypescriptchart.js

解决方案


推荐阅读