2017-04-19 37 views
1

我認爲我的問題是已知的問題,每次我製作新圖表並將鼠標懸停在畫布上的某些點上時,舊圖表始終閃爍。 Here's one of the threads我跟着嘗試修復它,但似乎沒有任何工作。我試圖重新添加畫布,使用銷燬,清除,如果哪個應該清除它,但沒有。ChartJs鼠標懸停錯誤(顯示以前的圖表)

這裏是我的代碼:

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)" 
     }] 
    } 
}); 

我嘗試過,並且代碼之後,甚至if(myChart!=null){myChart.destroy();}加入myChart.destroy();,但仍然一無所獲。任何幫助如何解決它將不勝感激。我發現的所有其他線程都很老,他們的解決方案不起作用。

編輯:有些東西,我試過了,沒有用:

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

回答

1

解決了!我加入這個JavaScript代碼上面:

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

而且改變了我的提交按鈕,有標識「提交按鈕」,這樣的:你按下提交鍵

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

這種方式,每次,它破壞前面的圖表。奇怪的是,當試圖使用myChart.destroy();我得到了錯誤。

+0

如何以及在哪裏聲明瞭您的myChart? – hyperfkcb

0
var ctxLine = document.getElementById("line-chart").getContext("2d"); 


//var myLineChart; 
//myLineChart; 
if(window.bar != undefined) 
    window.bar.destroy(); 
window.bar = new Chart(ctxLine, {});