I was unable to to compile a Production AOT build ("npm run build.prod.rollup.aot") in Angular Seed - after implementing ng2-charts in my app (when I run "npm run start.deving - I could've seen the chart, but it would fail for Karma UnitTesting - in the ***.spec.ts file).
Implement ng2-charts in an Angular-Seed
261 Views Asked by Yonatan Ayalon At
2
There are 2 best solutions below
0
On
I'm not using ng2-charts in my Angular 6.1 website; however, I am using chart.js by itself in mgechev/angular-seed. I decided I don't need or want the extra ng2-charts wrapper. In the end, you're really just using chart.js anyway.
As of writing this response, Chart.js developers have some work to do to make exporting its members compliant with newer standards.
To get Chart.js to work, all you need to do is:
Update
project.config.tsto include ROLLUP_NAMED_EXPORTSthis.ROLLUP_NAMED_EXPORTS = [ ...this.ROLLUP_NAMED_EXPORTS, { 'node_modules/chart.js/src/chart.js': ['Chart'] } ];Update
project.config.tsto include additional packages// Add packages const additionalPackages: ExtendPackages[] = [ { name: 'chart.js', path: 'node_modules/chart.js/dist/Chart.bundle.min.js' }, ... ];In your component
import { Chart } from 'chart.js'; ... export class MyComponent implements OnInit { @ViewChild('myChart') myChartRef: ElementRef; chartObj: Chart; ... }Then load chart configuration in ngOnInit() per Chart.js documentation
HTML will look something like this:
<div class="chart-container"> <canvas #myChart></canvas> </div>
The following solution did the work for me (after installing ng2-chart & charts.js via npm):
import ChartsModule in your main module (e.g. "home.module.ts"):
Then - provide it to let other components (that are inside this module) use this module (do the same in your equivalent "...spec.ts" file (e.g. "home.component.spec.ts"):
--
Just to make sure, I've added the actual chart implementation component (e.g. "line-chart.component.ts"):
and use it in each component (that is part of the module):