使用环境 VUE
当手动给echarts的父容器添加类名改变大小时,直接重新调用创建echarts的方法,echarts大小也不会改变。于是我添加了个setTimeout暂时解决了问题,现在跟大家分享一下。
<template>
<div style="height:100%;">
<button @click="onChange">点击</button>
<div class="chartWrap" :class="{ high: highFlag }" ref="chartWrap">
<div id="chart"></div>
</div>
</div>
</template>
<script>
import * as echarts from 'echarts';
export default {
name: '',
data() {
return {
yData: ['很吵加工价', '晒解决的解放军', '时偶尔会哦', "hi黑ofhi哦啊好囧"],
series1: [15, 4, 5, 15],
series2: [5, 45, 11, 42],
highFlag:false
};
},
methods: {
initChart() {
this.chart = echarts.init(document.getElementById('chart'))
let option = {
title: {
text: '表格'
},
xAxis: {
type: 'value',
},
yAxis: {
type: 'category',
data: this.yData
},
series: [
{
type: 'bar',
name: '什么',
data: this.series1
},
{
type: 'bar',
name: '碍地方级',
data: this.series2
}
]
}
this.chart.setOption(option, true)
},
onChange() {
this.highFlag = !this.highFlag // 让div添加新类名
if (this.highFlag) {
setTimeout(() => {
this.yData = ['1516161', 1516161, 1516161, 151516, 15161, 1161];
this.series1 = [12, 12, 12, 12, 51, 1]
this.series2 = [14, 15, 12, 13, 12, 15]
this.initChart()
this.chart.resize({ animation: { duration: 1000 } });
}, 500);
}else{
setTimeout(() => {
this.yData = ['1516161', 1516161, 1516161, 151516];
this.series1 = [12, 12, 12, 12]
this.series2 = [14, 15, 12, 13]
this.initChart()
this.chart.resize({ animation: { duration: 1000 } });
}, 500);
}
}
},
mounted() {
this.initChart()
},
};
</script>
<style scoped lang="scss">
.chartWrap {
width: 100%;
height: 300px;
#chart {
width: 100%;
height: 100%;
}
}
.high {
height: 400px;
}
</style>
setOption中第二个参数的作用
当我们已经生成了一个echarts图,我们想删除掉它里面的一些数据,但是无效,这是我们只需要this.chart.setOption(option,true)
这样即可。