Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Created September 4, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joduplessis/4172a2a2bd4aa54d6e5dd1e52c4a545c to your computer and use it in GitHub Desktop.
Save joduplessis/4172a2a2bd4aa54d6e5dd1e52c4a545c to your computer and use it in GitHub Desktop.
Boilerplate for updating ng2-charts in Angular. This is in reference to a couple of bugs where changing data dynamically doesn't update the chart data. See more here https://github.com/valor-software/ng2-charts/issues/291
import { Component, SimpleChanges, ViewChildren } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts/ng2-charts';
@Component({
selector: 'app',
template: '<canvas baseChart #chart
[chartType]="'line'"
[options]="chartOptions"
[labels]="chartLabels"
[colors]="chartColors"
[datasets]="chartData"></canvas>',
})
export class DashboardComponent implements OnInit, After {
@ViewChildren(BaseChartDirective) charts: QueryList<BaseChartDirective>;
updateCharts(): void {
this.charts.forEach((child) => {
if (child.chartType == 'line') {
child.labels = ['Labels','go','here'];
child.data = [1,2,3];
}
child.ngOnChanges({} as SimpleChanges);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment