score:0

this took me forever to figure out so adding what worked for me here in case anybody else struggles with this in angular 2+:

app.module.ts:

import * as chartlabels from 'chartjs-plugin-labels';

...

export class appmodule {
  constructor() {
    basechartdirective.unregisterplugin(chartlabels); // this makes chart plugins work in components
  }
}

component.ts:

... // other imports
import * as chartlabels from 'chartjs-plugin-labels';

  ... // component annotations
  export class mychartcomponent {

    ... // other chart members

    public doughnutchartplugins = [chartlabels];
    public doughnutchartoptions: chartoptions = { 
      responsive: true,
      maintainaspectratio: true,
      plugins: {
        labels: {
          render: 'value',
        }
      },
    };

   ... // constructor and so on

component.html

<canvas basechart
    [data]="doughnutchartdata"
    [labels]="doughnutchartlabels"
    [charttype]="doughnutcharttype"
    [options]="doughnutchartoptions"
    [plugins]="doughnutchartplugins"
    [legend]="doughnutchartlegend"
    [colors]="doughnutchartcolors"
>
</canvas>

score:1

for example drawing in center of doughnut chart i find workaround using it in options animation, so no need to refister a plugin

    animation: {
      onprogress: function(chart) {
        let width = chart.chart.width,
          height = chart.chart.height,
          ctx = chart.chart.ctx;

        ctx.restore();
        let fontsize = (height / 114).tofixed(2);
        ctx.font = fontsize + "em sans-serif";
        ctx.textbaseline = "middle";
        ctx.fillstyle = '#dddddd';

        let text = "75%",
          textx = math.round((width - ctx.measuretext(text).width) / 2),
          texty = height / 2;

        ctx.filltext(text, textx, texty);
        ctx.save();
      },
      oncomplete: function(chart) {
        let width = chart.chart.width,
          height = chart.chart.height,
          ctx = chart.chart.ctx;

        ctx.restore();
        let fontsize = (height / 114).tofixed(2);
        ctx.font = fontsize + "em sans-serif";
        ctx.textbaseline = "middle";
        ctx.fillstyle = '#dddddd';

        let text = "75%",
          textx = math.round((width - ctx.measuretext(text).width) / 2),
          texty = height / 2;

        ctx.filltext(text, textx, texty);
        ctx.save();
      },
    },

score:4

maybe follow this thread (https://github.com/valor-software/ng2-charts/issues/496) in case there becomes a more "official" way, but here is what i did:

at the top of your component:

declare var chart: any;

that will stop typescript from complaining and give you access to the chart object.

then you can use:

chart.pluginservice.register

here's an example of code for a plugin that i was using: https://github.com/chartjs/chart.js/issues/78#issuecomment-220829079

update (may 2018): this answer is likely not valid or the best way to do this anymore.

score:7

i would avoid to declare chart like this. instead you can do import {chart} from 'chart.js' since it is a subdependency of ng2-charts anyway.

by this approach your ide can still do autocompletion and you are not telling angular to just believe that there is something called chart.

to be consistent you should also add it to your package.json.


Related Query