Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  66] [ 4]  / answers: 1 / hits: 20706  / 7 Years ago, wed, april 19, 2017, 12:00:00

I think my issue is a known one, everytime I make a new chart and hover my mouse over some points in the canvas, the old charts flash in and out all the time. Here's one of the threads I followed to try and fix it, but nothing seems to work. I tried re-appending the canvas, using destroy, clear, if statements which should clear it, but nothing.



Here's my code:



var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: chartjsDate,
datasets: [{
label: 'temp',
data: chartjsTemp,
backgroundColor: rgba(240,240,240,0.5)
}]
}
});


I tried adding myChart.destroy(); before and after that code, even if(myChart!=null){myChart.destroy();}, but still nothing. Any help on how to fix it would be appreciated. All other threads I found are quite old and their solutions don't work.



Edit: Some stuff I tried, to no avail:



var myChart;
if (myChart != undefined || myChart !=null) {
myChart.destroy();
}

More From » charts

 Answers
5

Solved! I added this above the javascript code:



var button = document.getElementById(submitButton);
submitButton.addEventListener(click, function(){
myChart.destroy();
});


And changed my submit button to have the id submitButton, like this:



<input type=submit class=btn btn-danger id=submitButton value=Send />


This way, everytime you press the submit button, it destroys the previous chart. The weird thing is that when trying to use myChart.destroy(); I got errors.


[#58096] Monday, April 17, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;