echarts代码结构(自用)

本文介绍了一个基于ECharts的设备使用状态监控组件,通过获取API数据动态更新饼状图,并实现屏幕适配,展示如何实时反映设备分布情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<template>
  <div ref="boxEcharts" class="echarts"></div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      leftEch: "",
      list: [],
      query: {
        currentPage: 1,
        pageSize: 20,
      },
    };
  },
  created() {
    this.initData(); // echarts需要的数据
  },
  mounted() {
    this.initEcharts();
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  // 销毁定时器
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    // echarts需要的数据
    initData() {
      request({
        url: `/api/SubDev/YkzptEqaccount`,
        method: "GET",
        data: this.query,
      }).then((res) => {
        this.list = res.data;
        this.updateChart(); // 更新echarts数据
      });
    },
    // 设备使用状态(初始化图表【饼图事列】)
    initEcharts() {
      this.leftEch = echarts.init(this.$refs.boxEcharts);
      let initoption = {
        title: {
          text: "设备使用状态统计",
          left: "center",
          top: 10,
        },
        tooltip: {
          trigger: "item",
        },
        grid: {
          left: "5%",
          right: "5%",
          bottom: "5%",
          top: "15%",
          containLabel: true,
        },
        legend: {
          orient: "vertical",
          left: "left",
          left: 20,
          top: 10,
        },
        series: [
          {
            type: "pie",
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.leftEch.setOption(initoption);
    },
    // 设备使用状态(数据更新图表)
    updateChart() {
      // 当拿到数据后,准备数据的配置项
      const dataOption = {
        // 如:
        series: [
          {
            type: "pie",
            data: this.list,
          },
        ],
      };
      this.leftEch.setOption(dataOption);
    },
    // 设备使用状态(当浏览器窗口大小发生变化,完成屏幕适配)
    screenAdapter() {
      let titleFontSize = (this.$refs.boxEcharts.offsetWidth / 100) * 3.6;
      // 浏览器分辨率大小相关的配置项
      const adapterOption = {
        // 如:
        title: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
        legend: {
          textStyle: {
            fontSize: titleFontSize / 3,
          },
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 3,
        },
        series: [
          {
            radius: titleFontSize * 3,
          },
        ],
      };
      this.leftEch.setOption(adapterOption);
      // 手动调用图表的 resize 才能产生效果
      this.leftEch.resize();
    },
  },
};
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值