How to import Chart.js chartjs-plugin-datalabels npm package into an Angular 7 project

后端 未结 1 637
有刺的猬
有刺的猬 2021-01-15 16:53

I have an Angular 7 (Ionic 4) project, where I trying out Chart.js and need to be able to custom draw some labels on a Pie chart, as I Have asked here.

I have been t

1条回答
  •  再見小時候
    2021-01-15 17:29

    You can import the directive like this:

    import * as pluginDataLabels from 'chartjs-plugin-datalabels';
    

    You can use Plugins in your ChartOptions like this:

    chartOptions: ChartOptions = {
      ...
      plugins: {
        pluginDataLabels 
      }
    

    Another way is to call it in your chart:

    public barChartPlugins = [pluginDataLabels];
    

    You can see it done here.

    However, both ways declare it globally. The only way I could figure out not to see them in all charts is to not display them.

    chartOptions: ChartOptions = {
      ...
      plugins: {
        datalabels: {
          display: false
        }, 
      }
    

    This also solves the Problem @NakulSharma has. You can see the plugin options here.

    0 讨论(0)
提交回复
热议问题