Reputation: 115
I am currently exploring Angular2 in combination with TypeScript and I would like to include the Chartjs module in my application. In the chartjs documentation is shown how to do it using the common canvas html tag:
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {[...]});
</script>
How can I do similar things with Angular2 and TypeScript?
Thanks in advance!
Upvotes: 1
Views: 625
Reputation: 102
You can use npm module:
Then you can use it like this in your module:
import { ChartModule } from 'angular2-chartjs';
@NgModule({
imports: [ ChartModule ]
// ...
})
export class AppModule {
}
And in html template:
<chart [type]="type" [data]="data" [options]="options"></chart>
Don't forget to fill it with data ;)
Upvotes: 1
Reputation: 658037
Something like:
@Component({
selector: 'my-component',
template: `
<canvas #myChart" width="400" height="400"></canvas>
<script>
`)}
export class MyComponent {
@ViewChild('myChart') myChart;
ngAfterViewInit() {
var myChart = new Chart(this.target.nativeElement, {[...]});
}
}
Upvotes: 1