echarts当容器大小变化时跟着变

<template>
  <div ref="chartContainer" class="chart-container">
    <!-- 图表将渲染到这个容器中 -->
  </div>
</template>

<script>
import * as echarts from "echarts";
import { bars } from "@/api/api";
export default {
  name: "EChartsComponent",
  data() {
    return {
      chart: null, // 用于存储ECharts实例
      barsData: [],
    };
  },
  mounted() {
    this.getdata();
    setInterval(() => {
      this.getdata();
    }, 120000);
    // this.initChart();
    window.addEventListener("resize", this.resizeChart);
  },
  // beforeUpdate() {
  //   window.removeEventListener("resize", this.resizeChart);
  // },
  // updated() {
  //   this.initChart();
  //   window.addEventListener("resize", this.resizeChart);
  // },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
    if (this.chart) {
      this.chart.dispose(); // 销毁图表实例
    }
  },
  methods: {
    getdata() {
      let obj = {
        t: "GetTestBars",
        f: "7",
        l: JSON.parse(window.localStorage.getItem("user")).LastSysLineID,
        testRoomCode: JSON.parse(window.localStorage.getItem("user"))
          .TestRoomCode,
      };
      bars(obj).then((res) => {
        if (res.data.Flag == 1) {
          this.barsData = res.data.Data;
          this.initChart();
        }
      });
    },
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      if (chartContainer) {
        const width = chartContainer.clientWidth;
        const height = chartContainer.clientHeight;
        if (this.chart) {
          this.chart.resize({
            width,
            height,
          });
        } else {
          this.chart = echarts.init(chartContainer);
          this.chart.setOption({
            // 设置ECharts图表配置
            title: {
              text: "配料签名信息",
              left: "center",
              top: "5%",
              textStyle: {
                color: "#01d1f2",
                fontSize: 16,
              },
            },
            tooltip: {
              trigger: "axis",
            },
            grid: {
              // 设置图表的布局位置
              height: "auto", // 柱状图向左偏移20%
              padding: [0, 0, 0, 5],
              bottom: "15%",
            },
            xAxis: {
              type: "category",
              data: ["试验", "批准", "领取", "监理"],
              axisLabel: {
                // 设置X轴标签的字体颜色
                color: "#01d1f2", // 红色
                // 设置X轴标签的字体大小
                fontSize: 13,
              },
            },
            yAxis: {
              type: "value",
              axisLabel: {
                // 设置X轴标签的字体颜色
                color: "#01d1f2", // 红色
                // 设置X轴标签的字体大小
                fontSize: 12,
              },
            },
            series: [
              {
                name: "已签数",
                type: "bar",
                data: this.barsData[0],
                itemStyle: {
                  barBorderRadius: 12, // 左上角、右上角、右下角、左下角
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#fccb05" }, // 起点颜色
                    { offset: 1, color: "#f5804d" }, // 终点颜色
                  ]),
                },
              },
              {
                name: "待签数",
                type: "bar",
                data: this.barsData[1],
                itemStyle: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: "#8bd46e",
                    },
                    {
                      offset: 1,
                      color: "#09bcb7",
                    },
                  ]),
                  barBorderRadius: 11,
                },
              },
              {
                name: "超时数",
                type: "bar",
                data: this.barsData[2],
                itemStyle: {
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: "#248ff7",
                    },
                    {
                      offset: 1,
                      color: "#6851f1",
                    },
                  ]),
                  barBorderRadius: 11,
                },
              },
            ],
          });
          window.dispatchEvent(new Event("resize")); // 触发resize事件以适应初始大小
        }
      }
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize(); // 当容器大小变化时,调用resize方法以适应新的宽高
      }
    },
  },
};
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%; /* 可以设置具体的像素值,或者相对值(如vh, vw, %等) */
}
</style>

第二种简单点

let myChart = echarts.init(document.getElementById("mychart"));
myChart.setOption(option);
//随着屏幕大小调节图表
window.addEventListener("resize", () => {
     myChart.resize();
});

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值