score:77

Accepted answer

try importing chartsmodule in your app.module.ts like this-

import { chartsmodule } from 'ng2-charts/ng2-charts';

imports: [
   .....
   chartsmodule
   .....
]

score:-1

in my case i had to change how i import and a little change in my html file tag:

chart.component.ts

import {component} from '@angular/core';
import {chart_directives} from 'ng2-charts/ng2-charts';

@component({
    selector: 'chart-balance',
    template: require('./templates/chart-balance.template'),
    styles: [`.chart {display: block; width: 100%;}`],
    directives: [chart_directives]
})

export class chartbalancecomponent {
    public barchartoptions:any = {
        scaleshowverticallines: false,
        responsive: true,
        scales: {
            yaxes: [{
                ticks: {
                    beginatzero: true,
                    callback: label => `${label.tolocalestring()}`
                }
            }]
        }
    };
    public barchartlabels:string[] = ['2014', '2015', '2016'];
    public barcharttype:string = 'bar';
    public barchartlegend:boolean = true;

    public barchartdata:any[] = [
        {data: [32131, 3432, 543], label:'test 1'},
        {data: [54353, 432, 768], label:'test 2'}
    ];

    // events
    public chartclicked(e:any):void {
        console.log(e);
    }

    public charthovered(e:any):void {
        console.log(e);
    }

}

here was another change i did:

notice -- > base-chart class="chart" ...

chart-balance.template

<base-chart class="chart"
        [datasets]="barchartdata"
        [labels]="barchartlabels"
        [options]="barchartoptions"
        [legend]="barchartlegend"
        [charttype]="barcharttype"
        (charthover)="charthovered($event)"
        (chartclick)="chartclicked($event)"></base-chart>

ng2-charts

export * from './components/charts/charts';
declare var _default: {
    directives: any[][];
};
export default _default;

charts

import { ondestroy, oninit, onchanges, eventemitter, elementref } from '@angular/core';
export declare class basechartcomponent implements ondestroy, onchanges, oninit {
    static defaultcolors: array<number[]>;
    data: number[] | array<number[]>;
    datasets: any[];
    labels: array<any>;
    options: any;
    charttype: string;
    colors: array<any>;
    legend: boolean;
    chartclick: eventemitter<any>;
    charthover: eventemitter<any>;
    private ctx;
    private cvs;
    private parent;
    private chart;
    private initflag;
    private element;
    constructor(element: elementref);
    ngoninit(): any;
    ngonchanges(): any;
    ngondestroy(): any;
    getchartbuilder(ctx: any): any;
    private refresh();
}
export interface color {
    backgroundcolor?: string | string[];
    borderwidth?: number | number[];
    bordercolor?: string | string[];
    bordercapstyle?: string;
    borderdash?: number[];
    borderdashoffset?: number;
    borderjoinstyle?: string;
    pointbordercolor?: string | string[];
    pointbackgroundcolor?: string | string[];
    pointborderwidth?: number | number[];
    pointradius?: number | number[];
    pointhoverradius?: number | number[];
    pointhitradius?: number | number[];
    pointhoverbackgroundcolor?: string | string[];
    pointhoverbordercolor?: string | string[];
    pointhoverborderwidth?: number | number[];
    pointstyle?: string | string[];
    hoverbackgroundcolor?: string | string[];
    hoverbordercolor?: string | string[];
    hoverborderwidth?: number;
}
export interface colors extends color {
    data?: number[];
    label?: string;
}
export declare const chart_directives: array<any>;

score:0

instead of importing the chartsmodule in app module, import it in inside module where your are using. for example, home module (if using charts in home page).

score:0

in case you are using a shared module, you also need to export the chartsmodule from there, to make it available to all your components which use your shared module.

// simplified module only showing what's important for chartsmodule
import { ngmodule } from '@angular/core';
import { chartsmodule } from 'ng2-charts';

@ngmodule({
    declarations: [],
    imports: [chartsmodule],
    exports: [chartsmodule]
})

export class sharedmodule {
    static forroot(): modulewithproviders<sharedmodule> {
        return {
            ngmodule: sharedmodule
        };
    }
}

score:0

in my case, it was nothing to do with including chartsmodule in multiple modules or exporting it.

i had created a chart component that used ng-chart and then found i needed another version of it. so i copied the component, renamed the class and template file, but forgot to update the templateurl property in the @component decorator.

having two different components referencing the same template file gave the exact same error:

can't bind to 'datasets' since it isn't a known property of 'canvas'

as soon as i changed the new component to use the correct html template, the error was resolved.

fyi, i reference chartsmodule as shown in @sanket's answer in the module of my chart components and the module of the page components that reference them (so it's not needed in appmodule).

score:1

if your component is declared in another module other than app.module.ts you will simply do the following:

app.module.ts

import { chartsmodule } from 'ng2-charts';

...

@ngmodule({
   ...

   imports: [
      ...
      chartsmodule,
      ...
   ]
})
export class appmodule{ }

module-where-your-component-is-declared.module.ts

import { chartsmodule } from 'ng2-charts';

...

@ngmodule({
   declarations: [yourcomponent],
   imports: [
      ...
      chartsmodule,
      ...
   ],
   ...
})
export class modulewhereyourcomponentisdeclared { }

score:2

the reason might be: you are using ng2charts directly from your child components/module.

solutions: you have to import the ng2charts on your parent module as well. (import ng2charts [ng2-charts], to your parent and child module)

happy fixing everyone.

thank you. marjun villegas

score:8

i'm working with ng2-chart@3.0.0-rc.2

looks like the property charttype is now called type

so it should look something like this:

    <canvas 
      style="display: block;background-color: #3a3939;" 
      width="600" 
      height="300" 
      id="canvas" 
      #mycanvas
      
      basechart 
      [type]="linecharttype" 
      [datasets]="linechartdata" 
      [labels]="linechartlabels" 
      [options]="mfchartoptions"
      [legend]="linechartlegend">
    </canvas>

score:18

i'm working with ng2-charts + angular 7 + ionic 4, and i spent several hours searching for a solution. and this finally worked to me (after following the initial steps, of course, like installing ng2-charts and charts.js). just import chartsmodule on the following files:

app.module.ts

import { chartsmodule } from 'ng2-charts';
...
imports: [chartsmodule]

yourpage.module.ts

import { chartsmodule } from 'ng2-charts';
@ngmodule({
  imports: [
    ionicmodule,
    commonmodule,
    formsmodule,
    routermodule.forchild([{ path: '', component: yourpagepage }]),
    **chartsmodule**
  ]
})

i was trying to import it also in yourpage.page.ts, but the solution was to import it in yourpage.module.ts!

try it out.

score:27

i had the very same problem. i found on github that you just have to insert the chartsmodule in the submodule as well. first you add in app.module.ts and the, in my case, reports.module.ts.


Related Query

More Query from same tag