Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'destroy' previous chart (canvas) after new one is shown | Angular 4

I have a code that update the data of an Graphic after a drop down is selected. However when I select an option it looks like the new Graphic and the old Graphic stays on the same canvas at the same time.

here's my code:

funcao(){

  this.graphicService.getDatas().subscribe(datas => {
        this.datas = datas; //Gets the data from the API and put on local datas variable

        if(this.data[0].dimension == "Tech")
        {
          this.counter=0;
        }
        else if(this.data[0].dimension == "Bus"){
          this.counter=1;
        }
              this.barChartData = { //Bar graphic data
                  labels: ["Entry", "Foundation"],
                  datasets: [{
                      label: this.datas[this.counter].subdimensions[0].name,
                      backgroundColor: 'rgba(37, 230, 131,0.7)'
                      data: [
                          this.datas[this.counter].subdimensions[0].entry,
                          this.datas[this.counter].subdimensions[0].foundation
                      ]
                  }]

              };

              this.canvas = document.getElementById('myChart');
              this.ctx = this.canvas.getContext('2d');

              let myChart = new Chart(this.ctx, { // Bar graphic configs
                     type: 'bar',
                     data: this.barChartData,
                     options: {
                         scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                          }
                     }
                 });
      });
}

HTML

<select [(ngModel)]="data[0].dimension" (change)="funcao()" class="form-control">
  <option *ngFor="let data of datas" [value]="data.dimension">{{ data.dimension }}</option>
</select>

  <canvas id="myChart"></canvas>

What I want to do is the old graphic disappears and only the new one stay on screen.

like image 723
Gustavo Avatar asked Oct 20 '25 13:10

Gustavo


2 Answers

Seems like you are using the ChartJS library. In that case, you can use the destroy() method to destroy any previous instance of chart.

ꜰɪʀꜱᴛ

add a property (in which the chart instance gonna be stored) in your chart component class :

public myChart: Chart

ꜱᴇᴄᴏɴᴅ

check and destroy the chart instance (if any) before creating a new one :

...
if (this.myChart) this.myChart.destroy(); //destroy prev chart instance
this.myChart = new Chart(this.ctx, {
         type: 'bar',
         data: this.barChartData,
         ...
like image 117
ɢʀᴜɴᴛ Avatar answered Oct 23 '25 02:10

ɢʀᴜɴᴛ


I know this has already been solved, but I was having the same problem and this solutions didn't work, probably because I am using recent versions.

In case someone is reading this thread and is in the same situation as me, this is what worked in my case: manually removing the canvas from the DOM in the ngOnDestroy() hook.

  ngOnDestroy(){
    ( < HTMLCanvasElement > document.getElementById('myChartCanvas')).remove();
  }
like image 42
Pipimi Avatar answered Oct 23 '25 03:10

Pipimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!