ChartJS 插件数据标签在 Ionic 3 上不显示标签

ChartJS plugin datalabels not showing labels on Ionic 3

我正在使用 Ionic 3,我正在显示一个条形图,我需要在图表上的条形旁边显示数据标签,就像这个问题

我按照目前找到的有关 chartjs-plugin-datalabels 的说明进行了操作,但没有用,它没有在我的图表上显示任何数据标签。

我通过 npm 安装了 chartjs 和 chartjs-plugin-datalabels,它们在我的 package.json

中看起来像这样
"chart.js": "^2.7.3",
"chartjs-plugin-datalabels": "^0.5.0",

我做了导入,就像文档对 https://chartjs-plugin-datalabels.netlify.com/guide/getting-started.html#integration 说的那样,但是

的导入行
import { ChartDataLabels } from 'chartjs-plugin-datalabels'; 

在 VSCode 编辑器中显示为“'ChartDataLabels' 已声明但其值从未被读取”

我还在 'options' 中添加了一个 'plugin' 参数,就像文档所说的那样,但仍然没有显示任何数据标签。

这是我的代码的摘录。

import { Component, ViewChild, ɵConsole } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, App } from 'ionic-angular';
import { Chart } from 'chart.js';  
import { ChartDataLabels } from 'chartjs-plugin-datalabels';


@ViewChild('barCanvas') barCanvas;

ionViewDidLoad() {
 this.barChart = new Chart(this.barCanvas.nativeElement, {
  type: 'bar',
  data: {
    labels: [..],
    datasets: [
      {
        label: "Ok",
        data: arrayOk,
        backgroundColor: '#014582',
        borderWidth: 1,
        datalabels: {
          align: 'end',
          anchor: 'start'
        }
      },
      {
        label: "Not Okay",
        data: arrayNotOk,
        backgroundColor: '#777676',
        borderWidth: 1,
        datalabels: {
          align: 'center',
          anchor: 'center'
        }
      }],
  },
  options: {
    plugins: {
      datalabels: {
        color: 'white',
        display: function (context) {
          return context.dataset.data[context.dataIndex] > 1;
        },
        font: {
          weight: 'bold'
        },
        formatter: Math.round,

        title: false
      }
    },
    title: {
      display: true,
      position: "top",
      text: "My Bar Chart with Datalabels",
      fontSize: 18,
      fontColor: "#111"
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }],
      xAxes: [{
        ticks: {
          autoSkip: false
        }
      }]
    },
    legend: false
  }
});
}

尝试启用插件

    var chart = new Chart(ctx, {
        plugins: [ChartDataLabels],
        options: {
            // ...
        }
    })

我通过在 class 构造函数中添加一个属性并将其传递给 ChartLabel 插件 属性 解决了我的问题。

import { Chart } from 'chart.js';
import { ChartDataLabels } from 'chartjs-plugin-datalabels';

export class Chart {

  chartLabels: any;

  constructor( ... ){
    this.chartLabels = ChartDataLabels;
  }
}