vue echarts 自适应问题,相对于屏幕,相对于主体,element-resize-detector


yarn add element-resize-detector -S
//引入
 import elementResizeDetectorMaker from "element-resize-detector"

===============以下为整页复制的echarts板块自适应================
<template>
  <div>
    <div class="echa" :style="{ width: widthCha }" id="main"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import elementResizeDetectorMaker from "element-resize-detector";
export default {
  mounted() {
    this.fn();

    //监听元素变化
    let erd = elementResizeDetectorMaker();
    let that = this;
    erd.listenTo(document.getElementById("main"), function (element) {
      that.$nextTick(function () {
        //使echarts尺寸重置
        that.myChart.resize();
      });
    });
  },
  methods: {
    fn() {
      //   监听body宽度进行echarts自适应
      window.onresize = () => {
        return (this.screenwidth = document.body.clientWidth);
      };

      // 基于准备好的dom,初始化echarts实例
      this.myChart = echarts.init(document.getElementById("main"));
      window.onresize = this.myChart.resize;
      this.myChart.setOption({
        title: {
          show: true,
          text: "2020年服务类合同-合同金额前5公司总金额及占比情况",
          x: "center",
          y: "top",
          textAlign: "left",
        },

        series: [
          {
            name: "面积模式",
            type: "pie",
            //   radius: [20, 150],控制饼图大小
            center: ["50%", "50%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 8,
            },
            data: [
              { value: 40, name: "rose 1" },
              { value: 38, name: "rose 2" },
              { value: 32, name: "rose 3" },
              { value: 30, name: "rose 4" },
              { value: 28, name: "rose 5" },
              { value: 26, name: "rose 6" },
              { value: 22, name: "rose 7" },
              { value: 18, name: "rose 8" },
            ],
          },
        ],
      });
    },
  },
  data() {
    return {
      widthCha: "100%",
    };
  },
};
</script>

<style lang="less" scoped>
.echa {
  height: 500px;
}
</style>

在这里插入图片描述
主要针对于收缩对div造成的宽度影响

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Echarts 是一个基于 Vue.js 的图表库,它可以方便地在 Vue.js 项目中使用 Echarts 进行数据可视化展示。关于 Vue Echarts自适应问题,可以通过以下几种方式来实现: 1. 使用 CSS 样式:可以通过设置图表容器的宽度和高度为百分比,使其随着父容器的大小变化而自适应调整。例如: ```html <template> <div class="chart-echarts> </div> </template> <style> .chart-container { width: 100%; height: 100%; } </style> ``` 2. 监听窗口大小变化:可以通过监听窗口的 resize 事件,在窗口大小变化时重新渲染图表。例如: ```html <template> <div class="chart-container"> <vue-echarts :options="chartOptions" ref="chart"></vue-echarts> </div> </template> <script> export default { data() { return { chartOptions: {...} }; }, mounted() { window.addEventListener('resize', this.handleResize); }, beforeDestroy() { window.removeEventListener('resize', this.handleResize); }, methods: { handleResize() { this.$refs.chart.resize(); } } }; </script> <style> .chart-container { width: 100%; height: 100%; } </style> ``` 3. 使用 Vue Echarts 提供的自适应方法:Vue Echarts 提供了一个 `resize` 方法,可以手动触发图表的自适应调整。例如: ```html <template> <div class="chart-container"> <vue-echarts :options="chartOptions" ref="chart"></vue-echarts> </div> </template> <script> export default { data() { return { chartOptions: {...} }; }, mounted() { this.$nextTick(() => { this.$refs.chart.resize(); }); } }; </script> <style> .chart-container { width: 100%; height: 100%; } </style> ``` 以上是几种常见的实现方式,你可以根据具体的需求选择适合的方法来实现 Vue Echarts自适应效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值