ng2-charts update labels and data

前端 未结 12 1052
长发绾君心
长发绾君心 2020-12-03 01:25

I\'m trying to create dynamically a chart using ng2-chart, I get information from an angular 2 service, when I change only labels of chart it works and when I change data on

12条回答
  •  离开以前
    2020-12-03 01:42

    Using BaseChartDirective i did chart update and it served the purpose. Sample below:

    import { BaseChartDirective } from 'ng2-charts/ng2-charts';
    

    inside the class add as below

    @ViewChild(BaseChartDirective) chart: BaseChartDirective;
    

    While you have the values to be changed, add as below

    setTimeout(() => {
    if (this.chart && this.chart.chart && this.chart.chart.config) {
      this.chart.chart.config.data.labels = this.labels_pie;
      this.chart.chart.update();
    }
    });
    

提交回复
热议问题