使用Echarts插件的時候,多次加載會出現There is a chart instance already initialized on the dom.的警告,意思是DOM上已經初始化了一個圖表實例。
解決辦法:
在使用Echarts插件的方法外面定義一個全局變量(注意:一定得是全局變量),然後在插件使用的方法內,添加判斷,調用eChart.dispose()方法先將圖表銷燬,然後再初始化就不會出現警告了。
var rateChart;//全局變量
function rate_fold(comPany,dataMon,nowSeries){
if (rateChart != null && rateChart != "" && rateChart != undefined) {
rateChart.dispose();//銷燬
}
rateChart = echarts.init(document.getElementById('rate_fold_line'));//初始化
var option = {title: {text: "大用戶成功率"},tooltip: {trigger: 'axis'},
legend: {data: comPany,orient:'vertical',x: 'right',y: 'center'},
calculable: true,xAxis: [{type: 'category',boundaryGap: false,
data: dataMon,axisLabel: {rotate: 50}}],
yAxis: [{type: 'value',max: 100,min: 98,splitNumber:7}],
series: nowSeries};
rateChart.setOption(option);
}