0
votes

I am having a spfx webpart where it shows a chart view based on the List selected in property pane. I can able to generate the chart but while changing the list the second chart data shows the prev chart data on hover.The below code is on render method in .tsx file

<ChartControl                  
        type={ChartType.Bar}                   
         data promise={this._data()}                      
         options={{
         legend: {
         display: true,
         position: "right",                
         },
         title: {
         display: true,
         text: "My Chart Data"
         },  
         }} />

The _data() method will be as below,

let Chart:Chart;
Chart.ChartData = {
    labels: ['Income','Expense','Debt','Savings'],
    datasets: [{
      label: 'Archive',
      data: [10,13,20,90]          
    }]
  };

I have tried to implement,

Chart.destroy()/Chart.clear()

but not working.It shows error and get rejected saying the Chart is un definded.

How to clear the old data,while loading next list in this scenario.

2
Could you give the error? - YLR
I save the date in state,and create a button to update the data in state, after I clicked the button twice, the correct data was displayed. - Amos
@YLR no error is pop up on the screen it shows undefined while debugging on Chart.Destroy() - Starter-code
@Amos_MSF how to save the chart data in state? - Starter-code

2 Answers

0
votes

@Starter-code

 this.state = {
          data: {
            labels:
              [
                'January', 'February', 'March', 'April', 'May', 'June', 'July'
              ],
            datasets:
              [{
                label: 'My Second Dataset',
                data:
                  [
                    65, 59, 80, 81, 56, 55, 40
                  ]
              }]
          }
        };
0
votes
Chart.datasets.pop();

Using this line in the code will empty the datasets, hence the new data will be added and hovering the chart will not show old data.