angular2-chartjs
Version:
Chart.js component for Angular2
35 lines (29 loc) • 909 B
text/typescript
import { Component, Input, ElementRef, OnInit, OnChanges, SimpleChanges } from '@angular/core';
export class ChartComponent implements OnInit, OnChanges {
chart: any;
type: string;
data: any;
options: any;
constructor(private elementRef: ElementRef) { }
ngOnInit() {
this.chart = new Chart(this.elementRef.nativeElement.querySelector('canvas'), {
type: this.type,
data: this.data,
options: this.options
});
}
ngOnChanges(changes: SimpleChanges) {
if (this.chart && changes['data']) {
let currentValue = changes['data'].currentValue;
['datasets', 'labels', 'xLabels', 'yLabels'].forEach(property => {
this.chart.data[property] = currentValue[property];
})
this.chart.update();
}
}
}