ChartJS 插件数据标签未在 Ionic 3 上显示标签 [英] ChartJS plugin datalabels not showing labels on Ionic 3

查看:19
本文介绍了ChartJS 插件数据标签未在 Ionic 3 上显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm working with Ionic 3, I'm displaying a bar chart and I need to show data label next to the bar on chart, like this question how to display data values on Chart.js

I followed the instructions I have found so far for chartjs-plugin-datalabels but is not working, it's not showing any data label on my chart.

I installed chartjs and chartjs-plugin-datalabels via npm and they look like this in my package.json

"chart.js": "^2.7.3",
"chartjs-plugin-datalabels": "^0.5.0",

I did the imports just like the docs says to https://chartjs-plugin-datalabels.netlify.com/guide/getting-started.html#integration , but the import line for

import { ChartDataLabels } from 'chartjs-plugin-datalabels'; 

is showing in VSCode editor as "'ChartDataLabels' is declared but its value is never read"

I've also added inside 'options' a 'plugin' parameter, just like the docs says so, but stills not showing any datalabels.

Here is an extract of my code.

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
  }
});
}

解决方案

I solved my problem by adding an attribute and passing to it ChartLabel plugin property in class constructor.

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

export class Chart {

  chartLabels: any;

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

这篇关于ChartJS 插件数据标签未在 Ionic 3 上显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆