ng2-charts update labels and data

前端 未结 12 1057
长发绾君心
长发绾君心 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:55

    This is an issue in the library ng2-charts, to resolve it I have cloned the github of ng2-charts in my app directory and have done following steps :

    • npm install
    • in appmodule import ng-2charts.ts from src directory.
    • add this updateChartLabels function to chart.ts file
    • call it in the onChanges function.

    public ngOnChanges(changes: SimpleChanges): void { if (this.initFlag) {

      if(changes.hasOwnProperty('labels')){
        console.log('labels changes ...');
        this.updateChartLabels(changes['labels'].currentValue);
      }
    //..
    //...
    }
    
    private updateChartLabels(newLabelsValues: string[] | any[]): void {
    this.chart.data.labels = newLabelsValues;
    }
    

提交回复
热议问题