ng2-charts icon indicating copy to clipboard operation
ng2-charts copied to clipboard

I'm using Angular 17 and can't find NgChartsModule so i'cant import

Open KrisnaDhira opened this issue 1 year ago • 8 comments

Reproduction of the problem

ng2-charts is a port & modification of Chart.js component for Angular 2. Sometimes the issue is related with Chart.js instead of ng2-charts. To confirm, if the issue shows in a pure Chart.js project, it is a issue of Chart.js. Pure Chart.js starting template: https://jsfiddle.net/Hongbo_Miao/mvct2uwo/3/

If the issue does not show in a pure Chart.js project, and shows in a ng2-charts project, please try to provide a minimal demo of the problem. ng2-charts starting template: http://plnkr.co/edit/Ka4NXG3pZ1mXnaN95HX5?p=preview

KrisnaDhira avatar Aug 02 '24 09:08 KrisnaDhira

I also had the same issue. For Angular 17, it would be best if you install ng2-charts version 5.0.4. It would allow you to import 'NgChartsModule' in standalone components as well.

jenilvora0408 avatar Aug 14 '24 09:08 jenilvora0408

Hi @KrisnaDhira with new version using BaseChartDirective instead of NgChartsModule image

madieukhang avatar Aug 22 '24 10:08 madieukhang

For anyone stumbling accross this, I struggled after migrating from Angular 14 to 18, ng-charts 4 to 6.

I could not make the providers: [provideCharts(withDefaultRegisterables())] to work. I only have modules and adding it to their providers did not work.

The trick for me was to do as @madieukhang said, and import the BaseChartDirective instead of NgChartsModule in my (sub) module.

But then no scales etc. were detected by chart.js. Looked in NgChartsModule... so just add the following lines in your (sub) module:

import { BaseChartDirective } from 'ng2-charts';
import { Chart, registerables } from 'chart.js';

Chart.register(...registerables);

@NgModule({
  imports: [ BaseChartDirective  ],
})
export class MySubModule { }

I have no idea if this is a desired way, at least it works. Maybe someone of the devs can help?

aschoerg avatar Sep 10 '24 09:09 aschoerg

... after playing around, I finally found out:

in your sub-module just add:

import { BaseChartDirective, provideCharts, withDefaultRegisterables } from 'ng2-charts';

@NgModule({
  imports: [ BaseChartDirective ],
  providers: [provideCharts(withDefaultRegisterables())]
})
export class MySubModule { }

The docs mislead me with the standalone component...

aschoerg avatar Sep 10 '24 10:09 aschoerg

... after playing around, I finally found out:

in your sub-module just add:

import { BaseChartDirective, provideCharts, withDefaultRegisterables } from 'ng2-charts';

@NgModule({
  imports: [ BaseChartDirective ],
  providers: [provideCharts(withDefaultRegisterables())]
})
export class MySubModule { }

The docs mislead me with the standalone component...

Thanks for the info. It worked.

lingoyak avatar Sep 27 '24 15:09 lingoyak

Anybody looking at this using nx and wondering why it's not working, try nx reset.

Tobias243 avatar Sep 29 '24 10:09 Tobias243

... after playing around, I finally found out:

in your sub-module just add:

import { BaseChartDirective, provideCharts, withDefaultRegisterables } from 'ng2-charts';

@NgModule({
  imports: [ BaseChartDirective ],
  providers: [provideCharts(withDefaultRegisterables())]
})
export class MySubModule { }

The docs mislead me with the standalone component...

Thanks it worked for me as well.

khurramshafeeq avatar Feb 21 '25 09:02 khurramshafeeq

... after playing around, I finally found out:

in your sub-module just add:

import { BaseChartDirective, provideCharts, withDefaultRegisterables } from 'ng2-charts';

@NgModule({
  imports: [ BaseChartDirective ],
  providers: [provideCharts(withDefaultRegisterables())]
})
export class MySubModule { }

The docs mislead me with the standalone component...

encounter the same problem from ng 17 to 18, fixed with @aschoerg solution as well.

IMHO This NEED to be addressed in the readme and/or Docs.

netalex avatar Apr 18 '25 06:04 netalex