0

I did following steps: npm install ng2-charts@next --save

It installed

"ng2-charts": "^3.0.0-rc.7"

npm install chart.js --save

It installed

chart.js": "^2.9.4",

npm install --save-dev ng2-charts-schematics

npm install

ng serve -o

Angular version is 11.2

"@angular/compiler": "11.2.2",
"@angular/core": "11.2.2",
"@angular/forms": "11.2.2",

When the parent component page loads, it throws these errors:

  NG0303: Can't bind to 'type' since it isn't a known property of 'canvas'.
    
   NG0303: Can't bind to 'datasets' since it isn't a known property of 'canvas'.
    
   NG0303: Can't bind to 'labels' since it isn't a known property of 'canvas'.
    
   NG0303: Can't bind to 'options' since it isn't a known property of 'canvas'.
    
   NG0303: Can't bind to 'plugins' since it isn't a known property of 'canvas'.
    
   NG0303: Can't bind to 'legend' since it isn't a known property of 'canvas'.

What could have gone wrong? this is not working? On expanding this alerts, :

logUnknownPropertyError @ InteractionHandler.ts:10069 elementPropertyInternal @ InteractionHandler.ts:9961 ɵɵproperty @ InteractionHandler.ts:14700 PieChartComponent_Template @ template.html:2 executeTemplate @ InteractionHandler.ts:9545 refreshView @ InteractionHandler.ts:9414 refreshComponent @ InteractionHandler.ts:10580 refreshChildComponents @ InteractionHandler.ts:9211 refreshView @ InteractionHandler.ts:9464 refreshComponent @ InteractionHandler.ts:10580 refreshChildComponents @ InteractionHandler.ts:9211 refreshView @ InteractionHandler.ts:9464 refreshEmbeddedViews @ InteractionHandler.ts:10534 refreshView @ InteractionHandler.ts:9438 refreshComponent @ InteractionHandler.ts:10580 refreshChildComponents @ InteractionHandler.ts:9211 refreshView @ InteractionHandler.ts:9464 refreshEmbeddedViews @ InteractionHandler.ts:10534 refreshView @ InteractionHandler.ts:9438 refreshEmbeddedViews @ InteractionHandler.ts:10534 refreshView @ InteractionHandler.ts:9438 refreshComponent @ InteractionHandler.ts:10580 refreshChildComponents @ InteractionHandler.ts:9211 refreshView @ InteractionHandler.ts:9464 renderComponentOrTemplate @ InteractionHandler.ts:9528 tickRootContext @ InteractionHandler.ts:10754 detectChangesInRootView @ InteractionHandler.ts:10779 detectChanges @ InteractionHandler.ts:22792 tick @ InteractionHandler.ts:29516 (anonymous) @ InteractionHandler.ts:29397 ZoneDelegate.invoke @ zone.js:386 onInvoke @ InteractionHandler.ts:28535 ZoneDelegate.invoke @ zone.js:385 Zone.run @ zone.js:143 run @ InteractionHandler.ts:28419 next @ InteractionHandler.ts:29396 schedulerFn @ InteractionHandler.ts:25889 __tryOrUnsub @ Subscriber.ts:265 next @ Subscriber.ts:207 _next @ Subscriber.ts:139 next @ Subscriber.ts:99 next @ superPropBase.js:70 emit @ InteractionHandler.ts:25879 checkStable @ InteractionHandler.ts:28472 onHasTask @ InteractionHandler.ts:28552 ZoneDelegate.hasTask @ zone.js:441 ZoneDelegate._updateTaskCount @ zone.js:462 Zone._updateTaskCount @ zone.js:284 Zone.runTask @ zone.js:205 drainMicroTaskQueue @ zone.js:601 Promise.then (async) scheduleMicroTask @ zone.js:584 ZoneDelegate.scheduleTask @ zone.js:410 onScheduleTask @ zone.js:294 ZoneDelegate.scheduleTask @ zone.js:400 Zone.scheduleTask @ zone.js:231 Zone.scheduleMicroTask @ zone.js:251 scheduleResolveOrReject @ zone.js:881 resolvePromise @ zone.js:819 (anonymous) @ zone.js:739 webpackJsonpCallback @ bootstrap:25 (anonymous) @ layouts-admin-layout-admin-layout-module.js:1 Show 20 more frames InteractionHandler.ts:10069

Pie chart html:

<canvas baseChart [type]="'pie'" [datasets]="pieChartDatasets" [labels]="pieChartLabels" [options]="pieChartOptions" [plugins]="pieChartPlugins" [legend]="pieChartLegend">

Pie chart ts file

import { Component } from '@angular/core'; import { ChartType, ChartOptions } from 'chart.js'; // import { SingleDataSet, Label, monkeyPatchChartJsLegend, monkeyPatchChartJsTooltip } from 'ng2-charts';

@Component({ selector: 'app-pie-chart', templateUrl: './pie-chart.component.html', styleUrls: ['./pie-chart.component.css'] })

export class PieChartComponent {

// Pie public pieChartOptions: ChartOptions<'pie'> = { responsive: false, }; public pieChartLabels = [ [ 'Download', 'Sales' ], [ 'In', 'Store', 'Sales' ], 'Mail Sales' ]; public pieChartDatasets = [ { data: [ 300, 500, 100 ] } ]; public pieChartLegend = true; public pieChartPlugins = []; }

module.ts has these:

import { PieChartComponent} from 'src/app/components/pie-chart/pie-chart.component';

declarations: [ PieChartComponent ]

Parent html file have <app-pie-chart></app-pie-chart>

Please Suggest? https://github.com/valor-software/ng2-charts/issues/1459[Issue on official Github]

Vishesh
  • 133
  • 3
  • 11
  • I have checked solution/suggestion at https://stackoverflow.com/questions/43903174/cant-bind-to-data-since-it-isnt-a-known-property-of-canvas >>But it throw another error mentioned in the comments – Vishesh Jul 25 '22 at 02:09
  • Uncaught TypeError: chart_js__WEBPACK_IMPORTED_MODULE_3__.Chart.register is not a function at ng-charts.module.ts:32 – Vishesh Jul 25 '22 at 02:28

1 Answers1

0

You should not install the next branch of ng2-charts, that is an older version as the most recent one, also when using ng2-charts version 3.0 or above you need to make sure you have at least version 3.0 of Chart.js installed, it does not work with version 2.

Using npm i chart.js should not give you 2.9.4 as you claimed, it should install the latest version but to be sure you can just specify the latest versions for both:

npm i chart.js@3.8.2
npm i ng2-charts@3.1.2
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • Thanks @LeeLenalee , I deleted nodes_module and package-lock.json file, and re-installed these two libraries . Still getting these error > > `Error: ./node_modules/ng2-charts/fesm2015/ng2-charts.mjs 512:22-44 Can't import the named export 'ɵɵngDeclareNgModule' from non EcmaScript module (only default export is available)` – Vishesh Jul 25 '22 at 11:17
  • idk then what the issue is, can you reproduce it wit hstackblitz? – LeeLenalee Jul 25 '22 at 11:23
  • Thanks @LeeLenalee for suggesting that, I just replicated the error on stackblitz with same version numbers [chart.js@3.8.2 , ng2-charts@3.1.2](https://stackblitz.com/edit/angular-ivy-rzrqxu?file=package.json) > > > https://stackblitz.com/edit/angular-ivy-rzrqxu?embed=1&file=package.json – Vishesh Jul 26 '22 at 10:36