Created
September 4, 2017 16:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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