echarts之响应式的实现

echarts之响应式的实现

BarChart.vue

<template>
  <div ref="chartP" class="bar-chart-wrap">
    <div :id="id" class="chart"></div>
  </div>
</template>

<script>
export default {
  name: "BarChart",
  props: {
    id: {
      type: String,
      default: "barChart",
    },
    seriesData: {
      type: Object,
      default: () => {},
    },
  },
  computed: {
    echarts() {
      return "echarts" + Math.random() * 100000;
    },
  },
  watch: {
    seriesData: {
      handler: function (newV) {
        console.log("newV", newV);
        this.drawBar();
      },
      deep: true,
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.drawBar();
    });
  },
  activated() {
    // 这是再keep-alive 缓存条件下的 从另外一个页面跳转到当前页面的 地图进行自适应
    if (this.myChart) {
      setTimeout(() => {
        this.myChart.resize();
      }, 200);
    }
  },
  methods: {
    drawBar() {
      this.myChart = this.$echarts.init(document.getElementById(this.id));
      this.myChart.setOption({
        color: ["#133891"],
        grid: {
          left: "0%",
          right: "0%",
          bottom: "0%",
        },
        xAxis: {
          type: "category",
          data: this.seriesData.xAxis,
          // data: ["2020", "2021"],
          splitLine: { show: false },
          axisTick: { show: false },
          axisLine: {
            // y轴刻度线
            show: false,
          },
        },
        yAxis: {
          type: "value",
          show: false,
        },
        series: [
          {
            data: this.seriesData.series,
            // data: ["222", "11"],
            type: "bar",
            barWidth: "30%",
            barGap: "70%",
            label: {
              show: true,
              position: "top",
            },
          },
        ],
      });
    },
  },
};
</script>

<style scoped lang="scss">
.bar-chart-wrap {
  width: 100%;
  height: 100%;
  position: relative;
  .chart {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    width: 100%;
    height: 100%;
  }
}
</style>

使用组件

<template>
  <div class="user2">
    <div class="user2-echrts">
      <div class="chart" v-for="(item, index) of sinosureList" :key="index">
        <div class="bar-chart">
          <barchart :id="`barchart${index + 1}`" :seriesData="item"></barchart>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import barchart from "./childCom/echarts/barChart.vue";
export default {
  name: "User2",
  components: {
    barchart,
  },
  data() {
    return {
      sinosureList: [
        {
          series: [38.06, 66.6],
          xAxis: ["2020", "2021"],
        },
        {
          series: [48.06, 26.6],
          xAxis: ["2020", "2021"],
        },
        {
          series: [58.06, 76.6],
          xAxis: ["2020", "2021"],
        },
      ],
    };
  },
  activated() {
    window.addEventListener("resize", this.resizeHandler, false);
  },
  deactivated() {
    window.removeEventListener("resize", this.resizeHandler);
  },
  methods: {
    // 这是再当前页面拉动 页面的时候 实现echart是响应式
    resizeHandler() {
      const barchart1 = this.$echarts.init(
        document.getElementById("barchart1")
      );
      const barchart2 = this.$echarts.init(
        document.getElementById("barchart2")
      );
      const barchart3 = this.$echarts.init(
        document.getElementById("barchart3")
      );
      barchart1 && barchart1.resize();
      barchart2 && barchart2.resize();
      barchart2 && barchart3.resize();
    },
  },
};
</script>
<style  lang="scss" scoped>
.echrts2 {
  margin-top: 1000px;
}
.user2 {
  height: 100%;
  width: 100%;
  .user2-echrts {
    display: flex;
    position: relative;
    height: 300px;
    width: 100%;
    background: #eee;
    .chart {
      width: 33.333%;
      height: 60%;
      display: flex;
      align-items: flex-end;
    }
    .bar-chart {
      width: 40%;
      height: 100%;
      margin-left: 32%;
    }
  }
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值