ng2-charts update labels and data

前端 未结 12 1107
长发绾君心
长发绾君心 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 02:00

    There is another way to do it:

    In your HTML you have

    
    
    

    and in the component I have a function which update the chart with new data, and then I clone the datasets and re-assign it

    drawChart(){
        this.ChartData=[{data: this.dataX, label: 'X'}]; // this.dataX has new values from some place in my code
        //nothing happened with my chart yet, until I add this lines:        
        let clone = JSON.parse(JSON.stringify(this.ChartData));
        this.ChartData=clone;
       //other stuff like labels etc.
    }
    

    this works for me, hope it works for you too

提交回复
热议问题