Chart.js属性'type'的类型不兼容,类型'string'不能赋给类型'“line”|“栏”|“分散”

q5lcpyga  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(179)

第一个
问题出在type: 'line',.chart.js中,它不工作,不与任何类型一起工作,抛出错误。

2fjabf4q

2fjabf4q1#

您的TypeScript编译器似乎有问题,因为图表type属性是特定类型而不是字符串。

选项1:

导入图表类型并将type转换为ChartType

import {  Chart, ChartType, ...} from 'chart.js';

 ...

function createChartConfig({labels, data, label, color}: ChartConfig) {
  return {
    type: 'line' as ChartType,
    options: {
   ...

选项2:(更简单/更简单的方法)

将整个配置强制转换为any。

new Chart(gainCtx, createChartConfig(gainConfig) as any);

在任何情况下,您都需要导入和注册所需的控制器、元件、刻度等。注册registerables可以注册所有内容,但最好只注册您的特定图表所需的内容。更多信息请参见Integration instructions

import { Chart, registerables } from 'chart.js';
...
Chart.register(...registerables);
j0pj023g

j0pj023g2#

我有一个类似的错误,当谷歌的错误,所以这个问题出现了,所以我会在这里下降一个相关的解决方案。
这在某种程度上是一个变通办法,但我得到了这个错误:

Types of property 'borderJoinStyle' are incompatible.
            Type 'string' is not assignable to type '"miter" | "round" | "bevel" | ((ctx: ScriptableContext<"line">, options: AnyObject) => CanvasLineJoin | undefined) | undefined'.

为了解决这个问题,我将设置从borderJoinStyle: "miter"更改为borderJoinStyle: "miter" as const
我也收到这个错误:

Types of property 'borderCapStyle' are incompatible.
            Type 'string' is not assignable to type '"butt" | "round" | "square" | ((ctx: ScriptableContext<"line">, options: AnyObject) => CanvasLineCap | undefined) | undefined'.

我做了一个与上面类似的更改。从borderCapStyle: "round"更改为borderCapStyle: "round" as const
我在Github上找到了答案:
https://github.com/apexcharts/react-apexcharts/issues/347#issuecomment-932848265

相关问题