使用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);
}