【问题标题】:Datalabels plugin throws error when using scriptable option使用可编写脚本的选项时,Datalabels 插件会引发错误
【发布时间】:2019-05-01 05:04:35
【问题描述】:

我有一个圆环图。我正在使用 chartjs-plugin-datalabels。对于背景颜色,我使用以下配置:

plugins: {
      datalabels: {
        backgroundColor: function(context) {
          return context.dataset.backgroundColor;
        }
}

基于docs,背景颜色属性应该允许脚本选项。

我收到以下错误:

error TS2322: Type '(context: Context) => string | string[] | CanvasGradient | CanvasPattern | ChartColor[]' is not assignable to type 'string | CanvasGradient | CanvasPattern | (string | CanvasGradient | CanvasPattern)[] | ((context: Context) => string | CanvasGradient | CanvasPattern)'.

我正在使用以下库版本:

"chart.js": "^2.8.0",
"chartjs-plugin-datalabels": "^0.6.0",

【问题讨论】:

  • 我认为问题在于context.dataset.backgroundColor 是一个数组,因为每个数据都会调用该函数,所以使用索引可能会有所帮助,试试这样的return context.dataset.backgroundColor[context.dataIndex]
  • @FranciscoSoares 成功了。回答,我会接受。

标签: chart.js


【解决方案1】:

由于饼图为其中的每个数据集定义了一个 backgroundColor 数组,并且为图表上的每个数据标签调用了该函数,因此您需要一种方法来判断该函数应该使用数组中的哪种颜色,可能使用索引可以解决问题。

试试这样的:

return context.dataset.backgroundColor[context.dataIndex];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多