我这里使用了mixins的方法;
在 util/resizeMixin.js
export default {
data() {
return {
chart: null,
};
},
mounted() {
window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
window.removeEventListener('resize', this.handleResize);
},
methods: {
handleResize() {
setTimeout(()=>{
this.chart.resize();
},300)
// const width = window.innerWidth;
// const height = window.innerHeight;
// console.log(`窗口宽度: ${width}px, 窗口高度: ${height}px`);
},
},
};
在具体的页面写
<template>
<div class="secondLine">
<div id="typeError" style="width: 650px; height: 250px"></div>
</div>
</template>
<script>
import ResizeHandler from "@/util/resizeMixin";
import echarts from "echarts";
export default {
data() {
return {
options: {},
};
},
mixins: [ResizeHandler],
mounted() {
this.drawnLine();
},
methods: {
drawnLine() {
this.chart = echarts.init(document.getElementById("typeError"));
this.chart.setOption({
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
grid: {
left: "3%",
right: "4%",
bottom: "20%",
containLabel: true,
},
xAxis: [
{
type: "category",
// data: ["T01", "T02", "T03", "T04", "T05", "T06", "T07"],
data: [
"参数超限",
"燃弧异常",
"受电弓异常",
"温度超限",
"线磨耗超限",
"悬挂异常",
"接触力超限",
"硬点异常",
],
axisTick: {
alignWithLabel: true,
},
axisLine: {
show: false,
lineStyle: {
color: "white",
},
},
splitLine: {
show: false,
},
},
],
yAxis: [
{
type: "value",
splitLine: {
show: true,
lineStyle: {
color: "#305583",
},
},
axisLine: {
show: false,
lineStyle: {
color: "white",
},
},
},
],
series: [
{
type: "bar",
barWidth: "30%",
data: [990, 552, 500, 334, 903, 330, 720, 600],
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "#94C2FD",
},
{
offset: 1,
color: "#5B8FF9",
},
]),
barBorderRadius: 11,
},
},
},
],
});
this.chart.resize();
},
},
};
</script>
<style scoped>
.secondLine {
display: flex;
justify-content: center;
}
</style>