React Chart.JS无法移动图例(TypeScript)

hvvq6cgz  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(185)

我有一个包含以下数据的圆环图组件:

const data = {
    labels: ["1", "2"],
    datasets: [
      {
        label: "peers",
        data: [1, 2],
        backgroundColor: ["#56E2CF", "#56AEE2"]
      }
    ]
  };

数据正常工作。以下是选项:

const options = {
    legend: {
      display: "right"
    }
  };

元件环圈图:

<Doughnut data={data} options={options} />

但图表仍然没有标题:

我尝试使用插件将选项设置为char.js样式:

const options = {
    plugins: {
      legend: {
        position: "right"
      }
    }
  };

图例移动,但出现类型错误(仍无标题):The types of 'plugins.legend.position' are incompatible between these types. Type 'string' is not assignable to type '"right" | "left" | "top" | "bottom" | "center" | "chartArea" | _DeepPartialObject<{ [scaleId: string]: number; }> | undefined'
请注意标题是如何向右移动的:

cyvaqqii

cyvaqqii1#

const options = {
    plugins: {
      legend: {
        position: "right" as const
      },
      title: {
        display: true,
        text: "Title"
      }
    }
  };

下面是来自TypeScript文档的说明-https://devblogs.microsoft.com/typescript/announcing-typescript-3-4-rc/#const-assertions

相关问题