ng2-charts update labels and data

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

    Apparently, if you do not modify the original reference to the labels array, it seems to work, at least for me. I mean, if you want a completely different set of labels, you should do something like this:

    In the template:

    
    

    In the ts component:

    this.lineChartLabels.length = 0;
    for (let i = tempLabels.length - 1; i >= 0; i--) {
      this.lineChartLabels.push(tempLabels[i]);
    }
    

    Or, using new ECMAScript syntax:

    this.lineChartLabels.length = 0;
    this.lineChartLabels.push(...tempLabels);
    

    The key is maybe the this.lineChartLabels.length = 0; statement, which practically 'empties' your array by setting its length to 0, without modifying the reference. Hope this helps!

提交回复
热议问题