ECharts图表集网站
第一次加载页面时,echarts初始化位置总是会有偏差,
解决:
加一个this.$nextTick()
<template>
<!-- 横向柱状图 -->
<div class="chart_view" id="chart3">
<!-- <div id="chartThree" :style="`width:${width}px;height:${height}px;`"></div> -->
<div id="chartThreet" style="width: 100%;height: 2.5rem;"></div>
</div>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
showEcharts() {
var myChart2 = this.$echarts.init(document.getElementById('chartThreet'));
myChart2.setOption({
grid: {
top: "15%",
left: "0",
right: "10%",
bottom: "5%",
containLabel: true
},
xAxis: {
type: "value",
axisLabel: {
textStyle: {
color: "#fff"
}
},
axisLine: {
LineStyle: {
color: "#fff"
}
}
},
yAxis: {
type: "category",
data: ['队伍6', '队伍5', '队伍4', '队伍3', '队伍2', '队伍1'],
axisLabel: {
textStyle: {
color: "#fff"
}
},
axisLine: {
LineStyle: {
color: "#fff"
}
}
},
series: [
{
type: "bar",
data: [226, 500, 600, 700, 570, 370],
label: {
show: true,
position: 'right'
},
itemStyle: {
barBorderRadius: [0,20,20,0],
color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
{
offset: 0,
color: "#005eaa"
},
{
offset: 0.5,
color: "#339ca8"
},
{
offset: 1,
color: "#fffddd"
}
])
}
}
]
})
// 初始化变形
this.$nextTick(() => {
myChart2.resize();
});
setTimeout(() => {
window.addEventListener('resize', function() {
myChart2.resize();
});
});
}
},
mounted() {
this.showEcharts();
}
}
</script>
<style scoped>
.chart_view {padding: .125rem;position: relative;width: 100%;height: 100%;}
</style>
解决方法
this.$nextTick(() => {
myChart2.resize();
});
setTimeout(() => {
window.addEventListener('resize', function() {
myChart2.resize();
})
});