echarts封装resize函数

文章描述了一个如何在Vue组件中使用mixins方法处理窗口大小改变时,使ECharts图表自动调整大小的示例。开发者在util/resizeMixin.js中定义了resize事件监听器,并在具体页面中混入该mixin,确保图表在页面尺寸变化时能正确响应。
摘要由CSDN通过智能技术生成

我这里使用了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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天吃饭的羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值