vue3 页面尺寸改变 Echarts自适应大小

<template>
  <div class="area_car_container height_full">
    <div class="diff back" ref="areaCarRef"></div>
  </div>
</template>

<script setup>
import myTitle from "@/views/warehouse/components/title.vue";
import { getAreaCar, getCarAreaIds } from "@/api/main";
import { ref, reactive, onMounted, onUnmounted, nextTick } from "vue";
import img2 from "@/assets/warehouse/icon/circle.png";

const { proxy } = getCurrentInstance();
const areaCarRef = ref(null);
let chart = null;
/**
 * 数据部分
 */
const data = reactive({
  timer: null,
  groceriesId: null,
  coalId: null,
  oilId: null,
  
  areaCar: [
    {
      name: "港区",
      value: 0,
      color: "rgba(0,0,0,0)",
      colorList: ["rgba(31,102,192,0.8)", "#2e78eca8"],
    },
    {
      name: "化工区",
      value: 0,
      color: "rgba(0,0,0,0)",
      colorList: ["rgba(253, 167, 223,0.8)", "#ff9ff3"],
    },
    {
      name: "杂货区",
      value: 0,
      color: "rgba(0,0,0,0)",
      colorList: ["rgba(212, 191, 144,0.8)", "#bdaa80"],
    },
    {
      name: "煤炭区",
      value: 0,
      color: "rgba(0,0,0,0)",
      colorList: ["rgba(163, 203, 56,0.8)", "#7b992a"],
    },
  ],
});

const getInfo = () => {
  getAreaCar().then((res) => {
    const { total, oil, coal, groceries, coalId, oilId, groceriesId } =
        res.data.carNumMonitor,
      array = [total, oil, groceries, coal];
    data.groceriesId = groceriesId;
    data.coalId = coalId;
    data.oilId = oilId;
    data.areaCar.forEach((item, index) => {
      item.value = array[index];
    });

    if (chart) {
      updateChart();
    } else {
      nextTick(() => {
        initChart();
      });
    }
  });
};

// 初始化定时器
const initTimer = () => {
  getInfo();
  if (data.timer) clearInterval(data.timer);
  data.timer = setInterval(() => {
    getInfo();
  }, 1000 * 15);
};
initTimer();

// 初始化echarts
const initChart = () => {
  chart = proxy.$echarts.init(areaCarRef.value);

  const datas = computedData(),
    option = {
      grid: {
        show: false,
        top: 10,
        bottom: 10,
      },
      xAxis: [
        {
          gridIndex: 0,
          type: "value",
          show: false,
          min: 0,
          max: 100,
          nameLocation: "middle",
          nameGap: 5,
        },
      ],
      yAxis: [
        {
          gridIndex: 0,
          min: 0,
          show: false,
          max: 100,
          nameLocation: "middle",
          nameGap: 30,
        },
      ],
      series: [
        {
          type: "scatter",
          symbol: "circle",
          symbolSize: 120,
          label: {
            normal: {
              show: true,
              formatter: "{b}",
              color: "#fff",
              textStyle: {
                fontSize: "20",
              },
            },
          },
          animationDurationUpdate: 1000,
          animationEasingUpdate: 1000,
          animationDelay: function (idx) {
            // 越往后的数据延迟越大
            return idx * 100;
          },
          itemStyle: {
            normal: {
              color: "#00acea",
            },
          },
          data: datas,
        },
      ],
      graphic: [
        {
          type: "image",
          id: "logo1",
          left: "32%",
          bottom: "10%",
          z: -10,
          bounding: "raw",
          origin: [76, 76],
          style: {
            image: img2,
            width: 50,
            height: 50,
            opacity: 0.4,
          },
        },
        {
          type: "image",
          id: "logo2",
          left: "40%",
          bottom: "35%",
          z: -10,
          bounding: "raw",
          origin: [76, 76],
          style: {
            image: img2,
            width: 90,
            height: 90,
            opacity: 0.4,
          },
        },
      ],
    };

  chart.setOption(option);

  chart.on("click", (params) => {
    let param = {
      type: 1,
    };
    const { dataIndex } = params,
      { coalId, oilId, groceriesId, type } = data, 
      objFun = {
        0: () => {
          param.id = 99;
        },
        1: () => {
          param.id = oilId;
        },
        2: () => {
          param.id = groceriesId;
        },
        3: () => {
          param.id = coalId;
        },
      };
    if (objFun[dataIndex]) objFun[dataIndex]();
    if (dataIndex == 0) {
    } else {
      getCarAreaIds(param).then((res) => {});
    }
  });
};

// 更新echarts
const updateChart = () => {
  const datas = computedData(),
    option = chart.getOption();
  option.series[0].data = datas;

  chart.setOption(option);
};

// 生成echarts数据
const computedData = () => {
  // 偏移量
  const offsetData = [
      [80, 55],
      [35, 73],
      [15, 33],
      [60, 23],
    ],
    // symbolSize 散点气泡大小
    symbolSizeData = [130, 115, 100, 85],
    datas = data.areaCar.map((item, index) => {
      return {
        name: item.name + "\n\n" + item.value + "辆",
        value: offsetData[index],
        symbolSize: symbolSizeData[index],
        label: {
          normal: {
            textStyle: {
              fontSize: 16,
            },
          },
        },
        itemStyle: {
          normal: {
            color: {
              x: 0,
              y: 0,
              x2: 1,
              y2: 0,
              type: "linear",
              global: false,
              colorStops: [
                {
                  offset: 0,
                  color: item.color,
                },
                {
                  offset: 0.8,
                  color: item.colorList[0],
                },
              ],
              opacity: 0.8,
              shadowColor: item.colorList[1],
              shadowBlur: 10,
              shadowOffsetX: 1,
              shadowOffsetY: 1,
            },
          },
        },
      };
    });
  return datas;
};

onMounted(() => {
  // 监听窗口大小变化并更新图表大小
  window.addEventListener("resize", () => {
    if (chart) chart.resize();
  });
});

onUnmounted(() => {
  if (data.timer) clearInterval(data.timer);
  if (chart) chart.dispose();
});
</script>
<style scoped lang="scss"></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

web网页精选

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

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

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

打赏作者

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

抵扣说明:

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

余额充值