Ng2-charts: How to integrate plugin of chartJs

Created on 16 Mar 2020  路  6Comments  路  Source: valor-software/ng2-charts

How to integrate below plugin in ng2-charts:

https://github.com/nagix/chartjs-plugin-style

Most helpful comment

Insall plugin

npm install chartjs-plugin-datalabels --save
````
Use this
```js
import * as pluginDataLabels from 'chartjs-plugin-datalabels';

In your class

public pieChartPlugins = [pluginDataLabels];
public options = {
   tooltips: {
     enabled: false
   },
   responsive: true,
   plugins: {
        datalabels: {
           formatter: (value, ctxt) => {
             let percentage;
             let datasets = ctxt.chart.data.datasets;
             if(datasets.indexOf(ctxt.dataset) === datasets.length - 1) {
               let sum = datasets[0].data.reduce((a, b) => a + b, 0);
               let percentage = Math.round((value / sum) * 100) + '%';
               return percentage;
             } else {
               return percentage;
             }
           },
           color: '#fff',
        }
    }
  }

In your html

<canvas baseChart
    [data]="data"
        [labels]="labels"
     [chartType]="chart_type"
    [options]="options"
    [plugins]="pieChartPlugins"></canvas>

All 6 comments

Do you find the answer for this ? I need

How to integrate below plugin in ng2-charts:

https://github.com/nagix/chartjs-plugin-style

@hadpro24 No I didn't find solution yet but I move on due to inactivity 馃憤

Ok. I already find solution

@hadpro24 Would you mind sharing solution here for other community members then we can close this issue

Insall plugin

npm install chartjs-plugin-datalabels --save
````
Use this
```js
import * as pluginDataLabels from 'chartjs-plugin-datalabels';

In your class

public pieChartPlugins = [pluginDataLabels];
public options = {
   tooltips: {
     enabled: false
   },
   responsive: true,
   plugins: {
        datalabels: {
           formatter: (value, ctxt) => {
             let percentage;
             let datasets = ctxt.chart.data.datasets;
             if(datasets.indexOf(ctxt.dataset) === datasets.length - 1) {
               let sum = datasets[0].data.reduce((a, b) => a + b, 0);
               let percentage = Math.round((value / sum) * 100) + '%';
               return percentage;
             } else {
               return percentage;
             }
           },
           color: '#fff',
        }
    }
  }

In your html

<canvas baseChart
    [data]="data"
        [labels]="labels"
     [chartType]="chart_type"
    [options]="options"
    [plugins]="pieChartPlugins"></canvas>

thanks @hadpro24

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrpotato3 picture mrpotato3  路  5Comments

grahammutter picture grahammutter  路  4Comments

shyamal890 picture shyamal890  路  4Comments

sarn3792 picture sarn3792  路  4Comments

egervari picture egervari  路  4Comments