score:77
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
.
Source: stackoverflow.com
Related Query
- NG2-Charts Can't bind to 'datasets' since it isn't a known property of 'canvas'
- Can't bind to 'data' since it isn't a known property of 'canvas'
- ng2-chart with Angular 4: Can't bind to 'data' since it isn't a known property of 'canvas'
- Can't bind to 'chartType' since it isn't a known property of 'canvas' with angular12
- Chartjs cannot read property datasets of undefined
- How to add images as labels to Canvas Charts using chart.js
- Chart.js - Multiple Doughnut Charts on same Canvas
- How can I implement Polar area charts with Chart.js and Canvas
- ng2 charts bar chart need spacing between 2 bars in series Angular
- Ng2 charts - Animation doesn't work on line chart onInit
- Hide the element text on line charts since it overlaps with the line
- TypeError: Cannot assign to read only property ... - Editing react-chartjs-2 datasets with Redux
- bind first property value of an array of object into chart.js
- Is there a max width of canvas Chart.js can draw charts within?
- display ng2 chart when the property of data returned is >=0
- How to 1. pass two datasets and 2.have permanent label on charts in Chartjs
- Is it possible to combine multiple charts in one canvas in chartjs?
- How to add space between datasets in ng2 doughnut chart
- The dataset for bar graph is not set properly using ng2 charts and ng5-slider in Angular
- The dataset in ng2 charts for bar graph is not setting properly
- Using a function in ng2 Charts
- Export Canvas Charts to png and pdf
- Chart.js canvas rendered by ng-repeat does not show charts
- Switching between different Chart.js charts using the same canvas
- chart.data.datasets.push() also adds datasets to other charts in array
- Angular 8 and ng2 charts - Updating labels and data
- How to run Chart.js samples using source code
- Charts js. How to pass datasets use a select dropdown by angular instead jquery
- Create Vue.js + Chart.js charts using datasets stored outside in a csv file
- How to bind json array data to chart.js with same canvas id?
More Query from same tag
- How to build dynamic charts with Chart.js
- Exclude value if under a certain percentage
- How to assign different background colors to chart.js pie chart data set?
- Why can't I use a variable in same function scope in Javascript?
- Frontend and backend for chart using chartjs, mongodb, and controller
- updating chartjs pie chart by using .keypress() not working
- Chart.js 3.x custom positioners: cannot use them in TypeScript
- Try to change style in current month in chartJS
- Is it possible to define data attributes for each dataset value in a Chart.js chart?
- Loading json file to chartjs
- How to specify ticks locations in chart.js?
- chartjs: trying to rotate the y-Axis label
- Chart.js tooltip background color setting
- Chart.js minimal width of Chart with scrollable overflow
- chart.js hide gridline inside chartarea but keep y-axis border
- Example: Doughnut | Error: Uncaught ReferenceError: Utils is not defined
- Chart.js javascript from db stopped working after adding axes
- Chart.js - get base 64 encoded string as variable
- To display data from mock server into chartjs using ember
- angular-chart.js custom color based on chart label values
- Is it possible to set the background color of a sector in a radar chart in Chart.js?
- drawing bar chart with Chart.js
- How can we type a variable which is a method in TypeScript ? (Using Chart.js)
- Chart.js data background color is overwriting point background color
- Prevent label of another data from being toggled whenever a label from one data is toggled
- Can I plot random points on a chart.js or Google Charts chart?
- Why is the html page only displaying one of the charts?
- Draw horizontal lines in Chart.js 2.0
- How can I update my ChartJS in real time (It's works only when I zoom-in and zoom-out)
- Why is my line graph going backwards in chartjs?