[Chartjs]-Attempting to destroy previous graph on canvas

2👍

Your code is riddle with issues, here is some of the stuff I see:


Look at what you are doing when you create the new charts:

var totalCasesChart = document.getElementById('totalCasesContainer').getContext('2d');
var caseBar = new Chart(totalCasesChart, casesOptions);
document.getElementById('casesGraphHeader').innerHTML = "Total Cases for " + country;

vs

 var totalCasesChart = document.getElementById('totalDeathsContainer').getContext('2d');
 new Chart(totalDeathsContainer, deathOptions);
 document.getElementById('deathsGraphHeader').innerHTML = "Total Deaths for " + country;

You are calling the:
await fetch('https://cors-anywhere.herokuapp.com/https://pomber.github.io/...');
again and again when you should do it just once…


There are many variables that should be global to reduce what you do in getCountryDataByMonth, a perfect example are the totalCasesChart and caseBar


I made a few tweaks to your code here:
https://raw.githack.com/heldersepu/hs-scripts/master/HTML/chart_test.html

Leave a comment